📄 antapp.java
字号:
/* * This code is from the book: * * Winder, R and Roberts, G (2000) Developing Java * Software, second edition, John Wiley & Sons. * * It is copyright (c) 2000 Russel Winder and Graham Roberts. */import java.awt.* ;import javax.swing.* ;import java.awt.event.* ;import SimFrameWork.* ;/** * This class provides the application window that an ant * simulation grid is displayed in. It also provides the static * main method and declares variables to specify the size * of the grid being used. * * @version 2.0 March 1999 * @author Graham Roberts */public class AntApp extends JFrame { /** * Construct and layout the contents of the display window. * The main area of the window displays a graphical representation * of the ant world. A status bar appears at the bottom of the * window. */ public AntApp() { super("Ant Foraging Simulation") ; try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { getContentPane().setLayout(new BorderLayout()) ; displayPanel = new JPanel() ; // Force the display panel to be the correct size to display // the simulation grid. displayPanel.setPreferredSize( new Dimension(WIDTH*BLOCKSIZE,HEIGHT*BLOCKSIZE)) ; getContentPane().add("Center",displayPanel) ; // Add a status panel at the bottom of the window JPanel statusPanel = new JPanel(new BorderLayout()) ; statusPanel.setBorder(BorderFactory.createLoweredBevelBorder()) ; getContentPane().add("South",statusPanel) ; statusLabel = new JLabel() ; statusPanel.add(statusLabel) ; // Add a listener for the window being closed. // This just exits the application. addWindowListener(new WindowAdapter () { public void windowClosing(WindowEvent e) { stopSimulation() ; System.exit(0) ; } }) ; pack() ; setVisible(true) ; } /** * Create a new ant world and start the simulation. The simulation * itself is run as a separate thread, as is the routine to update * the status bar at the bottom of the window. */ public void runSimulation() { AntDisplay display = new AntDisplay(WIDTH,HEIGHT,BLOCKSIZE,displayPanel) ; AntWorld world = new AntWorld(WIDTH,HEIGHT,display) ; control = new MainLoop(world) ; control.start() ; status = new AntStatus(world,statusLabel) ; status.start() ; } /** * Stop the simulation thread. */ public void stopSimulation() { status.stopControl() ; control.stopControl() ; } /** * Main method to start the application. */ public static void main(String[] args) { AntApp app = new AntApp() ; app.runSimulation() ; } /** * Thread object used to run the simulation. * */ private MainLoop control ; /** * Thread object used for the status thread. * */ private AntStatus status ; /** * Panel used to display the simulation grid. */ private JPanel displayPanel ; /** * Label used to display status information. */ private JLabel statusLabel ; /** * Constants setting the simulation grid size in patches. */ private final int WIDTH = 75 ; private final int HEIGHT = 75 ; /** * Constant setting display block size in pixels. */ private final int BLOCKSIZE = 8 ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -