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

📄 polarshow.java

📁 人工股市(Artificial Stock Market
💻 JAVA
字号:
package asm;import java.awt.event.*;import java.util.Vector;import java.awt.*;/** * Title:        Artificial Stock Market * Description:  人工模拟股市(来源:SFI的Swarm版本)的Java版本 * Copyright:    Copyright (c) 2003 * Company:      http://agents.yeah.net * @author jake * @version 1.0 */public class PolarShow extends Frame implements Runnable{  Panel view = new Panel();  Thread runner1;//定义独立线程  Graphics gra;//在一个面板view上画图  int cycles=100;//图中显示的横坐标数目  int cyclemax;//主程序中定义的历史数据最大长度  int originx=40;//画图区域原点的坐标  int originy=20;  int type;  int itemCount=9;  double maxvalue[];  public int nAgentIndex=0;  AsmModel local;//主程序的本地拷贝  Label label2 = new Label();  Choice choiceItem = new Choice();  public PolarShow(AsmModel pd,int type1) {    super("数据柱状图");    local=pd;    type=type1;    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));    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);      }    });      choiceItem.addItem("股票需求量");      choiceItem.addItem("总财富");      choiceItem.addItem("股票份额");      choiceItem.addItem("现金量");      choiceItem.addItem("规则平均特定度");      choiceItem.addItem("预测量");      choiceItem.addItem("预测偏差");      choiceItem.addItem("预测系数a");      choiceItem.addItem("预测系数b");    label2.setText("查看项目:");    label2.setBounds(new Rectangle(11, 358, 66, 17));    choiceItem.setBounds(new Rectangle(73, 355, 99, 18));    maxvalue=new double[itemCount];    for(int i=0;i<itemCount;i++){        maxvalue[i]=0.01;    }    choiceItem.select(2);    this.add(view, null);    this.add(label2, null);    this.add(choiceItem, 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=WorldVariants.cycleMax;    repaint();    if(runner1==null){      runner1=new Thread(this);      runner1.start();    }  }  public void paint(Graphics g) {    /**@todo: Override this java.awt.Component method*/    //画图函数    int agCount=local.agentList.size();    double values[]=new double[agCount];    double maxval=0.01;    for(int i=0;i<agCount;i++){      Agent ag=(Agent)local.agentList.elementAt(i);      switch (choiceItem.getSelectedIndex()){      case 0:        values[i]=ag.demand;        break;      case 1:        values[i]=ag.wealth;        break;      case 2:        values[i]=ag.position;        break;      case 3:        values[i]=ag.cash;        break;      case 4:        values[i]=ag.avspecificity;        break;      case 5:        values[i]=ag.forecast;        break;      case 6:        values[i]=Math.abs(ag.realDeviation);        break;      case 7:        values[i]=ag.pdcoeff;        break;      case 8:        values[i]=ag.offset;        break;      }      if(values[i]>maxval){        maxval=values[i];      }    }    if(maxval>maxvalue[choiceItem.getSelectedIndex()]||maxval/maxvalue[choiceItem.getSelectedIndex()]<0.01){      maxvalue[choiceItem.getSelectedIndex()]=maxval;    }    int width=517-originx-25;    int height=316-originy;    //清空画图区域    gra.clearRect(0,0,517,316);    //设定原点坐标    int x=0,y=0,y0=height-originy;    //对设定的要画的横坐标点数循环   for(int i=0;i<agCount;i++){      //当前点坐标      int x1=(int)((i*width)/agCount);      int x2=(int)(((i+1)*width)/agCount);      double rect_height;      if(maxvalue[choiceItem.getSelectedIndex()]!=0){         rect_height=(double)values[i]/(double)maxvalue[choiceItem.getSelectedIndex()];      }else{        rect_height=0;      }      int rect_width=(int)(width/agCount)-3;      gra.setColor(Color.blue);      gra.fillRect(x1+originx+3,(int)((1-rect_height)*height),rect_width,(int)(rect_height*height));    }    //画坐标轴及其说明文字    gra.setColor(Color.black);    gra.drawLine(originx,height,width+originx,height);    gra.drawLine(originx,height,originx,0);    gra.drawString("Agent编号",width+originx,height);    gra.drawString(choiceItem.getSelectedItem(),originx-40,originy/2);    this.setTitle(choiceItem.getSelectedItem()+"(柱状图)");    for(int i=0;i<agCount;i++){        int x3=(int)(i*width/agCount)+originx;        int y3=height;        gra.drawLine(x3,y3,x3,y3-2);        String txt;        txt=Integer.toString((int)(i));        gra.drawString(txt,x3,y3+12);    }    for(int i=0;i<=9;i++){        int x3=originx;        int y3=(int)((10-i)*height/10);        gra.drawLine(x3,y3,x3+2,y3);        String txt=Float.toString((float)((double)(i*maxvalue[choiceItem.getSelectedIndex()])/(double)10));        if(txt.length()>=5)txt=txt.substring(0,5);        gra.drawString(txt,x3-40,y3+10);    }    super.paint(g);  }  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 + -