import java.awt.*;
import java.applet.*;
public class Chengtie extends Applet implements Runnable
{
Thread t=null;
//音乐
AudioClip audioRing;
AudioClip audioLine;
AudioClip audioStop;
AudioClip audio13;
public final static int numOfTrain=28;
public final static int numOfRingTrain=8;
public final static int numOfOneTrain=10;
public final static int numOf13Train=10;
public final static int numOfStation=55;
static Train trains[]=new Train[numOfTrain];
static Station stations[]=new Station[numOfStation];
//定义城铁方向常量
public final static int EAST=0;
public final static int NORTH=270;
public final static int WEST=180;
public final static int SOUTH=90;
public final static int SOUTHEAST=60;
public final static int NORTHWEST=240;
public final static int NORTHWESTL=243;
public final static int SOUTHEASTL=63;
public final static int SOUTHEASTR=16;
public final static int NORTHWESTR=196;
//定义地图位置常量
public final static int LINE_LEFT=2;
public final static int LINE_RIGHT=630;
public final static int LINE_TOP=536;
public final static int RING_LEFT=290;
public final static int RING_RIGHT=500;
public final static int RING_TOP=410;
public final static int RING_BOTTOM=582;
public final static int T_TOP=123;
public final static int T_LEFT=149;
public final static int T_MIDDLEX=330;
public final static int T_MIDDLEY=167;
public final static int T_RIGHT=490;
public final static int T_BOTTOM=395;
public final static int T_STARX=285;
public final static int T_ENDX=490;
//采用双缓冲技术后台绘制图像
Image offScreenImage;
Image backGroundImage;
Image stopImage,stopInImage,playImage,playInImage;
Graphics offScreenGraphics;
MediaTracker mediatracker;
int imgButtonLeft=330;
int imgButtonHeight=200;
Label label1,label2;
Choice choiceWhichTrain;
TextField textWhichSpeed;
TextField textStatus;
Button buttonChangeSpeed;
public int traintoBeTraced=0; //被跟踪运行状况的列车号
public boolean trainPaused=false; //列车是否处于暂停状态
boolean moveDisplay=false; //是否显示运行状态
int sleeptime=150;
public void init()
{
t=new Thread(this);
//设置各列车的初始方向及位置
for(int i=0;i<4;i++) //0-3 西直门站
trains[i]=new Train(RING_LEFT,431,NORTH,Color.red,i,-55*i,"环线");
setVisible(false);
for(int i=4;i<8;i++) //4-7 西直门站
trains[i]=new Train(RING_LEFT,431,SOUTH,Color.red,i,-55*(i-4),"环线");
setVisible(false);
for(int i=0;i<5;i++) //8-12 苹果园站
trains[i+8]=new Train(LINE_LEFT,510,SOUTHEAST,Color.green,
i+numOfRingTrain,-40*i,"一线");
for(int i=5;i<10;i++) //13-17 四惠东站
trains[i+8]=new Train(LINE_RIGHT,LINE_TOP,WEST,Color.green,
i+numOfRingTrain,-40*(i-5),"一线");
for(int i=0;i<5;i++) //18-22 北京北站
trains[i+18]=new Train(T_STARX,T_BOTTOM,NORTHWESTL,Color.blue,i+18,-50*i,"13号线"); setVisible(false);
for(int i=5;i<10;i++) //23-27 东直门
trains[i+18]=new Train(T_ENDX,T_BOTTOM,NORTH,Color.blue,i+18,-50*(i-5),"13号线");
Station.initStation(stations);
mediatracker=new MediaTracker(this);
offScreenImage=createImage(this.size().width,this.size().height);
showStatus("Loading image");
//载入图像
backGroundImage=getImage(getCodeBase(),"Chengtie.jpg");
waitLoadingImage(backGroundImage);
stopImage=getImage(getCodeBase(),"stop.gif");
waitLoadingImage(stopImage);
stopInImage=getImage(getCodeBase(),"stop.in.gif");
waitLoadingImage(stopInImage);
playImage=getImage(getCodeBase(),"play.gif");
waitLoadingImage(playImage);
playInImage=getImage(getCodeBase(),"play.in.gif");
waitLoadingImage(playInImage);
showStatus("Images has been loaded.");
setLayout(null);
label1=new Label("选择列车:");
choiceWhichTrain=new Choice();
for(int i=0;i<numOfTrain;i++)
choiceWhichTrain.addItem("第"+String.valueOf(i)+"号列车");
textStatus=new TextField(23);
label2=new Label("输入列车运行速度(5-10):");
textWhichSpeed=new TextField("5",2);
buttonChangeSpeed=new Button("改变速度");
add(label1);
add(choiceWhichTrain);
add(textStatus);
add(label2);
add(textWhichSpeed);
add(buttonChangeSpeed);
label1.setBounds(10,20,60,20);
choiceWhichTrain.setBounds(70,20,85,20);
textStatus.setBounds(155,20,185,20);
label2.setBounds(10,200,135,20);
textWhichSpeed.setBounds(10,220,60,20);
buttonChangeSpeed.setBounds(70,220,75,20);
audioRing=getAudioClip(getCodeBase(),"randj.au");
audioLine=getAudioClip(getCodeBase(),"lionking.au");
audioStop=getAudioClip(getCodeBase(),"ding.au");
audio13=getAudioClip(getCodeBase(),"audio13.au");
t.start();
t.suspend();
}
public void start()
{ if(traintoBeTraced>=0&&traintoBeTraced<8)
{
audioRing.loop();
audioLine.stop();
audio13.stop();
}
t.resume();
}
public void stop()
{
if(traintoBeTraced>=0&&traintoBeTraced<numOfRingTrain)
audioRing.stop();
else if(traintoBeTraced>=numOfRingTrain&&traintoBeTraced<18)
audioLine.stop();
else if(traintoBeTraced>=18&&traintoBeTraced<28)
audio13.stop();
t.suspend();
}
public void destroy()
{
if(t!=null){t.stop();t=null;}
}
public void run()
{
while(true) //实现到站暂停及显示跟踪列车情况
{
for(int i=0;i<numOfTrain;i++)
{
if(trains[i].isAtStation()&&trains[i].hasPausedTimes<=trains[i].shouldPausedTimes)
{
trains[i].isMoving=false;
trains[i].hasPausedTimes++;
if(i==traintoBeTraced)
{
showStatus(trains[i].traintype+"第"+String.valueOf(traintoBeTraced)+
"号列车达到"+trains[i].atWhichStation()+"站");
if(trains[i].hasPausedTimes==1)
{
audioStop.play();
textStatus.setText(trains[i].traintype+"第"+
String.valueOf(traintoBeTraced)+"号列车到达"+
trains[i].atWhichStation()+"站");
}
moveDisplay=false;
}
}
else
{
trains[i].isMoving=true;
trains[i].hasPausedTimes=0;
if((i==traintoBeTraced)&&(!moveDisplay))
{
showStatus(trains[i].traintype+"第"+
String.valueOf(traintoBeTraced)+"号列车正在运行中");
textStatus.setText(trains[i].traintype+"第"+
String.valueOf(traintoBeTraced)+"号列车正在运行中");
moveDisplay=true;
}
}
}
//控制一线地铁改变方向
for(int i=8;i<18;i++)
{
if(trains[i].positionX==LINE_RIGHT&&trains[i].isMoving)
trains[i].setDirection(WEST);//四惠东->四惠
if(trains[i].positionX==LINE_LEFT&&trains[i].isMoving)
trains[i].setDirection(SOUTHEAST);//苹果园 古城
if(trains[i].positionX==15&&trains[i].direction==SOUTHEAST)
trains[i].setDirection(EAST);// 古城八角游乐园
if(trains[i].positionX==15&&trains[i].direction==WEST)
trains[i].setDirection(NORTHWEST);// 古城 苹果园
}
//控制环线地铁改变方向
for(int i=0;i<8;i++)
{
if((trains[i].positionX==RING_RIGHT-10)&&((trains[i].positionY<=RING_TOP&&
t
评论0