To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/1/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.Rectangle;
  16import java.util.ArrayList;
  17import java.util.List;
  18
  19/**
  20 *
  21 * @author James
  22 */
  23public class Node extends Rectangle {
  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    private boolean selected = false;
  30    
  31    public Node(String name) {
  32        this.setSize(20, 20);
  33        this.setName(name);
  34    }
  35    
  36    public Node(Object o) {
  37        this(o.toString());
  38    }
  39
  40    public void addAnt(Ant ant) {
  41        ants.add(ant);
  42    }
  43    
  44    public int countAnts() {
  45        return ants.size();
  46    }
  47    
  48    public void removeAnt(Ant ant) {
  49        ants.remove(ant);
  50    }
  51    
  52    public void addFood() {
  53        food.add(new Food());
  54    }
  55    
  56    public void addFood(int n) {
  57        for(int i = 0; i < n; i++)
  58            addFood();
  59    }
  60    
  61    public void removeFood(Ant ant) {
  62        Food f = food.get(0);
  63        food.remove(0);
  64        ant.setFood(f);
  65    }
  66    
  67    public int countFood() {
  68        return food.size();
  69    }
  70    
  71    public void setNest(Nest n) {
  72        nest = n;
  73    }
  74    
  75    public Nest getNest() {
  76        return nest;
  77    }
  78    
  79    public boolean isNest() {
  80        return getNest() != null;
  81    }
  82    
  83    public boolean containsFood() {
  84        return countFood() != 0;
  85    }
  86    
  87    public boolean containsAnts() {
  88        return countAnts() != 0;
  89    }
  90    
  91    public boolean empty() {
  92        return !(containsFood() && containsAnts());
  93    }
  94    
  95    public String toString() {
  96        return getName();
  97    }
  98
  99    public int hashCode() {
 100        return toString().hashCode();
 101    }
 102
 103    public boolean equals(Object o) {
 104        return o instanceof Node &&
 105                ((Node)o).getName().equals(getName());
 106    }
 107    
 108    public String getName() {
 109        return name;
 110    }
 111
 112    private void setName(String name) {
 113        this.name = name;
 114    }
 115
 116    public List<Node> getAdjacencyList() {
 117        return Main.getGraph().getAdjacencyList(this);
 118    }
 119    
 120    public Color getColor() {
 121        if(isSelected())
 122            return Color.GREEN;
 123        else
 124            return Color.BLACK;
 125    }
 126
 127    public void draw(Graphics g) {
 128        
 129        
 130        
 131        Graphics2D g2d = (Graphics2D)g;
 132        Color old = g2d.getColor();
 133        
 134        g2d.setColor( getColor());
 135  
 136        g2d.fillOval((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());
 137
 138        g2d.setColor(old);
 139    }    
 140
 141    public boolean isSelected() {
 142        return selected;
 143    }
 144
 145    public void setSelected(boolean selected) {
 146        this.selected = selected;
 147    }
 148
 149    public void setLocation(int x, int y) {
 150
 151        super.setLocation((int)(x - getWidth() / 2), (int)(y - getHeight() / 2));
 152    }
 153}
 154

· Node.java ends ·

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