熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

java繪制一個成交量的統計圖

2022-06-13   來源: JSP教程 

  在前面我用多線程寫了一個繪制了股票K線圖的程序這裡我再通過繪制一個成交量的統計圖說明對鼠票事件的應用這裡我們要傳入幾個參數:
  
  
    >
    >
    >
  
  
  sellhisjava源程序如下:
  import javaawt*;
  import javaapplet*;
  import javaawtfont*;
  import javaawtevent*;
  import javaio*;
  import javalang*;
  import javautil*;
  public class sellhis extends Applet{
  static int Len;
  int leftMargine=;
  int rightMargine=;
  int topMargine=;
  int buttomMargine=;
  int widthheightwh;
  int minStickLength=; //坐標的短刻度
  int majStickLength=; //坐標的長刻度
  int minStick=;
  int majStick;
  String title;
  String xTitle=股票代碼;
  String xCode[];
  float yData[]xPce[];
  String MaxPce;
  boolean Mouse_Move;
  int xyXYhxhy;
  Label label[]=new Label[];
  boolean mouse_move=false;
  public void init() {
  width=thisgetBounds()width;
  height=thisgetBounds()height;
  w=widthleftMarginerightMargine;
  h=heighttopMarginebuttomMargine;
  setSize(widthheight);
  Color bc=new Color();
  setBackground(bc);
  setLayout(null);
  for(int I=;I<;I++){
  label[I]=new Label();
  label[I]setForeground(Colorblue);
  thisadd(label[I]);
  if(I==)
  label[I]reshape(topMargine);
  if(I==)
  label[I]reshape(topMargine);
  if(I==)
  label[I]reshape(topMargine);
  }
  try{
  title=getTitle(getParameter(itemmonth));
  }
  catch(Exception e){
  Systemoutprintln(Param itemmonth error!);
  Systemoutprintln(e);
  }
  try{
  xCode=Subicode(getParameter(itemcode));
  Len=xCodelength;
  hx=w/Len;
  }
  catch(Exception e){
  Systemoutprintln(Param itemcode error!);
  Systemoutprintln(e);
  }
  try{
  yData=Substr(getParameter(itemval));
  }
  catch(Exception e){
  Systemoutprintln(Param itemval error!);
  Systemoutprintln(e);
  }
  try{
  xPce=Substr(getParameter(itempce));
  MaxPce=getMaxVal(xPce);
  majStick=IntegerparseInt(MaxPcesubstring())+; //取最大pce的最大整數
  }
  catch(Exception e){
  Systemoutprintln(Param itempce error!);
  Systemoutprintln(e);
  }
  try{
  addMouseListener(new MouseAdapter(){
  public void mouseReleased(MouseEvent evt){//檢測釋放鼠標按鍵事件
  setCursor(CursorgetDefaultCursor());
  Point ioc=evtgetPoint();
  X=iocx; //把當前座標傳遞給另一線程
  Y=iocy;
  //repaint();
  }
  });
  addMouseMotionListener(new MouseMotionAdapter(){
  public void mouseDragged(MouseEvent evt){
  Point ioc=evtgetPoint();
  if((iocx>leftMargine & iocx< w+leftMargine) & (iocy >topMargine & iocy< h+topMargine))
  setCursor(CursorgetPredefinedCursor(CursorHAND_CURSOR));
  else
  setCursor(CursorgetDefaultCursor());
  }
  public void mouseMoved(MouseEvent evt){ //檢測鼠標移動事件
  Point ioc=evtgetPoint();
  X=iocx;
  Y=iocy;
  Graphics gten;
  gten=getGraphics();
  gtensetXORMode(Colorwhite);
  gtensetColor(Colorblue);
  int Axsis;
  
  if(iocx >leftMargine & iocx< w+leftMargine & iocy>topMargine & iocy  try{
  Axsis=(XleftMargine)/hx; //設定橫座標范圍
  //在以下區域內把鼠標當前橫座標處的各種股票參數顯示在相應的標簽上
  label[]setText(xCode[Axsis]);
  label[]setText(FloattoString(yData[Axsis]));
  float pcent=(xPce[Axsis]/);
  label[]setText(FloattoString(pcent));
  }
  catch(Exception err){
  }
  try{
  if(y >topMargine & y< h+topMargine)
  gtendrawLine(leftMargineyw+leftMarginey);
  if(Y >topMargine & Y< h+topMargine)
  gtendrawLine(leftMargineYw+leftMargineY);
  if(x >leftMargine & x< w+leftMargine)
  gtendrawLine(xtopMarginexh+topMargine);
  if(X >leftMargine & X< w+leftMargine)
  gtendrawLine(XtopMargineXh+topMargine);
  x=X;
  y=Y;
  }
  finally{
  gtendispose();
  }
  }
  }
  });
  }
  catch(Exception e)
  {
  Systemoutprintln(sellhis construct add mouse listener error!);
  Systemoutprintln(e);
  }
  }
  public void paint(Graphics g){
  try{
  gdrawString(股票代碼:topMargine);
  gdrawString(賣空成交金額:topMargine);
  gdrawString(賣空成交金額占總賣空成交金額%:topMargine);
  
  
  
  gdrawRect(leftMarginetopMarginewh); //矩形方框
  
  
  
  int xyxydy;
  int totalStick=(majStick)*minStick;
  x=leftMargine;
  dy=h/((majStick)*minStick);
  for(int I=;I<=totalStick;I++){
  y=heightbuttomMargineI*dy;
  y=y;
  x=xminStickLength;
  if(I%minStick==){
  x=xmajStickLength;
  gdrawString(+((I)/minStick)+%xy+); //y軸刻度數字
  }
  gdrawLine(xyxy); //y軸刻度標
  }
  for(int I=;I  x=leftMargine+I*hx;
  hy=(int)((xPce[I]/)*(h/((majStick)*)));
  y=heightbuttomMarginehy;
  float pcent=(xPce[I]/);
  Color pceCololr=new Color();
  gsetColor(pceCololr);
  gsetFont(new Font(Times New RomanFontPLAIN));
  gdrawString(FloattoString(pcent)xy);//在柱狀圖上繪制%
  if(I%==)
  gsetColor(Colororange);
  else
  gsetColor(Colorred);
  gfillRect(xyhxhy); //繪制柱狀圖
  //gsetColor(Colorred); //繪制股票代碼
  //gsetFont(new Font(Times New RomanFontPLAIN));
  //gdrawString(+xCode[I]xheighttopMargine+);
  }
  Color titleColor=new Color();
  gsetColor(titleColor);
  gsetFont(new Font(monospacedFontBOLD));
  gdrawString(title);
  }
  catch(Exception e){
  Systemoutprintln(sellhis paint error);
  Systemoutprintln(e);
  }
  }
  public static float[] Substr(String str){
  int I=;
  StringTokenizer st = new StringTokenizer(str);
  int len=untTokens();
  float[] val=new float[len];
  while(sthasMoreTokens()) {
  val[I]=IntegerparseInt(stnextToken());
  I++;
  }
  return val;
  }
  public static String[] Subicode(String str){
  int I=;
  StringTokenizer st = new StringTokenizer(str);
  int len=untTokens();
  String[] val=new String[len];
  while(sthasMoreTokens()) {
  val[I]=stnextToken();
  I++;
  }
  return val;
  }
  public static String getMaxVal(float[] Maxval){
  int I;
  float result;
  result=Maxval[];
  for(I=;I  if(result  result=Maxval[I];
  }
  return Float.toString(result);
  }
  public String getTitle(String str){
  String title;
  title=str.substring(0,4)+"年"+str.substring(4,6)+"月賣空股票統計";
  return title;
  }
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19647.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.