在前面 我用多线程写了一个绘制了股票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= ; int rightMargine= ; int topMargine= ; int buttomMargine= ; int width height w h; int minStickLength= ; //坐标的短刻度 int majStickLength= ; //坐标的长刻度 int minStick= ; int majStick; String title; String xTitle= 股票代码 ; String xCode[]; float yData[] xPce[]; String MaxPce; boolean Mouse_Move; int x y X Y hx hy; Label label[]=new Label[ ]; 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( ); setBackground(bc); setLayout(null); for(int I= ;I< ;I++) label[I]=new Label(); label[I] setForeground(Color blue); this add(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) 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( ))+ ; //取最大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[ ] setText(xCode[Axsis]); label[ ] setText(Float toString(yData[Axsis])); float pcent=(xPce[Axsis]/ ); label[ ] setText(Float toString(pcent)); catch(Exception err) try if(y >topMargine & y < h+topMargine) gten drawLine(leftMargine y w+leftMargine y ); if(Y >topMargine & Y< h+topMargine) gten drawLine(leftMargine Y w+leftMargine Y); if(x >leftMargine & x < w+leftMargine) gten drawLine(x topMargine x h+topMargine); if(X >leftMargine & X< w+leftMargine) gten drawLine(X topMargine X h+topMargine); x =X; y =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( 股票代码: topMargine ); g drawString( 卖空成交金额: topMargine ); g drawString( 卖空成交金额占总卖空成交金额%: topMargine ); g drawRect(leftMargine topMargine w h); //矩形方框 int x y x y dy; int totalStick=(majStick )*minStick; x =leftMargine; dy=h/((majStick )*minStick); for(int I= ;I<=totalStick;I++) y =height buttomMargine I*dy; y =y ; x =x minStickLength; if(I%minStick== ) x =x majStickLength; g drawString( +((I)/minStick)+ % x y + ); //y轴刻度数字 g drawLine(x y x y ); //y轴刻度标 for(int I= ;I x =leftMargine+I*hx; hy=(int)((xPce[I]/ )*(h/((majStick )* ))); y =height buttomMargine hy; float pcent=(xPce[I]/ ); Color pceCololr=new Color( ); g setColor(pceCololr); g setFont(new Font( Times New Roman Font PLAIN )); g drawString(Float toString(pcent) x y );//在柱状图上绘制% if(I% == ) g setColor(Color orange); else g setColor(Color red); g fillRect(x y hx hy); //绘制柱状图 //g setColor(Color red); //绘制股票代码 //g setFont(new Font( Times New Roman Font PLAIN )); //g drawString( +xCode[I] x height topMargine+ ); Color titleColor=new Color( ); g setColor(titleColor); g setFont(new Font( monospaced Font BOLD )); g drawString(title ); catch(Exception e) System out println( sellhis paint error ); System out println(e); public static float[] Substr(String str) int I= ; StringTokenizer st = new StringTokenizer(str ); int len=untTokens(); 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= ; StringTokenizer st = new StringTokenizer(str ); int len=untTokens(); 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[ ]; 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; cha138/Article/program/Java/JSP/201311/19647