To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/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 antgraph.Pheromone.Direction; 13import java.awt.Color; 14import java.awt.Graphics; 15import java.awt.Graphics2D; 16import java.awt.RenderingHints; 17import java.util.Comparator; 18 19/** 20 * 21 * @author James 22 */ 23public class Edge extends GraphComponent implements Comparable<Edge> { 24 25 private Node a, b; 26 private Pheromone pheromone = new Pheromone(); 27 public static final int THICKNESS = 7; 28 private double weight = 0; 29 private int centerX, centerY; 30 private boolean showWeights = false; 31 32 /** Creates a new instance of Edge */ 33 public Edge(Node a, Node b) { 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() + ", S: " + this.getPheromone().toString(); 66 } 67 68 public Pheromone getPheromone() { 69 return pheromone; 70 } 71 72 public Pheromone getPheromone(Direction direction) { 73 return pheromone.getPheromone(direction); 74 } 75 76 public void update() { 77 this.setLine(a.getCenterX(), a.getCenterY(), b.getCenterX(), b.getCenterY()); 78 79 } 80 81 public void setLine(double x1, double y1, double x2, double y2) { 82 setLine((int)x1, (int)y1, (int)x2, (int)y2); 83 } 84 85 public void setLine(int x1, int y1, int x2, int y2) { 86 setLine(x1, y1, x2, y2, THICKNESS); 87 } 88 89 public void setLine(int x1, int y1, int x2, int y2, int thickness) { 90 //http://www.rgagnon.com/javadetails/java-0260.html 91 // The thick line is in fact a filled polygon 92 93 int dX = x2 - x1; 94 int dY = y2 - y1; 95 // line length 96 double lineLength = Math.sqrt(dX * dX + dY * dY); 97 98 double scale = (double)(thickness) / (2 * lineLength); 99 100 // The x,y increments from an endpoint needed to create a rectangle... 101 double ddx = -scale * (double)dY; 102 double ddy = scale * (double)dX; 103 ddx += (ddx > 0) ? 0.5 : -0.5; 104 ddy += (ddy > 0) ? 0.5 : -0.5; 105 int dx = (int)ddx; 106 int dy = (int)ddy; 107 108 // Now we can compute the corner points... 109 110 super.reset(); 111 super.addPoint(x1 + dx, y1 + dy); 112 super.addPoint(x1 - dx, y1 - dy); 113 super.addPoint(x2 - dx, y2 - dy); 114 super.addPoint(x2 + dx, y2 + dy); 115 116 setWeight(Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))); 117 centerX = (x1 + (int)getWeight() / 2); 118 centerY = (y1 + (y2 - y1) / 2); 119 } 120 121 public void draw(Graphics g) { 122 super.draw(g); 123 if( showWeights()) { 124 Graphics2D g2d = (Graphics2D)g; 125 g2d.setRenderingHint 126 (RenderingHints.KEY_ANTIALIASING, 127 RenderingHints.VALUE_ANTIALIAS_ON); 128 129 Color old = g2d.getColor(); 130 131 g2d.setColor(Color.BLACK); 132 133 g2d.drawString(Integer.toString(getPheromone().getStrength()), (int)getCenterX() - 3, (int)getCenterY() - 10); 134 135 g2d.setColor(old); 136 } 137 } 138 139 public int getCenterX() { 140 return centerX; 141 } 142 143 public int getCenterY() { 144 return centerY; 145 } 146 147 public double getWeight() { 148 return 1.0;//weight; 149 } 150 151 private void setWeight(double weight) { 152 this.weight = Math.round(weight); 153 } 154 155 public int compareTo(Edge otherEdge) { 156 double thisStrength = this.getPheromone() == null ? -1.00 : this.getPheromone().getStrength(); 157 double otherStrength = otherEdge.getPheromone() == null ? -1.00 : otherEdge.getPheromone().getStrength(); 158 return (int) ( otherStrength - thisStrength); 159 } 160 161 public static Comparator<Edge> TowardNestComparator = new Comparator<Edge>() { 162 public int compare(Edge e2, Edge e1) { 163 // int pheromoneStrength = gs1.getPheromone() != null ? gs1.getPheromone().getStrength() : 0; 164 return e1.getPheromone().getStrength(Direction.TOWARD_NEST) - e2.getPheromone().getStrength(Direction.TOWARD_NEST); 165 } 166 }; 167 168 public static Comparator<Edge> AwayFromNestComparator = new Comparator<Edge>() { 169 public int compare(Edge e2, Edge e1) { 170 // int pheromoneStrength = gs1.getPheromone() != null ? gs1.getPheromone().getStrength() : 0; 171 return e1.getPheromone().getStrength(Direction.AWAY_FROM_NEST) - e2.getPheromone().getStrength(Direction.AWAY_FROM_NEST); 172 } 173 }; 174 175 public boolean showWeights() { 176 return showWeights; 177 } 178 179 public void showWeights(boolean showWeights) { 180 this.showWeights = showWeights; 181 } 182 183 public void toggleWeights() { 184 this.showWeights = !this.showWeights; 185 } 186}
· Edge.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/src/antgraph/Edge.java. This is a spam-protection measure; sorry for the inconvenience.