Make a train turn?

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){}       
      }
   }

I think youll have to rotate each piece of the train separatly…

or make images for each way the train can turn… but thats a wast of time…

How is the rotation done? When I try like this:

public void roter()
{
g2.rotate(-1.0, x, y);
g2.fill3DRect(x,y,60,20,true);
g2.rotate(1.0, -x, -y);
}

The turning is done ones, then the train returns to start position again?

make each square have a rotation variable so u can keep its rotation value so itll stay the way its supposed to be.

<make each square have a rotation variable

javatypo, can U be more specific ? I dont have any idea of , how I can do that?

For each part of the train that u have, make an Image obj with a graphics 2d object for it. Make sure that the Image is big enough so that the train part can rotate 360 degrees and always be shown.

Then make an int for each of these images to record their current radius.

You could use arrays of graphics contexts, and images and ints.

Then when u are moving the train it has an up/down/left/right speed.

So say u are turning moving up, and are going to turn left. You will slowly decrease the up speed, and increase the left speed. And as you do that, you want to rotate the image so that it looks like it is actaully turning.

so u could have some code like: (when drawing)


g2d1.rotate(Math.toRadians(angle));
g2d1.drawImage(img,center,of_the_image,this);
...

g.drawImage(traincar,xpos,ypos,this);

So that the image that is paints is rotated.

Is that more of a better explanation? If you have anymore questions just post again. I think I know what you may ask… 8)

btw, that method of doing things is based on that fact that you are making this game a top down view…