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

· Node.java ends ·

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