0

In this program I want to animate a train and when the train come to some x coordinates I want to rotate trains each train car(Rectangles) one by one. Train consists five cars and a rectangle represents a car. When train arrives to some location I want to animate track change(downer track to upper track). So I want to rotate each car when It arrives to the track change location. I used the following code to do this but it will rotate all the cars at once and first car 45 deg (Correct) and second car 90 and third car 135... etc.

CODE:

private void drawLineBTrain(Graphics g){

    Graphics2D gg = (Graphics2D) g;

    for(int i = 0; i < b.getSize(); i++){            
        if(rotate){
            gg.rotate(-Math.PI/4, b.getCar(i).getPosX(), b.getCar(i).getPosY());
        }
        gg.fillRect(b.getCar(i).getPosX(), b.getCar(i).getPosY(), 80, 24);
    }
}

public void moveLineBTrain(Train t, boolean goRight){

    if(goRight) {
        b = t;
        int x, y;
        for(int i = 0; i < b.getSize(); i++) {
            x = b.getCar(i).getPosX();
            b.getCar(i).setPosX(++x);
            if(x > ((getWidth() / 2) - 140) && x < ((getWidth() / 2) + 140)){
                y = 490 + (int)( (double) (-100 * x) / 280 );
                b.getCar(i).setPosY(y);
                rotate = true;
            }
        }
    } else {
        b = t;
        int x, y;
        for(int i = 0; i < b.getSize(); i++) {
            x = b.getCar(i).getPosX();
            b.getCar(i).setPosX(--x);
            if(x > ((getWidth() / 2) - 140) && x < ((getWidth() / 2) + 140)){
                y = 490 + (int)( (double) ( -100 * (1344 - x) / 280 ));
                b.getCar(i).setPosY(y);
            }
        }
    }
}
Jayanga Kaushalya
  • 2,674
  • 5
  • 38
  • 58
  • You have the line `if(rotate)` Is that what determines if a car is rotated or not? If so, what is `rotate`? – Jesse Jashinsky Nov 12 '12 at 17:45
  • Yes, rotate is a boolean. When the train cars x position is in the correct place. I make rotate to true and then I can rotate cars. Otherwise they are going straight. – Jayanga Kaushalya Nov 12 '12 at 17:47
  • When is `moveLineBTrain` executed? It doesn't look like it's executed during the for loop in `drawLineBTrain`, which means `if(rotate)` will either be false or true for all cars, not just some. – Jesse Jashinsky Nov 12 '12 at 17:55
  • They are called separately. First moveLineBTrain will executed and then drawLineBTrain will executed through repaint. I am sure value is changing. That's why all the rectangles are rotated at once. – Jayanga Kaushalya Nov 12 '12 at 17:59
  • A better way to rotate an image may be this: http://stackoverflow.com/questions/12165977/java-image-rotation/12166204 – Dan D. Nov 12 '12 at 18:35

1 Answers1

2

I guess your problem is that you mistake the rotating the "canvas", with rotating the "object". What you are doing is:

  1. You rotate the "canvas" 45 degrees, then you draw the first rectangle on it. Result: A rectangle that is rotated 45 degrees.
  2. You rotate the "canvas" 45 degrees (again!), then you draw the second rectangle on it. Result: A rectangle that is rotated 45 degrees and a second rectangle that is rotated 90 degrees.

My guess is (I can't test the code right now) that you have to move the rotation out of the for loop and undo your changes to the transformation matrix at the end, so that subsequent calls to a draw Method are not being transformed (rotated). Try this and tell us if it works, if not I will think of something more clever:

private void drawLineBTrain(Graphics g){

    Graphics2D gg = (Graphics2D) g;
    AffineTransform aT = gg.getTransform(); // We store the initial transformation matrix

    for(int i = 0; i < b.getSize(); i++){            

    if(rotate){
            gg.rotate(-Math.PI/4, b.getCar(i).getPosX(), b.getCar(i).getPosY());
    }

        gg.fillRect(b.getCar(i).getPosX(), b.getCar(i).getPosY(), 80, 24);
        gg.setTransform(aT); // We restore the transformation matrix
    }
}

Here is a good text about Graphics2D Transformations, directly from Sun. They put it this way:

To perform additional transformations, such as rotation or scaling, you can add other transforms to the Graphics2D context. These additional transforms become part of the pipeline of transformations applied during rendering

What that means is that if you call the rotate() method, the Graphics2D context (The "canvas") is being rotated and stays that way, until you rotate it back (or restore the initial transformation matrix).

das_weezul
  • 6,082
  • 2
  • 28
  • 33
  • Now when it came to the rotational point, entire train will rotate 45 deg and appear somewhere in the top. And thanks for the idea. I didn't knew that it is rotating entire canvas. But if it is rotating entire canvas why the other objects in canvas not rotating. Which means previously drawn. – Jayanga Kaushalya Nov 12 '12 at 18:43
  • Try my edited version. Sorry for the trial-and-error approach, but I can't test it right now ;) – das_weezul Nov 12 '12 at 18:57
  • No it is completely ok. Thank you very much for helping. Now it is working. Thank you again for your helpfulness and very clear answer. – Jayanga Kaushalya Nov 12 '12 at 19:14