Storing a rotated image

All right…so I’ve been spending days and days looking at various methods by which
to rotate an image, and thus far haven’t been able to find what I’m looking for.
This is what I want to be able to do:

Image source = getImageFromWherever();
Image rotated = getRotatedImage(source, degrees);

Image getRotatedImage(Image source, int degrees)
{
// This is the part I don’t know how to do. :stuck_out_tongue:
}

I’ve been able to rotate the image on screen, but not store the result in another
Image object. Suggestions? Thanks.

once at the beginning:


AffineTransform identityTransform = new AffineTransform();

later:


g.translate(shipX,shipY);
g.rotate(Math.toRadians(whatsoever);
g.drawImage(shipSprite,-shipW/2,-shipH/2,shipW,shipH,null);
g.setTransform(identityTransform);

see:
http://java.sun.com/docs/books/tutorial/2d/display/transforming.html
and:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics2D.html

I assume g is a Graphics object. But what I want to do is rotate the image and then
store that result in another Image object. So after the rotation I have two images: the
original and the rotated. I’m not looking to actually DRAW either of these to screen…
that’s done later and is unrelated to this process.

you can also check out my rather weak tip of the week on
http://community.java.net/javadesktop/
(a couple of pages down).

Hehe. Well drawing to the screen or drawing into an image is the same thing (kinda) :slight_smile:

-create a managed image (to get acceleration)
-get it’s grapics
-draw into it
-dispose graphics

Then you have a new image with the stuff you just’ve drawn into it.


//init stuff
GraphicsConfiguration gc;
GraphicsDevice device;

device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gc = device.getDefaultConfiguration();
[...]
//later
BufferedImage img = gc.createCompatibleImage(60,76,Transparency.BITMASK);
Graphics2D g = (Graphics2D)img.getGraphics();
g.translate(shipX,shipY);
g.rotate(Math.toRadians(whatsoever);
g.drawImage(shipSprite,-shipW/2,-shipH/2,shipW,shipH,null); 
g.dispose();

Yeah. I know. The code you just gave me is pretty much what I’ve got already. But where is the rotated image being stored? img? I need a copy of the rotated image, not just a temporary version that I draw and throw away. Maybe I’m not being clear enough. If this code you supplied was in the aforementioned getRotatedImage() method, what would I be returning?

Somebody’s missing something in this thread, and it’s probably me. ::slight_smile:

Yes, getRotatedImage() will return “img” from oNyxs code sample. BufferedImage extends Image and can be used as any other Image you’ve got.

Ahh…after some fiddling, I figured it out. What a stupid little problem to get stuck on. ::slight_smile: Thanks for the help.