📄 graphdialog.java
字号:
package javax.swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.StringTokenizer;
public class GraphDialog extends JDialog {
/**
* @param args
*/
public GraphDialog(JFrame frame)
{
super(frame,true);
jbInit();
addListeners();
store();
setTitle("TestDialog");
this.setSize(new Dimension(300,200));
pack();
}
public void jbInit()
{
GraphPanel p = new GraphPanel();
p.setPreferredSize(new Dimension(200,200));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.CENTER);
}
public void addListeners()
{
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
public void store()
{
}
public static void main(String[] args) {
JFrame f= new JFrame();
f.setSize(new Dimension(300,500));
GraphDialog d= new GraphDialog(f);
d.show();
System.exit(0);
}
class GraphPanel extends JPanel
{
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="股票代?";
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;
GraphPanel()
{
super();
jbInit();
}
public void jbInit()
{
width=600;
height=400;
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("200201");
}
catch(Exception e){
System.out.println("Param itemmonth error!");
System.out.println(e);
}
try{
xCode=Subicode("00002,00014,00019,00023,00041,00102,00113,00114,00129,00132,00142,00146,00179,00203,00213,00223,00341");
Len=xCode.length;
hx=w/Len;
}
catch(Exception e){
System.out.println("Param itemcode error!");
System.out.println(e);
}
try{
yData=Substr("272950,193950,1015600,142700,1618500,671650,2453300,630150,28375,1827660,7700,75700,27375,3375600,47500,17500,17500");
}
catch(Exception e){
System.out.println("Param itemval error!");
System.out.println(e);
}
try{
xPce=Substr("219,156,817,114,2302,540,1974,507,22,1471,6,60,22,4717,38,114,14");
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<h+topMargine){
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("股票代?:",20,topMargine-15);
g.drawString("?空成交金?:",150,topMargine-15);
g.drawString("?空成交金?占??空成交金?%:",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<Len;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 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 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 String getMaxVal(float[] Maxval){
int I;
float result;
result=Maxval[0];
for(I=0;I<Maxval.length;I++){
if(result<Maxval[I])
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;
}
public int getLeftMargine(){
return leftMargine;
}
public int getButtomMargine(){
return buttomMargine;
}
public void setLeftMargine(int leftMargine){
this.leftMargine=leftMargine;
}
public void setButtomMargine(int buttomMargine){
this.buttomMargine=buttomMargine;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -