?? polarshow.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;//圖中顯示的橫坐標(biāo)數(shù)目 int cyclemax;//主程序中定義的歷史數(shù)據(jù)最大長度 int originx=40;//畫圖區(qū)域原點的坐標(biāo) 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("數(shù)據(jù)柱狀圖"); 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("現(xiàn)金量"); choiceItem.addItem("規(guī)則平均特定度"); choiceItem.addItem("預(yù)測量"); choiceItem.addItem("預(yù)測偏差"); choiceItem.addItem("預(yù)測系數(shù)a"); choiceItem.addItem("預(yù)測系數(shù)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*/ //畫圖函數(shù) 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; //清空畫圖區(qū)域 gra.clearRect(0,0,517,316); //設(shè)定原點坐標(biāo) int x=0,y=0,y0=height-originy; //對設(shè)定的要畫的橫坐標(biāo)點數(shù)循環(huán) for(int i=0;i<agCount;i++){ //當(dāng)前點坐標(biāo) 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)); } //畫坐標(biāo)軸及其說明文字 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){}; } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -