package tool;import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;import javax.swing.*;public class DynamicChart extends JFrame {
private int bfWidth=300;
private int bfHeight=200;
//private int x , y;
private int x1, x2, y1, y2;
private boolean flag = true;
private int xV,yV;
List<Point> points = new ArrayList<Point>(); Random random = new Random();
private int i = 0;
private int j = 0;
private int countX = 0;
private int countY = 0;
private Image offImage = null;
public boolean kb = true;
Thread thread = null;
JLabel dataMonitorJLabel = new JLabel();
private SimuFrame simuFrame;
YJPanel yjp;ZJPanel zjp; public DynamicChart( SimuFrame simuFrame,YJPanel yjp,ZJPanel zjp,int yV) {

this.simuFrame = simuFrame;
this.yjp = yjp;this.yV=yV;
this.zjp = zjp; this.setBounds(20, 650, bfWidth, bfHeight);
this.setVisible(flag);
this.setResizable(false); BThread bThread = new BThread();
thread = new Thread(bThread); thread.start();
// if (yjp.isStart()) {
// dataMonitorJLabel.setLocation(80, 50);
// dataMonitorJLabel.setText("XXX指标:" + x2);
// this.add(dataMonitorJLabel);
//
// } this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void paint(Graphics g) {
if (offImage == null) {
offImage = this.createImage(getWidth(), getHeight());
}
Graphics goff = offImage.getGraphics();
g.drawImage(offImage, 0, 0, null);
drawXYaixs(goff);
getPoints();
drawDyn(goff);
if (yjp.isReset()) {
goff.clearRect(0, 0, getBfWidth(), getBfHeight());
yjp.setReset(false);
}

}
public void getPoints() {
i+=5 ;
x2 = (int) (i * Math.pow(0.6, countX));
y2= (int)(zjp.grassess.size()* Math.pow(0.6, countY));
points.add(new Point(x2, y2));



// i += 20;
// x2 = (int) (i * Math.pow(0.6, countX));
// y2 = (int) ((random.nextInt(i)) * Math.pow(0.6, countY));
// points.add(new Point(x2, y2));
// System.out.println(x2 + "+++" + y2);
// if (simuFrame.ischart) {
//
dataMonitorJLabel.setSize(30, 10);
dataMonitorJLabel.setText("X:" + zjp.grassess.size());
this.add(BorderLayout.EAST, dataMonitorJLabel); // } } public void drawXYaixs(Graphics g) {
((Graphics2D) g).setStroke(new BasicStroke(1.5f));
g.drawLine((int) (getBfWidth() * 0.1), (int) (getBfHeight() * 0.9),
(int) (getBfWidth() * 0.9), (int) (getBfHeight() * 0.9));
g.drawLine((int) (getBfWidth() * 0.9 - 8),
(int) (getBfHeight() * 0.9 - 4), (int) (getBfWidth() * 0.9),
(int) (getBfHeight() * 0.9));
g.drawLine((int) (getBfWidth() * 0.9 - 8),
(int) (getBfHeight() * 0.9 + 4), (int) (getBfWidth() * 0.9),
(int) (getBfHeight() * 0.9)); g.drawLine((int) (getWidth() * 0.1), (int) (getBfHeight() * 0.9),
(int) (getBfWidth() * 0.1), (int) (getBfHeight() * 0.2));
g.drawLine((int) (getWidth() * 0.1 - 4),
(int) (getBfHeight() * 0.2 + 8), (int) (getBfWidth() * 0.1),
(int) (getBfHeight() * 0.2));
g.drawLine((int) (getWidth() * 0.1 + 4),
(int) (getBfHeight() * 0.2 + 8), (int) (getBfWidth() * 0.1),
(int) (getBfHeight() * 0.2));
g.drawString("x轴" + (int) (270 / (Math.pow(0.6, countX))),
(int) (getBfWidth() * 0.6), (int) (getBfHeight() * 0.97));
g.drawString("y轴" + (int) (210 / (Math.pow(0.6, countY))),
(int) (getBfWidth() * 0.06), (int) (getBfHeight() * 0.15)); ((Graphics2D) g).setStroke(new BasicStroke(1.0f));
} public void drawDyn(Graphics g) { for (int j = 0; j < points.size(); j++) { if (j > 0) {// 至少两个点后才画图 // x,y 都在坐标范围内时,直接画图
if ((int) (points.get(j).getX() + 0.1 * getWidth()) < 0.9 * getWidth()
&& (int) (0.9 * getBfHeight() - (points.get(j).getY() + 0.1 * getBfHeight())) > 0.2 * getBfHeight()) { g.drawLine(
(int) (points.get(j - 1).getX() + 0.1 * getWidth()),
(int) (0.9 * getBfHeight() - points.get(j - 1)
.getY()),
(int) ((points.get(j).getX() ) + 0.1 * getBfWidth()),
(int) (0.9 * getBfHeight() - points.get(j).getY()));
}
// 当x越界,而y不越界时,x坐标缩减到原来的60%;
else if ((int) (points.get(j).getX() + 0.1 * getBfWidth()) > 0.9 * getBfWidth()
&& (int) (points.get(j).getY() + 0.1 * getBfHeight()) > 0.2 * getBfHeight()) {
countX++;
System.out.println("x越界" + countX);
changePointXValue();
g.clearRect(0, 0, getWidth(), getHeight());
drawXYaixs(g); }// 当x不越界,y越界时,y坐标缩小到原来的60%;
if ((int) (points.get(j).getX() + 0.1 * getWidth()) < 0.9 * getWidth()
&& (int) (0.9 * getBfHeight() - (points.get(j).getY() + 0.1 * getBfHeight())) < 0.2 * getBfHeight()) {
countY++;
System.out.println("y越界" + countY);
changePointYValue();
g.clearRect(0, 0, getBfWidth(), getBfHeight());
drawXYaixs(g);
}
}
}
} private void changePointYValue() {
for (int i = 0; i < points.size(); i++) {
points.get(i).setLocation(points.get(i).getX(),
points.get(i).getY() * 0.6); } } public void changePointXValue() {
for (int i = 0; i < points.size(); i++) {
points.get(i).setLocation(points.get(i).getX() * 0.6,
points.get(i).getY()); }
} public int getBfWidth() {
return bfWidth;
} public void setBfWidth(int bfWidth) {
this.bfWidth = bfWidth;
} public int getBfHeight() {
return bfHeight;
} public void setBfHeight(int bfHeight) {
this.bfHeight = bfHeight;
} public void inition(){
i=0;
points.removeAll(points);

repaint();
}
private class BThread implements Runnable { public void run() {
while (true) {
if (yjp.isReset()) {
System.out.println("an reset jian le "+points.size());
inition();

System.out.println("an reset jian le "+points.size());

}
while (yjp.isStart()) {
repaint();

try {
Thread.sleep(yjp.speed-1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
} public int getX2() {
return x2;
} public void setX2(int x2) {
this.x2 = x2;
}
}