📄 nest.java
字号:
package jwo.jpss.ants; // Part of the ant simulation package.
import java.awt.*; // For drawing the nest.
import jwo.jpss.spatial.*; // For spatial footprint.
import java.util.*; // For dynamic collections.
// *******************************************************
/** Class for defining an ants' nest.
* @author Jo Wood
* @version 1.2, 23rd August, 2001.
*/
// *******************************************************
public class Nest extends Animal implements Dynamic,Drawable
{
// ------------------ Object variables ------------------
private Vector ants; // A colony of ants.
private Rectangle bounds; // Boundary of nest.
// Component drawing the graphics.
private GraphicsListener gListener;
private Ant donorAnt; // Last ant to donate food to the nest.
private float queenProb; // Probability of producing a queen.
// Declare immutable nest 'laws'
private static final int[][] antColours = { {0,0,0}, {150,0,0}, // black, red,
{0,0,150}, {100,150,0}, // blue, green
{150,0,100}, {150,100,0}, // purple, brown
{150,100,150},{100,100,100}}; // mauve, grey
private static final int WIDTH = 20;
private static final int HEIGHT = 20;
private static final int INITIAL_FOOD = 20000;
private static final int METABOLIC_RATE = 10; // Rate at which nest consumes food.
private static final int QUEEN_PROD_LEVEL = 8000; // Food required before queen produced.
private static final int QUEEN_FOOD_MAX = 4000; // Maximum initial queen food level.
private static final int ANT_PROD_LEVEL = 4000; // Food required before ant produced.
private static final int ANT_FOOD_MAX = 1000; // Maximum initial ant food level.
// -------------------- Constructors ---------------------
/** Creates a nest with a given spatial footprint.
* @param x location of the nest.
* @param y location of the nest.
*/
public Nest(float x, float y)
{
super(INITIAL_FOOD, new Footprint(x,y,WIDTH, HEIGHT));
// Store drawable size of nest.
bounds = new Rectangle(Math.round(x),Math.round(y),WIDTH,HEIGHT);
// Give each new ant a random number of food units and place it
// at the location of the nest.
ants = new Vector(20);
queenProb = 0.01f;
int[] antRGB = antColours[(int)(Math.random()*antColours.length)];
Color antColour = new Color(antRGB[0],antRGB[1],antRGB[2]);
// Produce initial gene pool of ants.
while (getFoodLevel() >= ANT_PROD_LEVEL)
{
int antFood = (int)(ANT_FOOD_MAX*Math.random());
metabolise(antFood);
Ant newAnt = new Ant(antFood,
getBounds().getXOrigin(),
getBounds().getYOrigin(),
antColour,this);
ants.addElement(newAnt);
donorAnt = newAnt;
}
}
/** Creates a nest full of ants inherited from a given queen.
* @param queen Queen ant setting up the new nest.
*/
public Nest(Queen queen)
{
// Queen gives half her food to the nest.
super(queen.getFoodLevel()/2,
new Footprint(queen.getBounds().getXOrigin(),
queen.getBounds().getYOrigin(),
WIDTH, HEIGHT));
Footprint fp = getBounds();
bounds = new Rectangle(Math.round(fp.getXOrigin()),
Math.round(fp.getYOrigin()),
Math.round(fp.getMERWidth()),
Math.round(fp.getMERHeight()));
queenProb = 0.01f;
ants = new Vector(20);
donorAnt = new Ant(1, queen, fp.getXOrigin(),fp.getYOrigin(), this);
}
// ------------------------ Methods -----------------------
/** Adds the given amount of food to the nest.
* @param donorAnt Ant donating the food to the nest.
* @param foodUnits Amount of food to add to nest.
*/
public void addFood(Ant donorAnt, int foodUnits)
{
eat(foodUnits);
this.donorAnt = donorAnt;
}
// ------------------ Implemented Methods -----------------
/** Draws the nest.
* @param g Graphics context in which to draw the nest.
*/
public void paint(Graphics g)
{
// Give nest a colour if it is alive.
if (isAlive())
{
g.setColor(new Color(155,155,100));
g.fillOval(bounds.x, bounds.y, bounds.width, bounds.height);
}
g.setColor(Color.black);
g.drawOval(bounds.x, bounds.y, bounds.width, bounds.height);
// Draw ants associated with this nest.
Enumeration enum = ants.elements();
while (enum.hasMoreElements())
{
Ant ant = (Ant)enum.nextElement();
ant.paint(g);
}
}
/** Adds a graphics listener to this object. Allows graphics to be
* drawn by a GraphicsListener.
* @param gListener Component doing the drawing.
*/
public void addGraphicsListener(GraphicsListener gListener)
{
this.gListener = gListener;
// Add a graphics listener to each ant.
Enumeration enum = ants.elements();
while (enum.hasMoreElements())
{
Ant ant = (Ant)enum.nextElement();
ant.addGraphicsListener(gListener);
}
}
/** Reports the list of SpatialObjects found at the given spatial
* model (within, matching, overlapping or containing).
* @param spatialModel Spatial model within which to search.
*/
public Vector objectsAt(SpatialModel spatialModel)
{
Vector spatialObjects = new Vector();
// Don't consider ants yet as this will slow down searching process.
// Ant searching code here.
// Add nest to the end of the list if not separate.
if (compare(spatialModel) != SEPARATE)
spatialObjects.addElement(this);
return spatialObjects;
}
/** Let the nest evolve for one time unit.
*/
public void evolve()
{
// Nest metabolises food.
metabolise(METABOLIC_RATE);
// See if nest is ready to produce a new queen.
if ((getFoodLevel() >= QUEEN_PROD_LEVEL) && (Math.random() <queenProb))
{
int queenFood = (int)(QUEEN_FOOD_MAX*Math.random());
Footprint fp = getBounds();
metabolise(queenFood);
Queen queen = new Queen(queenFood, donorAnt,
fp.getXOrigin()+fp.getMERWidth()/2,
fp.getYOrigin()+fp.getMERHeight()/2,
this);
queen.addGraphicsListener(gListener);
ants.addElement(queen);
}
// See if the nest is ready to produce a new ant.
if (getFoodLevel() >= ANT_PROD_LEVEL)
{
int antFood = (int)(ANT_FOOD_MAX*Math.random());
Footprint fp = getBounds();
metabolise(antFood);
Ant newAnt = new Ant(antFood, donorAnt,
fp.getXOrigin()+fp.getMERWidth()/2,
fp.getYOrigin()+fp.getMERHeight()/2,
this);
newAnt.addGraphicsListener(gListener);
ants.addElement(newAnt);
}
// Let each ant in the nest go about its business.
Enumeration enum = ants.elements();
if (enum.hasMoreElements())
{
do
{
Ant ant = (Ant)enum.nextElement();
ant.evolve();
// Remove any dead ants from the colony.
if (!(ant.isAlive()))
ants.removeElement(ant);
}
while (enum.hasMoreElements());
gListener.redrawGraphics();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -