0

I have been looking up a lot of examples, every time I try, my image becomes offset, and also not rotated by the degree I am looking for.

I have a class which extends JPanel, and draws an image. This JPanel is then put into my JFrame. I need to have a method, which when I click a button can take the image of the JPanel, rotate it, and return ( with the new height and width ). - Then I can ask the JPanel to repaint using the new image, and.. it should have rotated.

If someone could please help me with an example, of rotating 90 degrees, and returning with now the height = the old width, and width = old height, that would be amazing!

Thanks,

ThePerson
  • 3,048
  • 8
  • 43
  • 69
  • 2
    @Getah seems to have provided you with an answer. Something to bear in mind. Rotate implies a point to rotate around. So for instance, if you wish to rotate around the centre. You move the shape so the centre is at 0,0, rotate it and then move it back so the centre is where it was. – Tony Hopkinson Nov 27 '11 at 23:57
  • 2
    See also this [answer](http://stackoverflow.com/q/3420651/230513). – trashgod Nov 28 '11 at 00:07
  • 1
    *"every time I try"* Show us one attempt, in the form of an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 28 '11 at 00:43
  • For memory and speed, it will probably be better to use a `JComponent` than a `JPanel` – Ky - Feb 14 '12 at 03:29

1 Answers1

2

Here you are

Image rotatedImage = new BufferedImage(imageToRotate.getHeight(null), imageToRotate.getWidth(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
g2d.rotate(Math.toRadians(90.0));
g2d.drawImage(imageToRotate, 0, -rotatedImage.getWidth(null), null);
g2d.dispose();
Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
GETah
  • 20,922
  • 7
  • 61
  • 103
  • Thanks Jan, i'm afraid this is giving me the exact result I keep getting... the image is rotated about 45 degrees, and off the page to the bottom left. It looks like the center is now the bottom left of the jpane, and it's been rotated 45 clockwise. :S – ThePerson Nov 27 '11 at 23:55
  • 3
    Also consider [`rotate(double theta, double x, double y)`](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#rotate%28double,%20double,%20double%29) and `Math.PI / 2`. – trashgod Nov 28 '11 at 00:13