📄 railway.java
字号:
//列车在哪一站,返回该站属性
public int atWhichStation() {
for (int i = 0; i < Stations.numOfStation; i++) {
if (positionX == Railway.stations[i].x &&
positionY == Railway.stations[i].y)
return i;
}
return 0;
}
//移动列车
public void move() {
if (isMoving) {
int[][] p = lines.getPoints(onWhichLine);
int pointNum = lines.getPointNum(onWhichLine);
if ((positionX == p[pointNum - 1][0]) &&
(positionY == p[pointNum - 1][1])) {
nextPoint = pointNum - 1;
}
if ((positionX == p[0][0]) && (positionY == p[0][1])) {
nextPoint = 1;
}
int k = nextPoint;
double diff = Math.sqrt(Math.pow((p[k][0] - p[k - 1][0]), 2) +
Math.pow((p[k][1] - p[k - 1][1]), 2));
double deltaX = speed * (p[k][0] - p[k - 1][0]) / diff;
double deltaY = speed * (p[k][1] - p[k - 1][1]) / diff;
switch (direction) {
case 0:
deltaX = deltaX;
deltaY = deltaY;
break;
case 1:
deltaX = -deltaX;
deltaY = -deltaY;
break;
}
int nextX = positionX + (int) deltaX;
int nextY = positionY + (int) deltaY;
//求斜率
double slope;
if (p[k][0] == p[k - 1][0]) {
slope = 100;
} //若斜率无穷大,则任设一个大于1的数
else slope = (p[k][1] - p[k - 1][1]) / (p[k][0] - p[k - 1][0]);
slope = (int) slope;
int minX = Math.min(p[k][0], p[k - 1][0]);
int maxX = Math.max(p[k][0], p[k - 1][0]);
int minY = Math.min(p[k][1], p[k - 1][1]);
int maxY = Math.max(p[k][1], p[k - 1][1]);
switch (direction) {
case 0:
if (( -1 <= slope) && (slope <= 1)) {
if ((minX < nextX) && (nextX < maxX)) {
positionX = nextX;
positionY = nextY;
break;
} else {
positionX = p[k][0];
positionY = p[k][1];
nextPoint++;
break;
}
} else {
if ((minY < nextY) && (nextY < maxY)) {
positionX = nextX;
positionY = nextY;
break;
} else {
positionX = p[k][0];
positionY = p[k][1];
nextPoint++;
break;
}
}
case 1:
if (( -1 <= slope) && (slope < 1)) {
if ((minX < nextX) && (nextX < maxX)) {
positionX = nextX;
positionY = nextY;
break;
} else {
positionX = p[k - 1][0];
positionY = p[k - 1][1];
nextPoint--;
break;
}
} else {
if ((minY < nextY) && (nextY < maxY)) {
positionX = nextX;
positionY = nextY;
break;
} else {
positionX = p[k - 1][0];
positionY = p[k - 1][1];
nextPoint--;
break;
}
}
}
}
}
//到站处理
public void stop() {
if (isAtStation()) {
if (hasPausedTime <= shouldPausedTime) {
isMoving = false;
hasPausedTime++;
} else {
hasPausedTime = 0;
isMoving = true;
changeDirection(); //实现到终点时返回
}
}
}
public void paint(Graphics g) {
g.fillRect(positionX - width / 2, positionY - height / 2, width, height); //显示车身
g.setPaintMode();
g.setColor(Color.green); //设置车号的颜色
g.drawString(String.valueOf(trainID), positionX - width / 2,
positionY - height / 2); //显示车号
}
}
//文件名:Lines.java
class Lines {
//乌鲁木齐-银川-呼和浩特
static int p00[] = {10,10};//{168, 155}; //乌鲁木齐
static int p01[] = {179, 169};
static int p02[] = {223, 179};
static int p021[] = {230, 193};
static int p03[] = {241, 211};
static int p04[] = {285, 244};
static int p05[] = {312, 259};
static int p06[] = {317, 264};
static int p07[] = {331, 269};
static int p08[] = {345, 269};
static int p10[] = {352, 257}; //银川
static int p11[] = {367, 220};
static int p12[] = {378, 221};
static int p13[] = {387, 226};
static int p14[] = {405, 226};
static int p20[] = {410, 222}; //呼和浩特
static int pointNumOfEW1 = 16;
static int arrayEW1[][] = {p00, p01, p02, p021, p03, p04, p05, p06, p07, p08,
p10, p11, p12, p13, p14, p20};
//西宁-兰州-西安-洛阳-郑州
static int q00[] = {304, 282}; //西宁
static int q10[] = {329, 292}; //兰州
static int q11[] = {333, 306};
static int q12[] = {340, 312};
static int q13[] = {362, 314};
static int q20[] = {382, 319}; //西安
static int q30[] = {421, 311}; //洛阳
static int q40[] = {436, 311}; //郑州
static int pointNumOfEW2 = 8;
static int arrayEW2[][] = {q00, q10, q11, q12, q13, q20, q30, q40};
//昆明-贵阳-长沙-南昌-金华-上海
static int r00[] = {303, 447}; //昆明
static int r01[] = {330, 429};
static int r10[] = {353, 428}; //贵阳
static int r11[] = {368, 426};
static int r12[] = {392, 414};
static int r20[] = {432, 405}; //长沙
static int r21[] = {442, 410};
static int r22[] = {455, 409};
static int r30[] = {468, 399}; //南昌
static int r31[] = {476, 402};
static int r32[] = {487, 400};
static int r33[] = {501, 389};
static int r40[] = {511, 385}; //金华
static int r41[] = {518, 382};
static int r42[] = {516, 370};
static int r50[] = {530, 354}; //上海
static int pointNumOfEW3 = 16;
static int arrayEW3[][] = {r00, r01, r10, r11, r12, r20, r21, r22, r30, r31,
r32, r33, r40, r41, r42, r50};
//银川-成都-昆明-南宁
static int s00[] = p10; //银川
static int s01[] = {347, 269};
static int s02[] = {349, 286};
static int s03[] = {359, 304};
static int s04[] = {359, 321};
static int s05[] = {346, 340};
static int s06[] = {334, 347};
static int s10[] = {325, 368}; //成都
static int s11[] = {314, 377};
static int s12[] = {314, 383};
static int s13[] = {295, 416};
static int s14[] = {293, 437};
static int s20[] = r00; //昆明
static int s21[] = {335, 453};
static int s22[] = {353, 462};
static int s30[] = {371, 478}; //南宁
static int pointNumOfSN1 = 16;
static int arraySN1[][] = {s00, s01, s02, s03, s04, s05, s06, s10, s11, s12,
s13, s14, s20, s21, s22, s30};
//北京-郑州-武汉-长沙-广州
static int t00[] = {461, 234}; //北京
static int t01[] = {442, 262};
static int t02[] = {440, 300};
static int t10[] = q40; //郑州
static int t11[] = {441, 330};
static int t20[] = {444, 371}; //武汉
static int t21[] = {444, 383};
static int t22[] = {434, 391};
static int t30[] = r20; //长沙
static int t31[] = {430, 441};
static int t32[] = {439, 454};
static int t40[] = {436, 474}; //广州
static int pointNumOfSN2 = 12;
static int arraySN2[][] = {t00, t01, t02, t10, t11, t20, t21, t22, t30, t31,
t32, t40};
//哈尔滨-长春-沈阳-天津-济南-南京-上海
static int u00[] = {560, 139}; //哈尔滨
static int u10[] = {550, 168}; //长春
static int u11[] = {539, 184};
static int u20[] = {534, 199}; //沈阳
static int u21[] = {512, 199};
static int u22[] = {508, 216};
static int u23[] = {491, 236};
static int u30[] = {473, 245}; //天津
static int u31[] = {465, 269};
static int u40[] = {473, 282}; //济南
static int u41[] = {482, 336};
static int u50[] = {501, 346}; //南京
static int u51[] = {512, 346};
static int u52[] = {519, 352};
static int u60[] = r50; //上海
static int pointNumOfSN3 = 15;
static int arraySN3[][] = {u00, u10, u11, u20, u21,u22, u23, u30, u31, u40, u41,
u50, u51, u52, u60};
//武汉-南昌-福州
static int v00[] = t20; //武汉
static int v01[] = {460, 382};
static int v10[] = r30; //南昌
static int v11[] = {477, 404};
static int v12[] = {493, 423};
static int v20[] = {512, 428}; //福州
static int pointNumOfSN = 6;
static int arraySN[][] = {v00, v01, v10, v11, v12, v20};
Lines() {}
//返回铁路标注点数据
public int[][] getPoints(int whichLine) {
if (whichLine == 11) {
return arrayEW1;
}
if (whichLine == 12) {
return arrayEW2;
}
if (whichLine == 13) {
return arrayEW3;
}
if (whichLine == 21) {
return arraySN1;
}
if (whichLine == 22) {
return arraySN2;
}
if (whichLine == 23) {
return arraySN3;
}
if (whichLine == 33) {
return arraySN;
}
int zero[][] = { {0, 0}, {0, 0}
};
return (zero);
}
//返回铁路标注点个数
public int getPointNum(int whichLine) {
if (whichLine == 11) {
return pointNumOfEW1;
}
if (whichLine == 12) {
return pointNumOfEW2;
}
if (whichLine == 13) {
return pointNumOfEW3;
}
if (whichLine == 21) {
return pointNumOfSN1;
}
if (whichLine == 22) {
return pointNumOfSN2;
}
if (whichLine == 23) {
return pointNumOfSN3;
}
if (whichLine == 33) {
return pointNumOfSN;
}
return 0;
}
}
class Stations {
int x, y; //车站的坐标
String stationName; //车站名
public final static int numOfStation = 26; //车站数
public Stations() {}
public Stations(int xy[], String name) {
this.x = xy[0];
this.y = xy[1];
this.stationName = name;
}
//初始化车站
public final static void initStation(Stations[] stations) {
//乌鲁木齐-银川-呼和浩特
stations[0] = new Stations(Lines.p00, "乌鲁木齐");
stations[1] = new Stations(Lines.p10, "银川");
stations[2] = new Stations(Lines.p20, "呼和浩特");
//北京-郑州-武汉-长沙-广州
stations[3] = new Stations(Lines.t00, "北京");
stations[4] = new Stations(Lines.t10, "郑州");
stations[5] = new Stations(Lines.t20, "武汉");
stations[6] = new Stations(Lines.t30, "长沙");
stations[7] = new Stations(Lines.t40, "广州");
//银川-成都-昆明-南宁
//stations[8]=new Stations(Lines.s00,"银川");
stations[9] = new Stations(Lines.s10, "成都");
stations[10] = new Stations(Lines.s20, "昆明");
stations[11] = new Stations(Lines.s30, "南宁");
//哈尔滨-长春-沈阳-天津-济南-南京-上海
stations[12] = new Stations(Lines.u00, "哈尔滨");
stations[13] = new Stations(Lines.u10, "长春");
stations[14] = new Stations(Lines.u20, "沈阳");
stations[15] = new Stations(Lines.u30, "天津");
stations[16] = new Stations(Lines.u40, "济南");
stations[17] = new Stations(Lines.u50, "南京");
stations[18] = new Stations(Lines.u60, "上海");
//西宁-兰州-西安-洛阳-郑州
stations[19] = new Stations(Lines.q00, "西宁");
stations[20] = new Stations(Lines.q10, "兰州");
stations[21] = new Stations(Lines.q20, "西安");
stations[22] = new Stations(Lines.q30, "洛阳");
//stations[23]=new Stations(Lines.q40,"郑州");
//昆明-贵阳-长沙-南昌-金华-上海
//stations[23]=new Stations(Lines.r00,"昆明");
stations[8] = new Stations(Lines.r10, "贵阳");
//stations[23]=new Stations(Lines.r20,"长沙");
stations[23] = new Stations(Lines.r30, "南昌");
stations[24] = new Stations(Lines.r40, "金华");
//stations[23]=new Stations(Lines.r50,"上海");
//武汉-南昌-福州
stations[25] = new Stations(Lines.v20, "福州");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -