import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
public class Carve extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;
List<Points> MyList = new ArrayList<Points>();
List<Points> MyList_copy = new ArrayList<Points>();
Image offScreenImage = null;
public static boolean Live = true; //用来控制CanveThread ,灵活控制是否画曲线
public static boolean cycle=true; //用来控制查看历史的时候点的读取
String str;
UUID uuid;
Thread th;
LinkToDB link = new LinkToDB();
public void paint(Graphics g){
Points p = new Points();
Points point2;
uuid=UUID.randomUUID();
p.setX(0);
p.setY(new Random().nextInt(250));
str = "insert into Point (Point_Id,Point_X,Point_Y) values('"+uuid+"',"+p.getX()+","+p.getY()+")";
try {
link.stt = link.con.createStatement();
link.stt.executeUpdate(str);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
MyList_copy.add(p);
MyList.add(p);
Color c = g.getColor();
for(int i=0;i<MyList.size();i++) //遍历整个ArrayList,读出里面所有的Points
{
if(MyList.size()==1){ //只有一个点时
Points point1 = MyList.get(0);
g.setColor(Color.RED);
g.drawLine(point1.getX(),point1.getY(),point1.getX(),point1.getY());
g.setColor(c);
}
else{
Points point1 = MyList.get(i);
if(i+1>=MyList.size()){ //画到MyList的最后一个点时
point2 =point1;
g.setColor(Color.RED);
g.drawLine(point1.getX(),point1.getY(),point2.getX(),point2.getY());
g.setColor(c);
}else{
point2 = MyList.get(i+1);
g.setColor(Color.RED);
g.drawLine(point1.getX(),point1.getY(),point2.getX(),point2.getY());
g.setColor(c);
}
}
}
for(int i=0;i<MyList.size();i++){ //实现曲线往前走
MyList.get(i).setX(MyList.get(i).getX()+20);
}
if(MyList.size()>30){ //优化,如果MyList中的points大于Applet能显示的范围,则去掉不能显示在Applet中的Points
for(int i=0;i<MyList.size()-30;i++){
MyList.remove(i);
}
} //只留下30个点,只要能够在Applet的显示范围显示就行
}
public void update(Graphics g) { //画纸
if(offScreenImage == null) {
offScreenImage = this.createImage(500, 300);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.ORANGE);
gOffScreen.fillRect(0, 0, 500, 300);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
private class carveThread implements Runnable{
public void run() {
while(Live){
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void init() {
this.setBackground(Color.YELLOW);
th=new Thread(new carveThread());
th.start();
// this.history();
}
/**
* 点类
* @author Administrator
*
*/
private class Points {
private int X;
private int Y;
public int getX() {
return X;
}
public void setX(int x) {
X = x;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
public Points(){}
public Points(int x,int y){
this.X =x;
this.Y =y;
}
}
/*
* 连接数据库
*/
private class LinkToDB {
public Statement stt; //声明一个Statement对象
public ResultSet rs; //声明一个ResultSet对象
public Connection con;
LinkToDB(){
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String linkName="jdbc:sqlserver://localhost:1433; Database=Points;user=sa;password=1234";
try {
Class.forName(driver);
con=DriverManager.getConnection(linkName); //建立连接
stt=con.createStatement(); //创建Statement
} catch (Exception e) {
e.printStackTrace();
// link.con.close();
}
}
}
public void change(){ //线程暂停
Live = false;
cycle = false;
}
public void continue1(){ //线程继续
cycle = false;
Live = true;
if(th.getState()==Thread.State.TIMED_WAITING){ //如果线程处于运行状态下(程序还在画曲线),就什么都不做
}
else{
th=new Thread(new carveThread());
th.start(); //否则,重新接着画曲线
}
}
/**
* 可能history里面往前走的没加对,修改之
*/
public void history(){ //查看历史记录方法
Live = false;
cycle=true;
Points t;
int j=1,h=1,l=0; //j用来帮助实现第一次画一个,第二次画两个.....,h用来帮助实现当画完整个MyList_copy2的时候退出循环,l用来记录当前所画的点下标
List<Points> MyList_copy2 = new ArrayList<Points>();
// for(int i=0;i<MyList_copy.size();i++){
// MyList_copy2.add(MyList_copy.get(i));
// } //复制MyList_copy的内容到Mylist_copy2
String str1 = "select Point_Y from Point";
try {
link.stt = link.con.createStatement();
link.rs = link.stt.executeQuery(str1);
while(link.rs.next()){
t = new Points();
t.setY(link.rs.getInt("Point_Y"));
t.setX(0);
MyList_copy2.add(t);
}
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());
}
// for(int m=0;m<MyList_copy2.size();m++){
// MyList_copy2.get(m).setX(0);
// }
Graphics g1 = this.getGraphics();
g1.clearRect(0, 0, 500, 300); //把屏幕先清空
Color c = g1.getColor();
while(cycle){
for(int k=0;k<MyList_copy2.size();k++){
j--;
if(j==0){
l=k;
break;
}
if(MyList_copy2.size()==1||k==0)
{
g1.setColor(Color.BLUE);
//只有一个点或k=0的情况下
g1.drawLine(MyList_copy2.get(0).getX(), MyList_copy2.get(0).getY(), MyList_copy2.get(0).getX(), MyList_copy2.get(0).getY());
g1.setColor(c);
}else if((k+1)==MyList.size())
{
g1.setColor(Color.BLUE);
g1.drawLine(MyList_copy2.get(k).getX(), MyList_copy2.get(k).getY(), MyList_copy2.get(k).getX(), MyList_copy2.get(k).getY());
g1.setColor(c);
}else
{
g1.setColor(Color.BLUE);
g1.drawLine(MyList_copy2.get(k-1).getX(), MyList_copy2.get(k-1).getY(), MyList_copy2.get(k).getX(), MyList_copy2.get(k).getY());
g1.setColor(c);
}
}
for(int n=0;n<l;n++){
MyList_copy2.get(n).setX(MyList_copy2.get(n).getX()+20); // 实现曲线往前走
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
g1.clearRect(0, 0, 510, 300); //每次画完之后清屏
if(h==MyList_copy2.size()) cycle=false; //画完之后,退出循环
h++;
j=h;
}
Live=false; //可能是因为用了线程(第200行),所以这里在把Live设为false
}
}