📄 antdisplay.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 SimFrameWork.Display ;/** * This class specializes Display to show an ant simulation display, * using AWT/Swing graphics. * * @version 2.0 March 1999 * @author Graham Roberts */class AntDisplay extends Display { /** * Initialize display. For better performance a grid is first * drawn onto an off-screen image. When complete the image * is copied onto the display panel in one operation. * * @param x width of display in patches * @param y height of display in patches * @param size size of patch in pixels * @param p JPanel used to display the graphics image */ public AntDisplay(int w, int h, int size, JPanel p) { super(w,h) ; panel = p ; blocksize = size ; image = p.createImage(w*blocksize,h*blocksize) ; imageGraphics = image.getGraphics() ; } /** * Display patch at x,y with value val. The value of val is * mapped to a colour used to display the patch. * * @param x x position of patch. * @param y y position of patch. * @param val value of patch. */ public void show(int x, int y, int val) { x *= blocksize ; y *= blocksize ; // If val is greater than zero then it represents // the number of wood chips on a patch. // If -1 then an unloaded ant. // If -2 then a loaded ant. if (val > 0) { val = val * 5 ; val = (val > 255) ? 255 : val ; Color clr = new Color(0,val,0) ; imageGraphics.setColor(clr) ; imageGraphics.fillRect(x,y,blocksize,blocksize) ; } else if (val == -1) { imageGraphics.setColor(ANTCOLOR) ; imageGraphics.fillRect(x,y,blocksize,blocksize) ; } else if (val == -2) { imageGraphics.setColor(LOADEDANTCOLOR) ; imageGraphics.fillRect(x,y,blocksize,blocksize) ; } } /** * Clear the display panel and display brown as the background * color. */ public void clear() { imageGraphics.setColor(new Color(128,64,0)) ; imageGraphics.fillRect(0,0,width*blocksize,height*blocksize) ; } /** * Show the updated image by copying it onto the display panel. */ public void showGrid() { panel.getGraphics().drawImage(image,0,0,panel) ; } // Variables to deal with graphics. private JPanel panel ; private Graphics imageGraphics ; private Image image ; private int blocksize ; // Colors of ants. private final Color ANTCOLOR = Color.red ; private final Color LOADEDANTCOLOR = Color.yellow ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -