?
Solved

How do i rotate image in Java?

Posted on 2004-04-26
13
Medium Priority
?
1,052 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

649 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