Search code examples
javagraphicsgraphics2d

How to rotate using Graphics2D.rotate()


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

Solution

  • 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).