?
Solved

How do i rotate image in Java?

Posted on 2004-04-26
13
Medium Priority
?
1,051 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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