www.pudn.com > asm_java.zip > CurveShow.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 CurveShow 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;
  public int nAgentIndex=0;
  AsmModel local;//主程序的本地拷贝
  Choice choicelen = new Choice();
  Label label1 = new Label();
  Label label2 = new Label();
  Choice choiceItem1 = new Choice();
  Label label3 = new Label();
  Choice choiceItem2 = new Choice();

  Label lblAgent = new Label();
  Choice choiceAgent = new Choice();
  Label label5 = new Label();
  Label label6 = new Label();

  public CurveShow(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(6, 26, 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);
      }
    });
    choicelen.setBounds(new Rectangle(69, 355, 94, 21));
    choicelen.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        choicelen_itemStateChanged(e);
      }
    });
    for(int i=0;i<40;i++){
       choicelen.addItem(Integer.toString((i+1)*100));
    }
    if(type==0){
      choiceItem1.addItem("股票价格");
      choiceItem2.addItem("股票价格");
      choiceItem1.addItem("股息");
      choiceItem2.addItem("股息");
      choiceItem1.addItem("风险中性价格");
      choiceItem2.addItem("风险中性价格");
      choiceItem2.addItem("无");
      choiceItem1.select(0);
      choiceItem2.select(2);
    }else if(type==1){
      for(int i=0;i0){
        //计算需要绘制的历史数据数组中的启示索引
        nStart=step-cycles;
    }
    //清空画图区域
    gra.clearRect(0,0,517,316);

    //设定原点坐标
    int x=0,y=0,y0=height-originy;
    //对设定的要画的横坐标点数循环
    noneItem=false;
   for(int i=0;ivalue2min)valuemin=value2min;
      }
      if(valuemax==valuemin)valuemin=valuemax-100;

      int y1=(int)(height*(value1-valuemin)/(valuemax-valuemin));
      //绘制合作者比例
      if(y1<=0){
        y1=1;
      }else if(y1>=height){
        y1=height;
      }
      if(isDrawn){
        gra.setColor(Color.blue);
        gra.drawLine(x+originx,height-y,x1+originx,height-y1);
      }
      //绘制不合作者比例
      int y2=(int)(height*(value2-valuemin)/(valuemax-valuemin));
      if(y2<=0){
        y2=1;
      }else if(y2>=height){
        y2=height;
      }

      if(isDrawn&&!noneItem){
        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(sName,originx-35,originy/2);
    this.setTitle(sName+"(曲线图)");
    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=0;i<=9;i++){
        int x3=originx;
        int y3=(int)((10-i)*height/10);
        gra.drawLine(x3,y3,x3+2,y3);
        float num=(float)((double)(i*(valuemax-valuemin))/(double)10+valuemin);
        String txt=Float.toString(num);
        if(txt.length()>=5)txt=txt.substring(0,5);
        gra.drawString(txt,x3-30,y3+5);
    }
    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){};
    }
  }
}