Solved

How do i rotate image in Java?

Posted on 2004-04-26
13
1,009 Views
Last Modified: 2008-01-09
Okay. so i got this thing that moves around on the screen (an image of an airplane) and i got this code that prints image of this plane on the screen:

  public void paintComponent(Graphics g) {
    g.drawImage(image, x,y,null);
    g.setColor( Color.white );
    g.drawString(r.getName(), x+50, y-15);
    }

it also has little information that follows the airplane. When the plane reaches the border of the screen it starts to turn 5 degrees each time paintComponent is run, and eventually it turns in a way that it flys back towards the inside. well what i wanna do is turn the image according to the degree , but without turning the strings. Help.
0
Comment
Question by:jhson114
  • 7
  • 6
13 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 10925044
Graphics2D g2d = (Graphics2D) g;
AffineTransform tx = new AffineTransform();
tx.rotate(radians);
g2d.drawImage(image, tx, this);
0
 

Author Comment

by:jhson114
ID: 10925255
This is what i did, and if i include g.drawImage(image,x,y,null), the image of plane shows up, but doesnt turn, if i dont include it, the plane doesnt show up at all.
  public void paintComponent(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();
    tx.rotate((5/Math.PI));
    g2d.drawImage(image, tx, null);

   // g.drawImage(image, x,y,null);
    g.setColor( Color.white );
    g.drawString(r.getName(), x+50, y-15);
  }

0
 
LVL 92

Expert Comment

by:objects
ID: 10925426
you need to rotate it and offset it.
in your case you probably also want to centre the rotation arounf the image centre.

    AffineTransform tx = new AffineTransform();
    tx.rotate((5/Math.PI), xcentre, ycentre);
    tx.translate(x, y);
    g2d.drawImage(image, tx, null);
0
 

Author Comment

by:jhson114
ID: 10925470
So in this case, since X, and Y are constantly change (since they are moving), should xcentre and ycentre be something like... x+5, y+5 (cuz you want it to be the center of the picture). is this right?
0
 

Author Comment

by:jhson114
ID: 10925488
Okay. heres what i have now:

  public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();
    double radians = Math.toRadians(5);
    tx.rotate(radians, x+5, y+5);
    tx.translate(x, y);
    g2d.drawImage(image, tx, null);
  }

When the code is execute, at the very beginning, it turns 5 degrees ONCE, and then doesnt turn anymore. I guess i'm on the right track...
0
 
LVL 92

Expert Comment

by:objects
ID: 10925502
not with the code above, cause the rotation is done first and then it is translated.
If you did the translation first then yes the roatation centre would need to shift :)

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jhson114
ID: 10925516
i dont get  it. man i feel stupid :(
i just cant get it to keep rotating.
0
 
LVL 92

Expert Comment

by:objects
ID: 10925525
> it turns 5 degrees ONCE

yes you use a constant rotation of 5 degrees.

double radians = Math.toRadians(5);
0
 
LVL 92

Expert Comment

by:objects
ID: 10925530
add a member variable rotation and in paintComponent() do:

double radians = Math.toRadians(rotation);

and update the value of the rotation value to rotate the ship.
0
 

Author Comment

by:jhson114
ID: 10925586
its starting to rotate, but it doesnt rotate from the image's center. To do this, i did something like:

 double xcentre=x+(image.getWidth(null)/2.0);
    double ycentre=x+(image.getHeight(null)/2.0);

tx.rotate(radians, xcentre, ycentre);

is this correct? getting the images width and height and dividing it by two to get the center of the image?
0
 
LVL 92

Expert Comment

by:objects
ID: 10925600
don't add x:

double xcentre=(image.getWidth(null)/2.0);
    double ycentre=(image.getHeight(null)/2.0);
0
 

Author Comment

by:jhson114
ID: 10925639
EVERYTHING WORKS!! AHAHAHA!! YOU ARE THE GREATEST!! THANKS :)
0
 
LVL 92

Expert Comment

by:objects
ID: 10925644
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now