Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:51:40+00:00 2026-06-14T04:51:40+00:00

In this program I want to animate a train and when the train come

  • 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);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:51:42+00:00Added an answer on June 14, 2026 at 4:51 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this program is for class to display the database in Listview.i want to add
I want to write this program to find a keyword in a list. If
I have written php program and uploaded on server. I want run this program
I don't know how to code this but i want program to skip or
this is something that i don't want to program, but i was looking for
I have this statement in my c program and I want to optimize. By
This is the last part of the program I am working on. I want
I want to know how to check whether is program running and run this
I have a Python program (with Django - does this matter?) that I want
Ok, I want to make my program print out the date: 1/1/2009 But this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.