📄 viewlog.java
字号:
package celluar;import java.awt.event.*;import java.awt.*;/** * Title: Game of Life * Description: * Copyright: Copyright (c) 2003 * Company: Http://agents.yeah.net * @author Keats * @version 1.0 */public class ViewLog extends Frame implements Runnable{ Panel view = new Panel(); Thread runner1;//定义独立线程 Graphics gra;//在一个面板view上画图 int cycles=100;//图中显示的横坐标数目 int cyclemax;//主程序中定义的历史数据最大长度 int originx=40;//画图区域原点的坐标 int originy=20; celluar localcell;//主程序的本地拷贝 Button btnClose = new Button(); Label label1 = new Label(); Choice choicelen = new Choice(); Label label2 = new Label(); public ViewLog(celluar cl) { super("数据走向……"); localcell=cl;//主程序实例化 try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); view.setBackground(Color.white); view.setBounds(new Rectangle(7, 25, 537, 316)); btnClose.setLabel("关闭"); btnClose.setBounds(new Rectangle(352, 354, 80, 25)); btnClose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btnClose_actionPerformed(e); } }); this.setBackground(Color.gray); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(WindowEvent e) { this_windowOpened(e); } public void windowClosing(WindowEvent e) { this_windowClosing(e); } }); choicelen.setBounds(new Rectangle(71, 355, 94, 21)); choicelen.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(ItemEvent e) { choicelen_itemStateChanged(e); } }); for(int i=0;i<20;i++){ choicelen.addItem(Integer.toString((i+1)*100));//选项从100-2000 } label1.setText("显示长度:"); label1.setBounds(new Rectangle(6, 360, 60, 12)); label2.setText("keatskk@sina.com"); label2.setBounds(new Rectangle(201, 359, 115, 13)); this.add(btnClose, null); this.add(view, null); this.add(choicelen, null); //this.add(btnRefresh, null); this.add(label1, null); this.add(label2, null); this.pack(); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { this_windowClosing(e); } }); this.add(btnClose, null); }//关闭 void btnClose_actionPerformed(ActionEvent e) { this.hide(); this.dispose(); } void this_windowClosing(WindowEvent e) { this.hide(); this.dispose(); } //打开 void this_windowOpened(WindowEvent e) { gra=view.getGraphics(); cyclemax=localcell.cyclemax;//获取参数 repaint(); if(runner1==null){ runner1=new Thread(this); runner1.start(); } } public void paint(Graphics g) { /**@todo: Override this java.awt.Component method*/ //画图函数 int width=517-originx-25; int height=316-originy; int step=localcell.steps; int nStart=0;// //开始nStart为零,运行一段时间后,图已经超过cycles范围,此时nStart就向后移 //|__________|____|_________|____ //0 nStart nEnd int nEnd=cycles; if(step-cycles>0){ //计算需要绘制的历史数据数组中的起始索引 nStart=step-cycles; } //清空画图区域 gra.clearRect(0,0,517,316); //设定原点坐标 int x=0,y=0,y0=height-originy; //对设定的要画的横坐标点数循环 for(int i=0;i<cycles;i++){ //当前点坐标 int x1=(int)((i*width)/cycles); int y1=(int)(localcell.HistoryData[(i+nStart)%cyclemax]*height/(localcell.size*localcell.size)); //绘制生存者比例 if(y1>0){ gra.setColor(Color.blue); gra.drawLine(x+originx,height-y,x1+originx,height-y1); } //前一点的坐标 x=x1; y=y1; } //画坐标轴及其说明文字 gra.setColor(Color.black); gra.drawLine(originx,height,width+originx,height); gra.drawLine(originx,height,originx,0); gra.drawString("时间",width+originx,height); gra.drawString("总数",originx+2,originy/2); for(int i=0;i<10;i++){ int x3=(int)(i*width/10)+originx; int y3=height; gra.drawLine(x3,y3,x3,y3-2); String txt; if(step>cycles){ txt=Integer.toString((int)(step-cycles+i*cycles/10)); }else{ txt=Integer.toString((int)(i*cycles/10)); } gra.drawString(txt,x3,y3+12); } for(int i=1;i<=4;i++){ int x3=originx; int y3=(int)((4-i)*height/4); gra.drawLine(x3,y3,x3+2,y3); String txt=Integer.toString((int)(i*localcell.size*localcell.size/4)); gra.drawString(txt,x3-30,y3+15); } gra.drawString("存活者",5,height-originy-15); gra.setColor(Color.blue); gra.drawLine(2,height-originy-30,originx-5,height-originy-30); super.paint(g); } void choicelen_itemStateChanged(ItemEvent e) { cycles=(choicelen.getSelectedIndex()+1)*100; repaint(); } public void stop() { if (runner1!=null) { // running = false; runner1.stop(); runner1=null; } } public void run() { while(true){ repaint(); try{Thread.sleep(1000);}catch(InterruptedException e){}; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -