Am creating a train simulation, but am having problems with how to turn the train? The turning has to be done while the train is moving.
The train:
class Tog extends JPanel
{
Graphics2D g2;
Rectangle bounds;
int x;
int y;
int x2;
int y2;
int antalVogne;
AffineTransform r;
Tog(int x,int y,int antalVogne)
{
this.x=x;
this.y=y;
x2=x;
y2=y;
this.antalVogne=antalVogne;
r = new AffineTransform();
}
public void update( Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bounds = getBounds();
g2.clearRect(0,0,(int)bounds.getWidth(),(int)bounds.getHeight());
g2.setColor(Color.yellow);
g2.fill3DRect(x,y,60,20,true);
if(antalVogne>0)
{
x2+=20;
for(int i=0;i<antalVogne;i++)
{
x2+=50;
g2.setColor(Color.red);
g2.fill3DRect(x2,y2,40,20,true);
g2.drawLine(x2-10,y2+10,x2,y2+10);
}
}
}
public void opdater(int x, int y)
{
this.x=x;
this.y=y;
x2=x;
y2=y;
}
public void roter()
{
// this is where the turning should be
}
}
The class where thread is created:
......
t = new Tog(x,y,2);
......
public void run()
{
while(true)
{
repaint();
if(x>=600)
{
x-=1;
}
else if(x<600 && x>500)
{
t.roter(); //the train should rotate here
x-=1;
y+=1;
}
else if(x<=500 && x>=400)
{
x-=1;
}
t.opdater(x,y);
try{
Thread.sleep(50);
}catch(InterruptedException a){}
}
}