To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/16.0/src/ants/Nest.java. This is a spam-protection measure; sorry for the inconvenience.
· Nest.java ·
1/* 2 * Nest.java 3 * 4 * Created on 13 October 2006, 22:47 5 * 6 * To change this template, choose Tools | Template Manager 7 * and open the template in the editor. 8 */ 9 10package ants; 11 12import ants.control.AntControl; 13import ants.event.EnterNestAntEvent; 14import ants.event.ExitNestAntEvent; 15import ants.event.NewAntEvent; 16import java.awt.Color; 17import java.util.Vector; 18 19/** 20 * 21 * @author James Hamilton 22 */ 23public class Nest extends MyObject implements Runnable { 24 25 private Vector<Ant> ants = new Vector<Ant>(); 26 private Vector<Food> food = new Vector<Food>(); 27 28 private int totalAnts = 0; 29 30 private Thread move; 31 32 /** Creates a new instance of Nest */ 33 public Nest() { 34 35 } 36 37 public Nest(GridSquare gs) { 38 setGridSquare(gs); 39 move = new Thread(this); 40 move.setDaemon(true); 41 move.start(); 42 } 43 44 public Nest(int n, GridSquare gs) { 45 this(gs); 46 addAnts(n); 47 48 totalAnts = n; 49 } 50 51 public void addAnt() { 52 Ant ant = new Ant(getGridSquare()); 53 ants.add(ant); 54 ant.addAntListener(AntControl.getInstance()); 55 ant.notifyListeners(new NewAntEvent(ant)); 56 totalAnts++; 57 AntControl.getInstance().setTotalAnts(totalAnts); 58 } 59 60 public void addAnts(int n) { 61 for(int i = 0; i < n; i++) 62 addAnt(); 63 } 64 65 public void releaseAnt() { 66 if(ants.size() != 0) { 67 // System.out.println("Releasing Ant.."); 68 69 Vector<GridSquare> surroundingSquares = getGridSquare().getGridSquares(1).getFreeGridSquares(); 70 71 // int n = (int)(surroundingSquares.size() * Math.random()); 72 73 74 Ant ant = ants.remove(0); 75 // ant.setGridSquare(surroundingSquares.elementAt(n)); 76 // surroundingSquares.elementAt(n).setAnt(ant); 77 ant.start(); 78 ant.notifyListeners(new ExitNestAntEvent(ant)); 79 80 } 81 } 82 83 public void addAnt(Ant ant) { 84 food.add(ant.getFood()); 85 ant.setFood(null); 86 ants.add(ant); 87 System.out.println("ANT Returned to NEST"); 88 ant.stop(); 89 ant.notifyListeners(new EnterNestAntEvent(ant)); 90 } 91 92 public void run() { 93 94 while(move != null) { 95 try { 96 97 releaseAnt(); 98 // System.out.println(ants.size() + " ants in nest"); 99 move.sleep((int)(10000 * Math.random()) + 1); 100 }catch (InterruptedException e) {} 101 } 102 } 103 104 public Color getColor() { 105 return Color.LIGHT_GRAY; 106 } 107 108 public String toString() { 109 return "Nest"; 110 } 111 112 public int getTotalAnts() { 113 return totalAnts; 114 } 115} 116
· Nest.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/16.0/src/ants/Nest.java. This is a spam-protection measure; sorry for the inconvenience.