1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.util.*; public class TreeDiagramMaker extends JFrame{ private JPanel pleft=null; private JPanel pright=null; private JButton bnode=null; private JButton bline=null; private JButton bsave=null; private int counter=0; private int curindex=0; private ArrayList<Rectangle> nodes=null; private int mousex=0,mousey=0; private boolean pressed=false; private ArrayList<Integer> parent=null; private ArrayList<Integer> children=null; //private ArrayList<Point> lines=null; //private ArrayList<Point> lines2=null; private int counterp=0; int rect1=-1,rect2=-1; public TreeDiagramMaker(){ super("Tree Diagram Maker"); setSize(900,700); setLayout(new BorderLayout()); pleft=new JPanel(); bnode=new JButton(); BufferedImage bufimage=null; try{ bufimage=ImageIO.read(new File("node.png")); }catch(Exception e){ } bnode.setIcon(new ImageIcon(bufimage)); bnode.addActionListener(new ButtonHandler()); pleft.add(bnode); pright=new DrawDiagram(); pright.setPreferredSize(new Dimension(600,600)); pright.addMouseListener(new MouseHandler()); pright.addMouseMotionListener(new MouseHandler()); add(BorderLayout.WEST,pleft); add(BorderLayout.EAST,pright); nodes=new ArrayList<Rectangle>(); parent=new ArrayList<Integer>(); children=new ArrayList<Integer>(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class DrawDiagram extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; if(!nodes.isEmpty()){ for(int i=0;i<nodes.size();i++){ g2.draw(nodes.get(i)); } } if(pressed){ g2.draw(nodes.get(curindex)); } if(!parent.isEmpty()&&!children.isEmpty()){ for(int i=0;i<parent.size();i++){ int centerx1=(int)(nodes.get(parent.get(i)).getMinX())+(int)(nodes.get(parent.get(i)).getMaxX()-nodes.get(parent.get(i)).getMinX())/2; int centery1=(int)(nodes.get(parent.get(i)).getMaxY()); int centerx2=(int)(nodes.get(children.get(i)).getMinX())+(int)(nodes.get(children.get(i)).getMaxX()-nodes.get(children.get(i)).getMinX())/2; int centery2=(int)(nodes.get(children.get(i)).getMinY()); g2.drawLine(centerx1,centery1,centerx2,centery2); } } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==bnode){ nodes.add(counter,new Rectangle(0,0,100,80)); curindex=counter; counter++; repaint(); } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ } public void mouseDragged(MouseEvent e){ if(pressed){ mousex=e.getX(); mousey=e.getY(); nodes.get(curindex).setLocation(mousex,mousey); repaint(); } } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseReleased(MouseEvent e){ pressed=false; } public void mousePressed(MouseEvent e){ for(int i=0;i<nodes.size();i++){ if(nodes.get(i).contains(e.getX(),e.getY())){ mousex=e.getX(); mousey=e.getY(); curindex=i; pressed=true; break; } } } public void mouseClicked(MouseEvent e){ if(e.getClickCount()==2){ rect1=-1; rect2=-1; for(int i=0;i<nodes.size();i++){ if(nodes.get(i).contains(e.getX(),e.getY())){ nodes.remove(i); counter--; curindex=0; for(int j=0;j<parent.size();j++){ if(parent.get(j)==i){ parent.remove(j); children.remove(j); counterp--; } } for(int j=0;j<children.size();j++){ if(children.get(j)==i){ children.remove(j); parent.remove(j); counterp--; } } repaint(); break; } } } if(e.getClickCount()==1){ for(int i=0;i<nodes.size();i++){ if(nodes.get(i).contains(e.getX(),e.getY())){ if(rect1==-1){ rect1=i; break; } if(rect1!=-1){ rect2=i; if(rect2!=rect1){ parent.add(counterp,rect1); children.add(counterp,rect2); counterp++; rect1=-1; rect2=-1; repaint(); break; } if(rect2==rect1){ rect1=-1; rect2=-1; break; } } } } } } } public static void main(String[] args){ TreeDiagramMaker ap=new TreeDiagramMaker(); } } |
Monday, February 29, 2016
Tree Diagram Maker
The main purpose of this short program is to demonstrate mouse events: mouse clicks, mouse presses, mouse drags, and mouse releases. Utilizing mouseDragged method, for instance, the nodes can be dragged around a panel. Double clicking a node will erase it. To connect two nodes, click both of them one by one.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment