Solved

How do i rotate image in Java?

Posted on 2004-04-26
13
1,040 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 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