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