To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/src/antgraph/gui/GraphCanvas.java. This is a spam-protection measure; sorry for the inconvenience.
· GraphCanvas.java ·
1/* 2 * GraphView.java 3 * 4 * Created on 04 March 2007, 13:21 5 * 6 * the graph is drawn on this canvas. also listens for mouse events. the canvas is printable 7 */ 8 9package antgraph.gui; 10 11import antgraph.Ant; 12import antgraph.Edge; 13import antgraph.Graph; 14import antgraph.GraphComponent; 15import antgraph.NoSuchNodeException; 16import antgraph.Node; 17import java.awt.Canvas; 18import java.awt.Color; 19import java.awt.Graphics; 20import java.awt.Graphics2D; 21import java.awt.RenderingHints; 22import java.awt.event.MouseAdapter; 23import java.awt.event.MouseEvent; 24import java.awt.event.MouseMotionAdapter; 25import java.awt.image.BufferedImage; 26import java.awt.print.PageFormat; 27import java.awt.print.Printable; 28import java.awt.print.PrinterException; 29import java.util.ArrayList; 30import java.util.Collections; 31import java.util.List; 32 33/** 34 * 35 * @author James 36 */ 37public class GraphCanvas extends Canvas implements Runnable, Printable { 38 39 private BufferedImage bi; 40 private Graphics big; 41 private Thread animate; 42 43 public static final int WIDTH = 500, HEIGHT = 500; 44 private Graph graph; 45 private Node selectedNode = null; 46 private Edge selectedEdge = null; 47 48 private List<GraphComponent> selectedComponents = new ArrayList<GraphComponent>(); 49 private int c = 0; 50 private boolean dragging = false; 51 52 public GraphCanvas(Graph g) { 53 setSize(WIDTH, HEIGHT); 54 this.graph = g; 55 56 //mouse listener 57 58 this.addMouseListener( new MouseAdapter() { 59 public void mousePressed(MouseEvent e) { 60 61 if(e.getButton() == e.BUTTON1) { 62 if(containsNode(e.getX(), e.getY())) { 63 if(nodeSelected()) { 64 Node a = getSelectedNode(); 65 Node b = nodeAt(e.getX(), e.getY()); 66 67 try { 68 graph.addEdge(a, b); 69 deselectAll(); 70 }catch (NoSuchNodeException ex) { 71 System.out.println(ex); //shouldn't happen 72 } 73 }else{ 74 deselectAll(); 75 76 setSelected(nodeAt(e.getX(), e.getY())); 77 78 List<Edge> edges = GraphJFrame.getGraph().getEdges(getSelectedNode()); 79 80 Collections.sort(edges); 81 System.out.println(edges); 82 } 83 }else if(containsEdge(e.getX(), e.getY())) { 84 85 deselectAll(); 86 87 setSelected(edgeAt(e.getX(), e.getY())); 88 }else{ 89 90 if(nodeSelected()) { 91 92 graph.addNode(e.getX(), e.getY()); 93 System.out.println(nodeAt(e.getX(), e.getY())); 94 try { 95 graph.addEdge(getSelectedNode(), nodeAt(e.getX(), e.getY())); 96 }catch (NoSuchNodeException ex) { 97 System.out.println(ex); //shouldn't happen 98 } 99 100 deselectAll(); 101 }else{ 102 103 graph.addNode(e.getX(), e.getY()); 104 } 105 } 106 }else if(e.getButton() == e.BUTTON3) { 107 if(containsNode(e.getX(), e.getY())) { 108 deselectAll(); 109 110 graph.removeNode(nodeAt(e.getX(), e.getY())); 111 }else if(containsEdge(e.getX(), e.getY())) { 112 deselectAll(); 113 114 graph.removeEdge(edgeAt(e.getX(), e.getY())); 115 }else{ 116 117 118 //graph.highlight(graph.getNodes()); 119 } 120 } 121 122 123 } 124 125 126 public void mouseReleased(MouseEvent e) { 127 if(dragging) { 128 deselectAll(); 129 dragging = false; 130 } 131 } 132 133 }); 134 135 136 addMouseMotionListener( new MouseMotionAdapter() { 137 public void mouseDragged(MouseEvent e) { 138 139 if(componentSelected()) { 140 dragging = true; 141 if(nodeSelected()) { 142 143 getSelectedNode().setLocation(e.getX(), e.getY()); 144 145 } 146 } 147 } 148 149 public void mouseMoved(MouseEvent e) { 150 dehighlight(); 151 152 GraphComponent gc =componentAt(e.getX(), e.getY()); 153 if(!dragging && gc != null && !gc.isSelected()) getGraph().highlight(gc);//gc.highlight(true); 154 155 156 } 157 }); 158 159 //buffered image for double buffering 160 bi = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB); 161 big = getBi().createGraphics(); 162 animate = null; 163 animate = new Thread(this); 164 animate.start(); 165 } 166 167 public Graph getGraph() { 168 return graph; 169 } 170 171 public void setGraph(Graph graph) { 172 173 if(this.graph != null) { 174 if(this.graph.getNest() != null) { 175 for(Ant ant : this.graph.getNest().getAnts()) { 176 177 ant.stop(); 178 } 179 for(Edge e : this.graph.getEdges()) { 180 e.getPheromone().stop(); 181 } 182 183 } 184 } 185 this.graph = graph; 186 } 187 188 public void update(Graphics g) { 189 clear(); 190 big.setColor(Color.BLACK); 191 if(graph.getNodes().size() > 0) { 192 193 if(graph.getEdges().size() > 0) { 194 195 for(Edge e : graph.getEdges()) { 196 197 e.draw(big); 198 199 } 200 } 201 202 for(Node n : graph.getNodes()) { 203 n.draw(big); 204 } 205 206 if(graph.getNest().countFood() == GraphJFrame.FOOD) { 207 208 } 209 210 } 211 g.drawImage(getBi(), 0, 0, this); 212 } 213 214 public void paint(Graphics g) { 215 update(g); 216 } 217 218 219 public void clear() { 220 big.setColor(Color.WHITE); 221 big.fillRect(0,0,getWidth(),getHeight()); 222 } 223 224 public void dehighlight() { 225 for(GraphComponent gc : getGraph().getComponents()) 226 gc.highlight(false); 227 } 228 229 public void addSelected(GraphComponent gc) { 230 selectedComponents.add(gc); 231 gc.setSelected(true); 232 } 233 234 public void setSelected(GraphComponent gc) { 235 deselectAll(); 236 addSelected(gc); 237 } 238 239 public boolean componentsSelected() { 240 return selectedComponents.size() > 0; 241 } 242 243 public boolean componentSelected() { 244 return selectedComponents.size() == 1; 245 } 246 247 public int countSelectedComponents() { 248 return selectedComponents.size(); 249 } 250 251 public List<GraphComponent> getSelectedComponents() { 252 return selectedComponents; 253 } 254 255 public GraphComponent getSelectedComponent() { 256 return componentsSelected() ? selectedComponents.get(0) : null; 257 } 258 259 public void deselectComponent(GraphComponent gc) { 260 selectedComponents.remove(gc); 261 gc.setSelected(false); 262 } 263 264 public void run() { 265 //canvas is redrawn every 200 milliseconds. 266 while(animate!=null) { 267 268 try { 269 animate.sleep(200); 270 271 repaint(); 272 }catch (Exception e) { 273 System.out.println(e); 274 } 275 276 } 277 big.dispose(); 278 } 279 280 public boolean containsNode(int x, int y) { 281 for(Node n : graph.getNodes()) 282 if(n.contains(x, y)) 283 return true; 284 return false; 285 } 286 287 public Node nodeAt(int x, int y) { 288 for(Node n : graph.getNodes()) 289 if(n.contains(x, y)) 290 return n; 291 return null; 292 } 293 294 public boolean containsEdge(int x, int y) { 295 for(Edge e : graph.getEdges()) 296 if(e.contains(x, y)) 297 return true; 298 return false; 299 } 300 301 public Edge edgeAt(int x, int y) { 302 for(Edge e : graph.getEdges()) 303 if(e.contains(x, y)) 304 return e; 305 return null; 306 } 307 308 public GraphComponent componentAt(int x, int y) { 309 for(GraphComponent gc : getGraph().getComponents()) 310 if(gc.contains(x, y)) 311 return gc; 312 return null; 313 } 314 315 public void setSelectedNode(Node n) { 316 317 setSelected(n); 318 n.setSelected(true); 319 320 } 321 322 public Node getSelectedNode() { 323 return nodeSelected() ? (Node)getSelectedComponent() : null; 324 } 325 326 327 public boolean nodeSelected() { 328 return getSelectedComponent() != null ? getSelectedComponent() instanceof Node : false; 329 } 330 331 public void setSelectedEdge(Edge e) { 332 setSelected(e); 333 e.setSelected(true); 334 335 } 336 public Edge getSelectedEdge() { 337 return edgeSelected() ? (Edge)getSelectedComponent() : null; 338 } 339 340 public void deselectAll() { 341 for(GraphComponent gc : getSelectedComponents()) 342 gc.setSelected(false); 343 getSelectedComponents().clear(); 344 } 345 346 public boolean edgeSelected() { 347 return getSelectedComponent() != null ? getSelectedComponent() instanceof Edge : false; 348 } 349 350 351 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { 352 /* printing */ 353 if (pageIndex == 0) { 354 Graphics2D g2d = (Graphics2D) graphics; 355 356 g2d.setRenderingHint 357 (RenderingHints.KEY_ANTIALIASING, 358 RenderingHints.VALUE_ANTIALIAS_ON); 359 360 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 361 paint(graphics); 362 363 int x = (int)pageFormat.getImageableX(); 364 int y = (int)pageFormat.getHeight() / 2; 365 int h = (int)g2d.getFontMetrics().getHeight(); 366 367 g2d.drawString("Adjacency List", x, y); 368 y += h + 3; 369 g2d.drawString("----------------------", x, y); 370 y += h + 3; 371 for(Node n : getGraph().getNodes()) { 372 373 g2d.drawString(n + ": " + getGraph().getAdjacencyList(n), x, y); 374 375 y += h + 3; 376 377 } 378 379 380 return PAGE_EXISTS; 381 }else{ 382 return NO_SUCH_PAGE; 383 } 384 } 385 386 public BufferedImage getBi() { 387 return bi; 388 } 389} 390
· GraphCanvas.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/src/antgraph/gui/GraphCanvas.java. This is a spam-protection measure; sorry for the inconvenience.