📄 train.java
字号:
import java.util.*;
import java.lang.*;
import java.awt.*;
public class train extends vehicle{
final static int LIspeed=6;//the speed is assigned for straight road;
final static int CIspeed=4;//the speed is assigned for curve road;
public train(int xPosition, int yPosition,int angle1){
super(xPosition,yPosition,angle1);
}
public void draw(Graphics g){
g.setColor(Color.blue);
g.drawRect(x-5,y-5,10,10);
g.fillRect(x-5,y-5,10,10);
}
//check if a vehicle can enter road from a station
public boolean canEnter(){
return !((mateVehi.angle>330 || mateVehi.angle<30) && mateVehi.stayStation==0);
}
//canHit() A train can never hit another train since whenever they stop they are in station
//check if a vehicle is close enough to one intersection
public boolean closeToCross(leverage oneleve){
if (angle==90 && Math.abs(y-oneleve.y)<20){
return (x-(oneleve.x+10)<=60 && x-(oneleve.x+10)>0);
}
else if (angle==270 && Math.abs(y-oneleve.y)<20){
return ((oneleve.x+10)-x<=60 && (oneleve.x+10)-x>0);
}
else {return false;}
}
public boolean passedCross(leverage oneleve){
if (angle==90 && Math.abs(y-oneleve.y)<20){
return ((oneleve.x+10)-x>=40);
}
else if (angle==270 && Math.abs(y-oneleve.y)<20){
return (x-(oneleve.x+10)>=40);
}
else {return true;}
}
/*
public void outOfStation(Graphics g){
g.setColor(Color.red);
g.fillRect(x,y,10,10);
}
*/
public void move(){
//make choice to stay Station
if (angle<360 && angle>=360-CIspeed){
if (canStay()){
stayStation();
if (mateVehi.stayStation!=0){
setPosition(centerX+225,centerY);
}
else{
setPosition(centerX+210,centerY);
}
angle=0;
// isRunning=false;
return; //stay in station
}
}
if (stayStation!=0){
if (stayStation==1){
//when canEnter() is false, the train keep its stayStation=1
if (canEnter()){
// outOfStation();//clear the image of this train from the station
setPosition(centerX+250,centerY);
stayStation=0;
// isRunning=true;
}
}
else{stayStation--;}
return;
}
//A train may start walking in the curve part of the road;
if (angle==90 && (x-LIspeed)<(centerX-150)){
angle=90;
setPosition(centerX-150,centerY-100);
}
if (angle==270 && (x+LIspeed)>(centerX+150)){
angle=270;
setPosition(centerX+150,centerY+100);
}
//A train may start walking in the straight part of the road;
if (angle<90 && angle+CIspeed>=90){
angle=90;
setPosition(centerX+150,centerY-100);
return;
}
if (angle<270 && angle+CIspeed>=270){
angle=270;
setPosition(centerX-150,centerY+100);
return;
}
//walk in straight line or curve circle;
if (angle==90 && !(x==centerX-150)){
setPosition(x-LIspeed,y);
}
else if (angle==270 && !(x==centerX+150)){
setPosition(x+LIspeed,y);
}
else if (angle>=90 && angle<270){
//x0=centerX-150, y0=centerY is the origin point for left half circle;
//x=x0+r*cos(angle), y=y0-r*sin(angle) to truck the trace of circle
angle=(angle+CIspeed)%360;
double c=(double) ((double) angle)*Math.PI/180;
setPosition((centerX-150)+((int) Math.floor(100*Math.cos(c))),centerY-((int) Math.floor(100*Math.sin(c))));
}
else {
//x0=centerX+150, y0=centerY is the origin point for right half circle;
//x=x0+r*cos(angle), y=y0-r*sin(angle) to truck the trace of circle
angle=(angle+CIspeed)%360;
double c=(double) ((double) angle)*Math.PI/180;
setPosition((centerX+150)+((int) Math.floor(100*Math.cos(c))), centerY-((int) Math.floor(100*Math.sin(c))));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -