服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java绘制一个成交量的统计图


  在前面,我用多线程写了一个绘制了股票k线图的程序.这里我再通过绘制一个成交量的统计图说明对鼠票事件的应用.这里我们要传入几个参数:
  
  
  
  
  
  

  
  sellhis.java源程序如下:
  import java.awt.*;
  import java.applet.*;
  import java.awt.font.*;
  import java.awt.event.*;
  import java.io.*;
  import java.lang.*;
  import java.util.*;
  public class sellhis extends applet{
  static int len;
  int leftmargine=50;
  int rightmargine=20;
  int topmargine=80;
  int buttommargine=20;
  int width,height,w,h;
  int minsticklength=3; //坐标的短刻度
  int majsticklength=8; //坐标的长刻度
  int minstick=10;
  int majstick;
  string title;
  string xtitle="股票代?a";
  string xcode[];
  float ydata[],xpce[];
  string maxpce;
  boolean mouse_move;
  int x0,y0,x,y,hx,hy;
  label label[]=new label[3];
  boolean mouse_move=false;
  public void init() {
  width=this.getbounds().width;
  height=this.getbounds().height;
  w=width-leftmargine-rightmargine;
  h=height-topmargine-buttommargine;
  setsize(width,height);
  color bc=new color(229,229,229);
  setbackground(bc);
  setlayout(null);
  for(int i=0;i<3;i++){
  label[i]=new label();
  label[i].setforeground(color.blue);
  this.add(label[i]);
  if(i==0)
  label[i].reshape(75,topmargine-25,60,15);
  if(i==1)
  label[i].reshape(230,topmargine-25,80,15);
  if(i==2)
  label[i].reshape(505,topmargine-25,60,15);
  }
  try{
  title=gettitle(getparameter("itemmonth"));
  }
  catch(exception e){
  system.out.println("param itemmonth error!");
  system.out.println(e);
  }
  try{
  xcode=subicode(getparameter("itemcode"));
  len=xcode.length;
  hx=w/len;
  }
  catch(exception e){
  system.out.println("param itemcode error!");
  system.out.println(e);
  }
  try{
  ydata=substr(getparameter("itemval"));
  }
  catch(exception e){
  system.out.println("param itemval error!");
  system.out.println(e);
  }
  try{
  xpce=substr(getparameter("itempce"));
  maxpce=getmaxval(xpce);
  majstick=integer.parseint(maxpce.substring(0,1))+2; //取最大pce的最大整数
  }
  catch(exception e){
  system.out.println("param itempce error!");
  system.out.println(e);
  }
  try{
  addmouselistener(new mouseadapter(){
  public void mousereleased(mouseevent evt){//检测释放鼠标按键事件
  setcursor(cursor.getdefaultcursor());
  point ioc=evt.getpoint();
  x=ioc.x; //把当前座标传递给另一线程
  y=ioc.y;
  //repaint();
  }
  });
  addmousemotionlistener(new mousemotionadapter(){
  public void mousedragged(mouseevent evt){
  point ioc=evt.getpoint();
  if((ioc.x>leftmargine & ioc.x< w+leftmargine) & (ioc.y >topmargine & ioc.y< h+topmargine))
  setcursor(cursor.getpredefinedcursor(cursor.hand_cursor));
  else
  setcursor(cursor.getdefaultcursor());
  }
  public void mousemoved(mouseevent evt){ //检测鼠标移动事件
  point ioc=evt.getpoint();
  x=ioc.x;
  y=ioc.y;
  graphics gten;
  gten=getgraphics();
  gten.setxormode(color.white);
  gten.setcolor(color.blue);
  int axsis;
  
  if(ioc.x >leftmargine & ioc.x< w+leftmargine & ioc.y>topmargine & ioc.y  try{
  axsis=(x-leftmargine)/hx; //设定横座标范围
  //在以下区域内,把鼠标当前横座标处的各种股票参数显示在相应的标签上
  label[0].settext(xcode[axsis]);
  label[1].settext(float.tostring(ydata[axsis]));
  float pcent=(xpce[axsis]/100);
  label[2].settext(float.tostring(pcent));
  }
  catch(exception err){
  }
  try{
  if(y0 >topmargine & y0< h+topmargine)
  gten.drawline(leftmargine,y0,w+leftmargine,y0);
  if(y >topmargine & y< h+topmargine)
  gten.drawline(leftmargine,y,w+leftmargine,y);
  if(x0 >leftmargine & x0< w+leftmargine)
  gten.drawline(x0,topmargine,x0,h+topmargine);
  if(x >leftmargine & x< w+leftmargine)
  gten.drawline(x,topmargine,x,h+topmargine);
  x0=x;
  y0=y;
  }
  finally{
  gten.dispose();
  }
  }
  }
  });
  }
  catch(exception e)
  {
  system.out.println("sellhis construct add mouse listener error!");
  system.out.println(e);
  }
  }
  public void paint(graphics g){
  try{
  g.drawstring("股票代?a:",20,topmargine-15);
  g.drawstring("?u空成交金?~:",150,topmargine-15);
  g.drawstring("?u空成交金?~占???u空成交金?~%:",320,topmargine-15);
  
  
  
  g.drawrect(leftmargine,topmargine,w,h); //矩形方框
  
  
  
  int x0,y0,x1,y1,dy;
  int totalstick=(majstick-1)*minstick;
  x0=leftmargine;
  dy=h/((majstick-1)*minstick);
  for(int i=1;i<=totalstick;i++){
  y0=height-buttommargine-i*dy;
  y1=y0;
  x1=x0-minsticklength;
  if(i%minstick==0){
  x1=x0-majsticklength;
  g.drawstring(""+((i)/minstick)+"0%",x1-25,y0+5); //y轴刻度数字
  }
  g.drawline(x0,y0,x1,y1); //y轴刻度标
  }
  for(int i=0;i  x0=leftmargine+i*hx;
  hy=(int)((xpce[i]/100)*(h/((majstick-1)*10)));
  y0=height-buttommargine-hy;
  float pcent=(xpce[i]/100);
  color pcecololr=new color(153,0,153);
  g.setcolor(pcecololr);
  g.setfont(new font("times new roman",font.plain,11));
  g.drawstring(float.tostring(pcent),x0,y0-5);//在柱状图上绘制%
  if(i%2==0)
  g.setcolor(color.orange);
  else
  g.setcolor(color.red);
  g.fillrect(x0,y0,hx,hy); //绘制柱状图
  //g.setcolor(color.red); //绘制股票代码
  //g.setfont(new font("times new roman",font.plain,10));
  //g.drawstring(""+xcode[i],x0,height-topmargine+40);
  }
  color titlecolor=new color(0,64,128);
  g.setcolor(titlecolor);
  g.setfont(new font("monospaced",font.bold,20));
  g.drawstring(title,190,40);
  }
  catch(exception e){
  system.out.println("sellhis paint error.");
  system.out.println(e);
  }
  }
  public static float[] substr(string str){
  int i=0;
  stringtokenizer st = new stringtokenizer(str,",");
  int len=st.counttokens();
  float[] val=new float[len];
  while(st.hasmoretokens()) {
  val[i]=integer.parseint(st.nexttoken());
  i++;
  }
  return val;
  }
  public static string[] subicode(string str){
  int i=0;
  stringtokenizer st = new stringtokenizer(str,",");
  int len=st.counttokens();
  string[] val=new string[len];
  while(st.hasmoretokens()) {
  val[i]=st.nexttoken();
  i++;
  }
  return val;
  }
  public static string getmaxval(float[] maxval){
  int i;
  float result;
  result=maxval[0];
  for(i=0;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)+"月?u空股票?y??";
  return title;
  }

扫描关注微信公众号