To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/2/src/antgraph/Edge.java. This is a spam-protection measure; sorry for the inconvenience.

· Edge.java ·

   1/*
   2 * Edge.java
   3 *
   4 * Created on 03 March 2007, 19:33
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package antgraph;
  11
  12import java.awt.Color;
  13import java.awt.Graphics;
  14import java.awt.Graphics2D;
  15import java.awt.Polygon;
  16import java.awt.RenderingHints;
  17
  18/**
  19 *
  20 * @author James
  21 */
  22public class Edge extends Polygon {//Line2D.Double {
  23    
  24    private Node a, b;
  25    
  26    private Pheromone pheromone;
  27    private boolean selected = false;
  28    
  29    public static final int THICKNESS = 7;
  30    
  31    /** Creates a new instance of Edge */
  32    public Edge(Node a, Node b) {
  33        this.setLine(a.getCenterX(), a.getCenterY(), b.getCenterX(), b.getCenterY());
  34        this.a = a;
  35        this.b = b;
  36    }
  37    
  38    public Node a() {
  39        return a;
  40    }
  41    
  42    public Node b() {
  43        return b;
  44    }
  45    
  46    public boolean from(Node n) {
  47        return n.equals(a);
  48    }
  49    
  50    public Node other(Node n) {
  51        return n.equals(a) ? b : a;
  52    }
  53    
  54    public int hashCode() {
  55        return a().hashCode() + b().hashCode();
  56    }
  57    
  58    public boolean equals(Object o) {
  59        return (o instanceof Edge &&
  60                ((Edge)o).a().equals(a()) &&
  61                ((Edge)o).b().equals(b()));
  62    }
  63
  64    public String toString() {
  65        return a.toString() + " <-> " + b.toString();
  66    }
  67    
  68    public Pheromone getPheromone() {
  69        return pheromone;
  70    }
  71    
  72    public Color getColor() {
  73        if(isSelected())
  74            return Color.GREEN;
  75        else
  76            return Color.BLACK;
  77    }
  78
  79    public void draw(Graphics g) {
  80
  81        Graphics2D g2d = (Graphics2D)g;
  82        g2d.setRenderingHint
  83          (RenderingHints.KEY_ANTIALIASING, 
  84            RenderingHints.VALUE_ANTIALIAS_ON);
  85        
  86        Color old = g2d.getColor();
  87        
  88        g2d.setColor( getColor());
  89
  90        this.setLine(a.getCenterX(), a.getCenterY(), b.getCenterX(), b.getCenterY());
  91     
  92        g2d.fill(this);
  93
  94        g2d.setColor(old);
  95    }    
  96    
  97    public boolean isSelected() {
  98        return selected;
  99    }
 100
 101    public void setSelected(boolean selected) {
 102        this.selected = selected;
 103    }    
 104    
 105    public void setLine(double x1, double y1, double x2, double y2) {
 106        setLine((int)x1, (int)y1, (int)x2, (int)y2);
 107    }
 108    
 109    public void setLine(int x1, int y1, int x2, int y2) {
 110        setLine(x1, y1, x2, y2, THICKNESS);
 111    }
 112    
 113    public void setLine(int x1, int y1, int x2, int y2, int thickness) {
 114        //http://www.rgagnon.com/javadetails/java-0260.html
 115        // The thick line is in fact a filled polygon
 116        
 117        int dX = x2 - x1;
 118        int dY = y2 - y1;
 119        // line length
 120        double lineLength = Math.sqrt(dX * dX + dY * dY);
 121        
 122        double scale = (double)(thickness) / (2 * lineLength);
 123
 124        // The x,y increments from an endpoint needed to create a rectangle...
 125        double ddx = -scale * (double)dY;
 126        double ddy = scale * (double)dX;
 127        ddx += (ddx > 0) ? 0.5 : -0.5;
 128        ddy += (ddy > 0) ? 0.5 : -0.5;
 129        int dx = (int)ddx;
 130        int dy = (int)ddy;
 131        
 132        // Now we can compute the corner points...
 133        
 134        super.reset();
 135        super.addPoint(x1 + dx, y1 + dy);
 136        super.addPoint(x1 - dx, y1 - dy);
 137        super.addPoint(x2 - dx, y2 - dy);
 138        super.addPoint(x2 + dx, y2 + dy);
 139        
 140    }  
 141}
 142

· Edge.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.