Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rotating an image

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have java rotate an imageIcon x degrees? The only way I can think of is to use paintshop to rotate the image for me and store 360 images for each degree of rotation. If so how?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use a JLabel, and in the label's paintComponent()
Graphics2D.rotate(..);
Graphics2D.drawImage(..);

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html
[ December 22, 2006: Message edited by: Michael Dunn ]
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain a little more, I have my code here so far:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the search facility, keywords
Graphics2D rotate
and you'll find working examples

if you still have problems, post what you've tried
(the posted code doesn't try anything regarding rotate)
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got it now and it works perfectly except for one thing. The whole canvas rotates, I don�t want that I only want to rotate the image on the canvas, I therefore made JPanel of same size as the image and made it into a drawing canvas and added upon the old canvas. The other images which are now not part of the same canvas are not affected by the rotation but because the rotation canvas obscures the other one, there is always a solid square that turns regardless of the shape of the image.
Let me give an example to explain, lets say I wanted to make a windmill, the blades should rotate but not the mill itself however you should be able to see the mill between the blades as they turn. In my example the rotationCanvas obscures the non-rotating canvas instead of only the windmill blades.
I hope you understand what I mean, I�ll include my code:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just ran your code and the square (white background) remained whilst the image
rotated inside - java 1.5.0_05, winxp

I got rid of the white background with
rotationPanel.setOpaque(false);

one problem with the code you have adapted is the image is not really sharp.

here's a version using a label - note the rendering hints
(only used a label because it was easier to copy existing code into your code)

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if this sounds dumb but why is your version better?
The problem I described still remains for example if I add a rectangle behind the turningPanel, only the image should block the rectangle not the whole panel.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand the problem now, about the transparency between the 'blades'.
(my test.gif was a round logo, so it had no such problem)

This is not something I've had to do before, but a (very) quick search
indicates this looks like what you would need to experiment with
g2.setComposite( AlphaComposite.getInstance(...))

I'll play around with it (after the break) and see if I can get something to
work OK
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic