📄 railway.java
字号:
//<applet code=Railway width=200 height=100>
//</applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;
public class Railway extends Applet implements Runnable, MouseListener,
ItemListener, ActionListener {
Thread thd = null, thd2;
Image backGroundImage, playImage, playInImage, stopImage, stopInImage,
signImage;
Image offScreenImage = null;
Graphics offScreenGraphics;
Button buttonChangeSpeed, buttonStopSound;
TextField textWhichSpeed;
Label label1, label2;
Choice choiceWhichTrain, choice2;
AudioClip sound;
MediaTracker tracker = new MediaTracker(this);
//界面中图形的位置
int imgBackGroundTop = 0;
int stopButtonLeft = 230; //暂停按钮左边距
int playButtonLeft = 350;
int imgButtonTop = 65;
public final static int numOfTrain = 13; //列车数
static Stations stations[] = new Stations[Stations.numOfStation];
static Train trains[] = new Train[numOfTrain];
public boolean trainPaused = false; //列车是否处于暂停状态
public int trainToBeTraced = 0; //正在被跟踪的列车号
//方向常量
public final static int ahead = 0; //沿着标注点的方向
public final static int back = 1; //逆着标注点的方向
//public final static int EAST2WEST=1; //在东西线上往西走
//public final static int NORTH2SOUTH=0; //在南北线上往南走
//定义地铁路线常量
public final static int EW1 = 11; //乌鲁木齐-银川-呼和浩特
public final static int EW2 = 12; //西宁-兰州-西安-洛阳-郑州
public final static int EW3 = 13; //昆明-贵阳-长沙-南昌-金华-上海
public final static int SN1 = 21; //银川-成都-昆明-南宁
public final static int SN2 = 22; //北京-郑州-武汉-长沙-广州
public final static int SN3 = 23; //哈尔滨-长春-沈阳-天津-济南-南京-上海
public final static int SN = 33; //武汉-福州
//定义初始站常量
public final int WLMQ[] = Lines.p00; //乌鲁木齐
public final int HHHT[] = Lines.p20; //呼和浩特
public final int XiNi[] = Lines.q00; //西宁
public final int ZeZo[] = Lines.q40; //郑州
public final int KuMi[] = Lines.r00; //昆明
public final int ShHa[] = Lines.r50; //上海
public final int YiCh[] = Lines.p10; //银川
public final int NaNi[] = Lines.s30; //南宁
public final int BeJi[] = Lines.t00; //北京
public final int GuZh[] = Lines.t40; //广州
public final int HEBi[] = Lines.u00; //哈尔滨
public final int FuZh[] = Lines.v20; //福州
public void init() {
//初始列车
Train.nextID = 0; //必须加上这句,否则刷新浏览器时火车号会改变
trains[0] = new Train(FuZh[0], FuZh[1], back, Color.red, SN); //福州始发
trains[1] = new Train(WLMQ[0], WLMQ[1], ahead, Color.red, EW1); //乌鲁木齐始发
trains[2] = new Train(HHHT[0], HHHT[1], back, Color.pink, EW1); //呼和浩特始发
trains[3] = new Train(XiNi[0], XiNi[1], ahead, Color.red, EW2); //西宁始发
trains[4] = new Train(ZeZo[0], ZeZo[1], back, Color.pink, EW2); //郑州始发
trains[5] = new Train(KuMi[0], KuMi[1], ahead, Color.red, EW3); //昆明始发
trains[6] = new Train(ShHa[0], ShHa[1], back, Color.pink, EW3); //上海始发
trains[7] = new Train(YiCh[0], YiCh[1], ahead, Color.red, SN1); //银川始发
trains[8] = new Train(NaNi[0], NaNi[1], back, Color.pink, SN1); //南宁始发
trains[9] = new Train(BeJi[0], BeJi[1], ahead, Color.red, SN2); //北京始发
trains[10] = new Train(GuZh[0], GuZh[1], back, Color.pink, SN2); //广州始发
trains[11] = new Train(HEBi[0], HEBi[1], ahead, Color.red, SN3); //哈尔滨始发
trains[12] = new Train(ShHa[0], ShHa[1], back, Color.pink, SN3); //上海始发
//初始车站
Stations.initStation(stations);
label1 = new Label("在状态栏显示列车的运行状况:");
add(label1);
choiceWhichTrain = new Choice();
choiceWhichTrain.addItem("第0号列车");
choiceWhichTrain.addItem("第1号列车");
choiceWhichTrain.addItem("第2号列车");
choiceWhichTrain.addItem("第3号列车");
choiceWhichTrain.addItem("第4号列车");
choiceWhichTrain.addItem("第5号列车");
choiceWhichTrain.addItem("第6号列车");
choiceWhichTrain.addItem("第7号列车");
choiceWhichTrain.addItem("第8号列车");
choiceWhichTrain.addItem("第9号列车");
choiceWhichTrain.addItem("第10号列车");
choiceWhichTrain.addItem("第11号列车");
choiceWhichTrain.addItem("第12号列车");
add(choiceWhichTrain);
choiceWhichTrain.addItemListener(this);
label2 = new Label("输入列车运行速度(3-10):");
add(label2);
textWhichSpeed = new TextField("5", 2);
add(textWhichSpeed);
buttonChangeSpeed = new Button("改变速度");
add(buttonChangeSpeed);
buttonChangeSpeed.addActionListener(this);
buttonStopSound = new Button("关闭声音");
add(buttonStopSound);
buttonStopSound.addActionListener(this);
addMouseListener(this);
//声音
sound = getAudioClip(getDocumentBase(), "sound.au");
//载入图像
showStatus("Loading image...");
backGroundImage = getImage(getDocumentBase(),"ChinaTrainMap.gif");
tracker.addImage(backGroundImage, 0);
stopImage = getImage(getDocumentBase(),"stop.GIF");
tracker.addImage(stopImage, 0);
stopInImage = getImage(getDocumentBase(), "stop.in.gif");
tracker.addImage(stopInImage, 0);
playImage = getImage(getDocumentBase(), "play.GIF");
tracker.addImage(playImage, 0);
playInImage = getImage(getDocumentBase(), "play.in.gif");
tracker.addImage(playInImage, 0);
offScreenImage = createImage(this.getSize().width,
this.getSize().height);
//产生一个图形区域,不能放在paint中,否则将占用大量CPU
waitLoadingImage();
showStatus("Images has been loaded.");
trains[0].color = Color.blue;
sound.loop();
}
public void run() {
while (true) {
//列车运行
for (int i = 0; i < numOfTrain; i++) {
trains[i].move();
}
//列车到站
for (int i = 0; i < numOfTrain; i++) {
trains[i].stop();
}
//状态栏显示
showState();
repaint();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
break;
}
}
}
public void start() {
thd = new Thread(this);
thd.start();
}
public void stop() {
thd.stop();
thd = null;
}
public void destroy() {
if (thd != null) {
thd.stop();
thd = null;
}
}
//利用双缓冲技术绘制图形
public void paint(Graphics g) {
offScreenGraphics = offScreenImage.getGraphics();
offScreenGraphics.drawImage(backGroundImage, 0, 0, this); //这个语句可以进行清屏
if (!trainPaused) {
offScreenGraphics.drawImage(playInImage, playButtonLeft,
imgButtonTop, this);
offScreenGraphics.drawImage(stopImage, stopButtonLeft, imgButtonTop, this);
}
if (trainPaused) {
offScreenGraphics.drawImage(playImage, playButtonLeft, imgButtonTop, this);
offScreenGraphics.drawImage(stopInImage, stopButtonLeft,
imgButtonTop, this);
}
for (int i = 0; i < numOfTrain; i++) {
offScreenGraphics.setColor(trains[i].color);
trains[i].paint(offScreenGraphics); //如果参数改成g将产生跳到}
g.drawImage(offScreenImage, 0, 0, this);
}
}
public void update(Graphics g) {
paint(g);
}
//状态栏显示
public void showState() {
if (trains[trainToBeTraced].isAtStation()) {
int n = trains[trainToBeTraced].atWhichStation();
showStatus("第" + String.valueOf(trainToBeTraced) + "号列车到达" +
stations[n].stationName + "站");
} else {
showStatus("第" + String.valueOf(trainToBeTraced) + "号列车在运行中...");
}
}
//等待图像装入后才继续运行
public void waitLoadingImage() {
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
return;
}
}
//处理选择控件,选择被跟踪的列车号
public void itemStateChanged(ItemEvent e) {
int tempNum;
tempNum = trainToBeTraced;
trainToBeTraced = choiceWhichTrain.getSelectedIndex();
trains[tempNum].color = trains[trainToBeTraced].color;
trains[trainToBeTraced].color = Color.blue;
}
//处理按钮控件,改变运行速度和声音
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonChangeSpeed) {
int input;
input = Integer.parseInt(textWhichSpeed.getText());
if (input < 3) {
input = 2;
textWhichSpeed.setText("3");
}
if(input>10){input=10;textWhichSpeed.setText("10");}
for (int i = 0; i < numOfTrain; i++) {
trains[i].speed = input;
}
}
if (e.getSource() == buttonStopSound) {
if (buttonStopSound.getLabel() == "关闭声音") {
sound.stop();
buttonStopSound.setLabel("打开声音"); }
else {
sound.loop();
buttonStopSound.setLabel("关闭声音"); }
}
}
//处理鼠标事件,暂停或继续列车的运行
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int width = playImage.getWidth(this);
int height = playImage.getHeight(this);
//按下暂停按钮
if ((imgButtonTop <= y) && (y <= imgButtonTop + height) &&
(stopButtonLeft <= x) && (x <= stopButtonLeft + width) &&
(!trainPaused)) {
thd.suspend();
sound.stop();
trainPaused = true;
repaint();
}
//按下运行按钮
if ((imgButtonTop <= y) && (y <= imgButtonTop + height) &&
(playButtonLeft <= x) && (x <= playButtonLeft + width) &&
(trainPaused)) {
thd.resume();
trainPaused = false;
repaint();
if (buttonStopSound.getLabel() == "关闭声音")
sound.loop();
else
sound.stop();
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
//文件名:Train.java
class Train extends Panel {
Lines lines = new Lines();
int positionX, positionY; //列车坐标
private int width, height; //列车宽和高
int direction; //方向
int speed = 0; //速度
boolean isMoving; //是否正在运行
int hasPausedTime = 0; //已经停靠站的时间
public final static int shouldPausedTime = 3; //应停靠站的时间
Color color; //车的颜色
int trainID = 0; //列车号
int onWhichLine; //在哪条铁路线上
int nextPoint; //沿标注点方向,下一个铁路标注点序号(火车在最末标注点时,该值为最末标注点序号)
public static int nextID = 0;
Train() {
this.width = 10; //车的宽和高均为10个像素
this.height = 10;
this.speed = 5; //每次移动5个像素
this.isMoving = true;
this.trainID = nextID++; //初始化一个车,车号加1
}
Train(int x, int y, int direction) {
this();
this.positionX = x;
this.positionY = y;
this.direction = direction;
}
Train(int x, int y, int direction, Color c) {
this(x, y, direction);
this.color = c;
}
Train(int x, int y, int direction, Color c, int onWhichLine) {
this(x, y, direction, c);
this.onWhichLine = onWhichLine;
}
public void setTrain(int x, int y, int direction) {
this.positionX = x;
this.positionY = y;
this.direction = direction;
}
public void reverseDirection() {
switch (direction) {
case 0:
direction = 1;
break;
case 1:
direction = 0;
break;
}
}
//改变火车方向
public void changeDirection() {
int pointNum = lines.getPointNum(onWhichLine);
if ((nextPoint >= pointNum) || (nextPoint <= 0))
reverseDirection(); //注意:pointNum要比点数大1
}
//列车是否在站
public boolean isAtStation() {
for (int i = 0; i < Stations.numOfStation; i++) {
if (positionX == Railway.stations[i].x &&
positionY == Railway.stations[i].y)
return true;
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -