📄 lifesim.java
字号:
package lifetest;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.Vector;import lifetest.World;/** * Title: LifeSim * Description: * Copyright: Copyright (c) 2004 * Company: www.swarmagents.com * @author jake * @version 1.0 */class ViewCanvas extends Canvas{ Image image; Color obs_color; Color origin_color; Color back_color; Color end_color; boolean first; boolean reset; lifesim localworld; public ViewCanvas(Image img,lifesim world){ super(); image = img; obs_color = Color.white; setBackground(Color.black); setForeground(Color.white); first = true; reset = false; localworld=world; } public ViewCanvas(lifesim world) { super(); image = null; localworld=world; } public void Clear() { reset = true; repaint(); } public void paint(Graphics g) { localworld.localwd.repaint(g); } Graphics GetGra() { return this.getGraphics(); }}public class lifesim extends Applet implements Runnable { boolean isStandalone = false; Thread runner; boolean running,First; int delay=1; //controls: ViewCanvas canvas=new ViewCanvas(this); Button btnStart=new Button(" Start "); Button btnConfig=new Button("Config"); Button btnStep=new Button("Step"); Button btnShow=new Button("Curves"); Button btnSave=new Button("Save"); Choice choiceAgType=new Choice(); Choice choiceDrawType=new Choice(); Panel leftPanel=new Panel(); Label lblTitle=new Label("Env Control Panel"); Label lblObj=new Label("Objects:"); Choice choiceObj=new Choice(); Label lblShapeType=new Label("Shapes:"); Choice choiceShapeType=new Choice(); Label lblFillType=new Label("Fill type:"); Choice choiceFill=new Choice(); Label lblLineWidth=new Label("Line width:"); Choice choiceLineWidth=new Choice(); Label lblGValue=new Label("Gradiant value:"); TextField txtGValue=new TextField(); Scrollbar slbGradiant=new Scrollbar(Scrollbar.HORIZONTAL,1,1,0,1000); Label lblRanAdd=new Label("Ran food supply:"); TextField txtRanAdd=new TextField(); Scrollbar slbAddFood=new Scrollbar(Scrollbar.HORIZONTAL,1,1,0,1000); Label lblDynEnv=new Label("Dynamic Env:"); Choice choiceDynEnv=new Choice(); Button btnRestart=new Button("Restart");//parameters for env panel: int objSelect=0; int shapeType=0; int fillType=0; int lineWidth=1; double gValue=0.00001; int gConstant=10000; boolean mousePressed=false; Point startPoint; Point endPoint; int interveneType=2; boolean haveSetup=false; //color constant static Color COLOR_BACK; static Color COLOR_LIFE; static Color COLOR_TAIL; static Color COLOR_GRASS; static Color COLOR_SEED; static Color COLOR_SELECT; World localwd; //DataBase dataBase; /**Get a parameter value*/ public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /**Construct the applet*/ public lifesim() { COLOR_BACK=Color.black; COLOR_LIFE=Color.white; COLOR_TAIL=Color.red; COLOR_GRASS=Color.blue; COLOR_SEED=Color.green; COLOR_SELECT=Color.white; localwd=new World(this); } /**Initialize the applet*/ public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { setLayout(new BorderLayout()); Panel pan=new Panel(); //som envents handler canvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { canvas_mouseDragged(e); } }); txtGValue.addTextListener(new java.awt.event.TextListener() { public void textValueChanged(TextEvent e) { txtGValue_textValueChanged(e); } }); txtRanAdd.addTextListener(new java.awt.event.TextListener() { public void textValueChanged(TextEvent e) { txtRanAdd_textValueChanged(e); } }); //prepare for the controls add("South",pan); pan.add(btnStart); btnRestart.enable(false); pan.add(btnRestart); pan.add(btnConfig); pan.add(btnStep); pan.add(btnShow); //pan.add(btnSave); choiceAgType.addItem("No seeds"); choiceAgType.addItem("Have seeds"); pan.add(choiceAgType); choiceAgType.select(1); localwd.agType=choiceAgType.getSelectedIndex(); choiceDrawType.addItem("Don't draw seeds"); choiceDrawType.addItem("Draw seeds"); choiceDrawType.select(localwd.drawType); pan.add(choiceDrawType); leftPanel.setLayout(new GridLayout(15,1)); leftPanel.add(lblTitle); txtRanAdd.setText(Integer.toString(localwd.FoodAddNum)); slbAddFood.setValue(localwd.FoodAddNum); Panel panAdd=new Panel(); panAdd.add(lblRanAdd); panAdd.add(txtRanAdd); leftPanel.add(panAdd); leftPanel.add(slbAddFood); leftPanel.add(lblDynEnv); choiceDynEnv.addItem("Random"); choiceDynEnv.addItem("large circle"); choiceDynEnv.addItem("circle"); choiceDynEnv.addItem("helix "); choiceDynEnv.addItem("ring"); choiceDynEnv.addItem("knot"); choiceDynEnv.addItem("game of life"); choiceDynEnv.addItem("fractal leaf"); leftPanel.add(choiceDynEnv); leftPanel.add(lblObj); choiceObj.addItem("Food"); choiceObj.addItem("Eraser"); choiceObj.addItem("Select"); leftPanel.add(choiceObj); leftPanel.add(lblShapeType); choiceShapeType.addItem("Rectangle"); choiceShapeType.addItem("Line"); leftPanel.add(choiceShapeType); leftPanel.add(lblFillType); choiceFill.addItem("Solid"); choiceFill.addItem("Gradiant"); //choiceFill.addItem("Random"); leftPanel.add(choiceFill); slbGradiant.setValue((int)(gConstant*gValue)); txtGValue.setText(Double.toString(gValue)); Panel panGValue=new Panel(); panGValue.add(lblGValue); panGValue.add(txtGValue); leftPanel.add(panGValue); leftPanel.add(slbGradiant); leftPanel.add(lblLineWidth); for(int i=1;i<100;i++){ choiceLineWidth.addItem(Integer.toString(i)); } choiceLineWidth.select(20); leftPanel.add(choiceLineWidth); add("West",leftPanel); this.add("Center",canvas); canvas.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { canvas_mousePressed(e); } public void mouseReleased(MouseEvent e) { canvas_mouseReleased(e); } }); //dataBase=new DataBase(); First=true; reinit(); } public void reinit(){ localwd.reinit(); } public Graphics getCanvasGraphics(){ return canvas.GetGra(); } public void start(){ if (runner == null){ runner= new Thread(this); runner.start(); running =false; } } public void stop() { if (runner!=null) { runner.stop(); runner=null; running = false; } } public void run() { if (First) { First = false; localwd.g=this.getCanvasGraphics(); } while (true) { //canvas.reset=false; if(running){ localwd.process(); showStatus("Model Cycle:"+Long.toString(localwd.modelTime)); }else{ if(localwd.modelTime>0&&!btnStart.getLabel().equals("Continue"))btnStart.setLabel("Continue"); } try { Thread.sleep(delay);} catch (InterruptedException e) { } } } public void destroy() { //当结束程序的时候,把线程也结束,不运行这里可能会在网页上出错。 if (runner!=null){ running = false; runner.stop(); runner=null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -