Solved

How do i rotate image in Java?

Posted on 2004-04-26
13
1,025 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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
 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 85
replace a word with other 1 44
jboss wildfly 10.1 10 76
Which non-HTML GUI front end to use with Java? 3 19
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

813 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

11 Experts available now in Live!

Get 1:1 Help Now