📄 antapplet.java
字号:
package jwo.jpss.ants; // Part of the ant simulation package.
import java.applet.Applet; // Required for applet functionality.
import java.awt.*; // For GUI.
import jwo.jpss.spatial.*; // For spatial classes.
import java.util.*; // For Java vector class.
// **************************************************************
/** Applet front end to the Ants in the Garden simulator.
* @author Jo Wood.
* @version 1.2, 26th July, 2001
*/
// **************************************************************
public class AntApplet extends Applet
implements GraphicsListener, Runnable
{
// ------------------- Object variables ---------------------
private Thread evolution; // Evolutionary process.
private Garden garden; // Garden containing ants.
private Image offscreenImage; // Double-buffering objects.
private Graphics offscreenGraphics;
// -------------------- Starter methods ---------------------
/** Initialises the applet within which the ants may be observed.
*/
public void init()
{
evolution = new Thread(this); // Make dynamic evolution a thread.
Dimension appletSize = getSize();
// Create an offscreen Image and garden on which to draw.
offscreenImage = createImage(appletSize.width,appletSize.height);
offscreenGraphics = offscreenImage.getGraphics();
garden = new Garden(new Footprint(0,0,appletSize.width,appletSize.height));
garden.addGraphicsListener(this);
}
/** Starts off the ant simulation or resumes it if window has been deiconified
* after being iconified.
*/
public void start()
{
evolution.start();
}
// ---------------------- Methods ------------------------
/** Allows the simulator to draw in this applet using double buffering.
* @param g Graphics context within which to draw.
*/
public void paint(Graphics g)
{
garden.paint(offscreenGraphics);
g.drawImage(offscreenImage,1,1,null);
}
/** Provides a description of this applet for the embedding browser.
* @return Message describing the applet.
*/
public String getAppletInfo()
{
return "Ants in the Garden - A Simulation by Jo Wood, 2001";
}
// ---------------- Implemented methods -----------------
/** Controls the evolution of the simulation as a threaded
* process.
*/
public void run()
{
garden.startEvolution();
}
/** Stops the ant simulation, either temporarily if the window is minimised,
* or permanently if it is closed.
*/
public void stop()
{
garden.stopEvolution();
showStatus("Ant simulator paused");
super.stop();
}
/** Redraws any graphics that need updating.
*/
public void redrawGraphics()
{
Graphics g = getGraphics();
if (g != null)
paint(g);
}
/** Checks that the given spatial object can be drawn
* in this applet.
* @param spObject Spatial object we wish to draw.
* @return True if the spatial object can be drawn.
*/
public boolean canDraw(SpatialObject spObject)
{
if (garden.compare(spObject) == SpatialModel.ENCLOSES)
return true;
else
return false;
}
/** Reports the list of SpatialObjects assoicated with the given
* spatial object (within, matching, overlapping or containing).
* @param spObject spatial object with which to compare.
* @return List of spatial objects in contact with the given one.
*/
public Vector objectsAt(SpatialObject spObject)
{
return garden.objectsAt(spObject);
}
/** Displays the given message in the applet. Some browsers may
* choose to ignore this request.
* @param message Message to display.
*/
public void displayMessage(String message)
{
showStatus(message);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -