Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (711)
Games in Android Showcase (213)
games submitted by our members
Games in WIP (785)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  AffineTransform cuts away parts of my image  (Read 1757 times)
0 Members and 1 Guest are viewing this topic.
Offline johtib

Senior Newbie

« Posted 2004-05-20 07:15:11 »


I've written this little tool ( to be able to generate rotated versions of an image. The way I'm currently doing the rotation isn't working properly for some reason and I really can't understand why so I'm hoping some of you guys could help me out.

To see what I mean just download and unzip the above file and run the JAR in it.

1. File->Open...
2. Select the image: images/ship_yellow.png
3. Enter the number of frames to generate and click the "Generate" button.

It works with 4 frames but not with 8, the images gets cut of. The source is included in the zip and in the JAR as well. Please have a look at it.

Best Regards,

Johan Tibell

Offline barfy

Junior Devvie

The evidence of things not seen

« Reply #1 - Posted 2004-05-20 10:31:48 »

There are several problems with your image generator:

1. Since the bounds of the image is modified by the rotation, when you call drawImage(source, 0, 0, null) for instance, you can no longer expect the top-left corner of the rotated source image to be at (0, 0) on the destination. Imagine it this way: A plain rectangle image stamped at (20, 0) and then rotated 45 degrees - you get an image whose minimum bounds are less than (20, 0).

Draw the current rotation onto an intermediate image first, then stamp that where you want it to be on the destination.

public BufferedImage generateRotatedImages(BufferedImage sourceImage, int numFrames)
            // TODO Add support for arbitrary alpha values when Java gets fast enough.
            BufferedImage image = graphicsConfiguration.createCompatibleImage(sourceImage.getWidth()
                        * numFrames, sourceImage.getHeight(), Transparency.BITMASK);
            // An intermediate image to draw a rotated instance of the source image
            BufferedImage rotatedSource = graphicsConfiguration.createCompatibleImage(sourceImage.getWidth(),
                                                                                                                          sourceImage.getHeight(), sourceImage.getColorModel().getTransparency());
            Graphics2D g2d = image.createGraphics();

            // Generate the rotated images.
            int sourceWidth = sourceImage.getWidth();
            int sourceHeight = sourceImage.getHeight();
            double angleBetweenFrames = 2 * Math.PI / numFrames;
            int imageIndex = 0; // the current rotated image index
            for (double a = 0; a < 2 * Math.PI; a += angleBetweenFrames)
                  Graphics2D rsG = rotatedSource.createGraphics();
                  rsG.rotate(a, rotatedSource.getWidth()/2, rotatedSource.getHeight()/2); // set rotation
                  rsG.drawImage(sourceImage, (rotatedSource.getWidth() - sourceWidth)/2, (rotatedSource.getHeight() - sourceHeight)/2, null);

                  g2d.drawImage(rotatedSource, imageIndex * sourceWidth, 0, null); // draw at specified coords

                  rsG.setComposite(AlphaComposite.Clear); // clear the image for use in the next iteration
                  rsG.fillRect(0, 0, rotatedSource.getWidth(), rotatedSource.getHeight());
                  imageIndex++; // increment the image index


            return image;

2. You would still get clipping when the square image is rotated and drawn onto a BufferedImage that is smaller than the size of the rotated image. Imagine a square image that is again rotated 45 degrees (to form a diamond shape) - the bounds of that rotated image is larger than the original, and when drawn onto a destination which is the same size as the square before rotation would result in clipping.

Add a bit of transparent buffer around your sprite image so that the sprite graphic you're interested in doesn't get clipped.

3. You can also consider calling repaint() (as well as revalidate()) on your ImageCanvas whenever the frames are generated. Currently, for 1-4 frames of animation, the ImageCanvas is not updated to draw the generated image.

Offline javazoid

Junior Devvie

Where's Flender?

« Reply #2 - Posted 2004-05-20 11:22:49 »

also take care of interpolation quality issues when rotating/transforming translucent images:



Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline johtib

Senior Newbie

« Reply #3 - Posted 2004-05-20 14:24:06 »

also take care of interpolation quality issues when rotating/transforming translucent images:



First of all, thanks a bunch both of you, I've almost solved my problems now. Regardsing the workaround you mentioned above I'm not quite sure how to "Using premultiplied image sources (such as INT_ARGB_PRE)", could you provide an example on how to modify the above code to do that?
Offline javazoid

Junior Devvie

Where's Flender?

« Reply #4 - Posted 2004-05-21 05:13:43 »

I have little or no experience with INT_ARG_PRE images. I wrote some test code and I remember it was slower than the INT_ARGB counterpart.
Additionally I had to call coercedata() in order to get something correctly displayed. Maybe I was wrong.

Jim Graham would be of great help for you.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

numerical (250 views)
2017-02-21 07:32:16

numerical (249 views)
2017-02-21 07:31:46

theagentd (362 views)
2017-02-18 13:42:33

theagentd (361 views)
2017-02-18 13:35:16

h.pernpeintner (1527 views)
2017-01-24 22:39:11

h.pernpeintner (1515 views)
2017-01-24 22:38:32

Galdo (2079 views)
2017-01-12 13:44:09

Archive (2090 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2627 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2342 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!