⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 viewlog.java

📁 博弈模型
💻 JAVA
字号:
package pdcelluar;import java.awt.*;import java.awt.event.*;/** * Title:竞争与合作,动态囚徒困境博弈模拟程序 * Description:画历史数据 * Copyright:    Copyright (c) 2003 * Company:http://agents.yeah.net * @author:jake * @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;  pdcelluar localpd;//主程序的本地拷贝  Button btnClose = new Button();  Choice choicelen = new Choice();  Label label1 = new Label();  Button btnRefresh = new Button();  public ViewLog(pdcelluar pd) {    super("数据走向...");    localpd=pd;    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));    }    label1.setText("显示长度:");    label1.setBounds(new Rectangle(6, 360, 60, 12));    btnRefresh.setLabel("刷新");    btnRefresh.setBounds(new Rectangle(207, 357, 65, 20));    btnRefresh.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        btnRefresh_actionPerformed(e);      }    });    this.add(btnClose, null);    this.add(view, null);    this.add(choicelen, null);    this.add(btnRefresh, null);    this.add(label1, null);    this.pack();    this.addWindowListener(new java.awt.event.WindowAdapter() {      public void windowClosing(WindowEvent e) {        this_windowClosing(e);      }    });  }  void this_windowClosing(WindowEvent e) {    this.hide();    this.dispose();  }  void btnClose_actionPerformed(ActionEvent e) {    this.hide();    this.dispose();  }   void this_windowOpened(WindowEvent e) {    gra=view.getGraphics();    cyclemax=localpd.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=localpd.steps;    int nStart=0;    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)(localpd.HistoryData[(i+nStart)%cyclemax]*height/localpd.AgCount);      //绘制合作者比例      if(y1>0){        gra.setColor(Color.blue);        gra.drawLine(x+originx,height-y,x1+originx,height-y1);      }      //绘制不合作者比例      int y2=height-(int)(localpd.HistoryData[(i+nStart)%cyclemax]*height/localpd.AgCount);      if(y1>0){        gra.setColor(Color.red);        gra.drawLine(x+originx,height-y0,x1+originx,height-y2);      }      //前一点的坐标      x=x1;      y=y1;      y0=y2;    }    //画坐标轴及其说明文字    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*localpd.AgCount/4));        gra.drawString(txt,x3-30,y3+15);    }    gra.drawString("合作者",5,height-originy-15);    gra.drawString("背叛者",5,height-5);    gra.setColor(Color.blue);    gra.drawLine(2,height-originy-30,originx-5,height-originy-30);    gra.setColor(Color.red);    gra.drawLine(2,height-20,originx-5,height-20);    super.paint(g);  }   void choicelen_itemStateChanged(ItemEvent e) {    cycles=(choicelen.getSelectedIndex()+1)*100;    repaint();  }  void btnRefresh_actionPerformed(ActionEvent e) {    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 + -