📄 foodsource.java
字号:
package jwo.jpss.ants; // Part of the ant simulation package.
import java.awt.*; // For drawing the food.
import jwo.jpss.spatial.*; // For spatial classes.
// *******************************************************
/** Class for defining a food source.
* @author Jo Wood
* @version 1.4, 10th October, 2001.
*/
// *******************************************************
public class FoodSource extends SpatialObject implements Drawable
{
// ------------------ Object variables ------------------
private int foodLevel; // Amount of available food.
private int x,y,width,height; // Drawable bounds.
// ------------------- Constructors ---------------------
/** Creates a food source at a given location and value.
* @param x x location of the food source.
* @param y y location of the food source.
* @param fLevel Amount of available food.
*/
public FoodSource(float x, float y, int fLevel)
{
super(new Footprint(x,y,1 + fLevel/1000,1+fLevel/1000));
setBounds(getBounds());
this.foodLevel = fLevel;
}
// ----------------------- Methods -----------------------
/** Returns the level of food left in the source.
* @return The level of food left in the source.
*/
public int getFoodLevel()
{
return foodLevel;
}
/** Allows a given amount of food to be removed. If there is
* not enough food left, remaining food will be removed.
* @return foodToRemove Amount of food to remove.
* @return Amount of food actually removed from the source.
*/
public int removeFood(int foodToRemove)
{
int foodRemoved;
// Reduce food supply.
if (foodLevel >= foodToRemove)
foodRemoved = foodToRemove;
else
foodRemoved = foodLevel;
// Update size of food source.
foodLevel -= foodRemoved;
Footprint newSize = getBounds();
newSize.setMERWidth(1 + foodLevel/1000);
newSize.setMERHeight(1 + foodLevel/1000);
setBounds(newSize);
return foodRemoved;
}
/** Draws the food source.
* @param g Graphics context in which to draw the food source.
*/
public void paint(Graphics g)
{
g.setColor(new Color(0,155,10));
g.fillRect(x,y,width,height);
g.setColor(Color.black);
g.drawRect(x,y,width,height);
}
/** Sets the bounding rectangle of this object.
* @param bounds New spatial footprint of this food source.
*/
public void setBounds(Footprint bounds)
{
super.setBounds(bounds);
// Store drawable bounds.
x = Math.round(bounds.getXOrigin());
y = Math.round(bounds.getYOrigin());
width = Math.round(bounds.getMERWidth());
height = Math.round(bounds.getMERHeight());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -