在前面我用多线程写了一个绘制了股票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 & iocytry{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=;Ix=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=;Iif(resultresult=Maxval[I];}
return Float.toString(result);
}
public String getTitle(String str){
String title;
title=str.substring(0,4)+"年"+str.substring(4,6)+"月卖空股票统计";
return title;
}