To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/src/antgraph/Node.java. This is a spam-protection measure; sorry for the inconvenience.
· Node.java ·
1/* 2 * Node.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.gui.GraphJFrame; 13import java.awt.Color; 14import java.awt.Graphics; 15import java.awt.Graphics2D; 16import java.awt.RenderingHints; 17import java.util.ArrayList; 18import java.util.List; 19 20/** 21 * 22 * @author James 23 */ 24public class Node extends GraphComponent { 25 26 private String name; 27 private List<Ant> ants = new ArrayList<Ant>(); 28 private List<Food> food = new ArrayList<Food>(); 29 30 31 private int x, y, radius; 32 33 public Node(String name) { 34 radius = 20; 35 36 this.setName(name); 37 } 38 39 public Node(String name, int x, int y) { 40 this(name); 41 this.x = x; 42 this.y = y; 43 update(); 44 } 45 46 public Node(Object o, int x, int y) { 47 this(o.toString(), x, y); 48 } 49 50 public Node(Object o) { 51 this(o.toString()); 52 } 53 54 public void addAnt(Ant ant) { 55 getAnts().add(ant); 56 } 57 58 public int countAnts() { 59 return getAnts().size(); 60 } 61 62 public void removeAnt(Ant ant) { 63 if(getAnts().size() > 0) getAnts().remove(ant); 64 } 65 66 public void addFood() { 67 food.add(new Food()); 68 } 69 70 public void addFood(int n) { 71 for(int i = 0; i < n; i++) 72 addFood(); 73 } 74 75 public void addFood(Food f) { 76 if(f != null) 77 food.add(f); 78 } 79 80 public Food removeFood() { 81 Food f = food.get(0); 82 food.remove(0); 83 return f; 84 } 85 86 public int countFood() { 87 return food.size(); 88 } 89 90 public Nest getNest() { 91 if(isNest()) return (Nest)this; 92 else return null; 93 } 94 95 public boolean isNest() { 96 return this instanceof Nest; 97 } 98 99 public boolean containsFood() { 100 return countFood() != 0; 101 } 102 103 public boolean containsAnts() { 104 return countAnts() != 0; 105 } 106 107 public boolean empty() { 108 return !(containsFood() && containsAnts()); 109 } 110 111 public String toString() { 112 return getName(); 113 } 114 115 public int hashCode() { 116 return toString().hashCode(); 117 } 118 119 public boolean equals(Object o) { 120 return o instanceof Node && 121 ((Node)o).getName().equals(getName()); 122 } 123 124 public String getName() { 125 return name; 126 } 127 128 private void setName(String name) { 129 this.name = name; 130 } 131 132 public List<Node> getAdjacencyList() { 133 return GraphJFrame.getGraph().getAdjacencyList(this); 134 } 135 136 137 public void draw(Graphics g) { 138 super.draw(g); 139 Graphics2D g2d = (Graphics2D)g; 140 g2d.setRenderingHint 141 (RenderingHints.KEY_ANTIALIASING, 142 RenderingHints.VALUE_ANTIALIAS_ON); 143 144 Color old = g2d.getColor(); 145 146 g2d.setColor(Color.WHITE); 147 148 g2d.drawString(toString(), (int)getCenterX() - 3, (int)getCenterY() + 3); 149 150 g2d.setColor(Color.RED); 151 152 g2d.drawString(getAnts().toString(), (int)getCenterX() + getRadius() / 2 + 5, (int)getCenterY() + getRadius() / 2 + 5); 153 154 g2d.setColor(old); 155 } 156 157 158 public void update() { 159 this.reset(); 160 for(double t = 0; t <= (2 * Math.PI); t += 2 * Math.PI / 50) 161 addPoint((int) (getX()+getRadius()/2 * Math.cos(t)),(int)(getY()+getRadius()/2 * Math.sin(t))); 162 } 163 164 165 public void setLocation(int x, int y) { 166 this.x = x; 167 this.y = y; 168 } 169 170 public int getRadius() { return radius; } 171 public int getX() { return x; } 172 public int getY() { return y; } 173 174 public int getCenterX() { 175 return getX(); 176 } 177 178 public int getCenterY() { 179 return getY(); 180 } 181 182 public List<Ant> getAnts() { 183 return ants; 184 } 185 186 public Color getColor() { 187 Color retValue; 188 189 if(isNest()) { 190 retValue = Color.GRAY; 191 }else if(containsFood()) { 192 retValue = Color.ORANGE; 193 }else{ 194 retValue = super.getColor(); 195 } 196 return retValue; 197 } 198} 199
· Node.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/src/antgraph/Node.java. This is a spam-protection measure; sorry for the inconvenience.