import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class JLine extends Applet implements Runnable{
private int xSum = 5;   //分多少个时间段
private int ySum = 6;   //分多少个状态段
private int x = 0,x1 = 0,y1 = 0;
private int count = 2; //最高允许几台机车
private Thread th;
private ArrayList al = new ArrayList();
private ArrayList color = new ArrayList();
private ArrayList y = new ArrayList();   //活动Y直;

private boolean flay = true;
int xSpace = 0,ySpace = 0 ;   //间距
Graphics g;
Random r = new Random(); public void init(){
g = super.getGraphics();
th = new Thread(this);

x = this.getWidth() - 20;
y1 = this.getHeight() - 20;
x1 = 30;
color.add(0,Color.red);
color.add(1,Color.blue);
color.add(2,Color.cyan);
for(int i = 0; i < count; i ++){
y.add(i,new Integer(y1));

}

this.xSpace = (x - 20 - x1)/xSum;
this.ySpace = (y1 - 20)/ySum;
th.start();
} //获得机车状态的直
private void jiChe(){
for(int i = 0; i < count; i ++){
al.add(i,new Integer((r.nextInt(ySum) + 1) * ySpace));
}
}

public void run(){
try{
while(true){
jiChe();
th.sleep(2000);
if(x + xSpace >= this.getWidth() - 30 - x1){
System.out.println(x+xSpace + ": " +(this.getWidth() - 30 - x1));
flay = true;
super.repaint();
x = x1 + xSpace;
}
this.paint(g);
}
}catch(Exception e){
e.printStackTrace();
}
} public void update(){
}

public void paint(Graphics g){
if(flay == true){
g.setColor(Color.BLACK);
g.drawLine(x1,y1,this.getWidth()-20-x1,y1);
g.drawLine(x1,y1,x1,10);
huaX(xSum,xSpace);
huaY(ySum,ySpace);
flay = false;
}
huaXian();
}

private void huaXian(){
int i = 0;
int yi = 0;
int index = 0;

Iterator it = al.iterator();
Iterator cit = color.iterator();
Iterator yit = y.iterator();

while(it.hasNext() && cit.hasNext() && yit.hasNext()){
i = (Integer)(it.next());
yi = (Integer)(yit.next());
g.setColor((Color)cit.next());

g.drawLine(x,yi,x+xSpace,y1- i);
g.fillOval(x-5,yi-5,10,10);
this.validate();
y.set(index,y1 - i);
index ++;
}

x += xSpace;
}
void huaX(int space,int amount){
g.setColor(Color.BLACK);
while(space > 0){

g.drawLine(x1+space*amount ,y1+5,x1+space*amount,y1-5);
// g.drawLine(x1+(space*amount) ,y1+5,x1+(space*amount),0);
g.drawString(space + "秒",x1+space*amount,y1 + 20);
space --;
amount --;
}
}
void huaY(int space,int amount){
g.setColor(Color.BLACK);
while(space > 0){

g.drawLine(x1-5,(y1-10)-space*amount,x1+5,y1-10-space*amount);
// g.drawLine(x1-5,(y1-10)-space*amount,this.getWidth(),y1-10-space*amount);
g.drawString(space +"故障",x1-20,y1-10-space*amount);
space --;
amount --;
}
}
}