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

· Node.java ends ·

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