Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (856)
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  
  Affine Transforms and rotating images  (Read 2871 times)
0 Members and 1 Guest are viewing this topic.
Offline Zaknafein

Junior Newbie

« Posted 2005-06-09 01:33:25 »

I was wondering how to rotate a png image.  I have tried searching the forums for relevant information, but haven't been able to figure out the problem yet.  Some of the topics I've read about using Affine Transform to rotate my images don't make a whole lot of sense to me.
How am I supposed to use Affine Transform to rotate my sprite?  I've already tried doing something akin to this:

Here is one method I use:

Write a draw(Graphics2D) method for the Sprite class.

In the painting method, you can store the current AffineTransfrom of the Graphics2D with


AffineTransform emptyXform = g2d.getTransform();
for( all the sprites ){

In the draw method of the sprite you can do any transformation necessary on the Graphics2D without wrecking anything. emptyXform simply resets the transform to one which doesn't do anything (it's probably just the identity transformation). Simply use

g2d.rotate(angle, centerX, centerY);

inside the draw() methods of the sprites.
(posted by Ask_Hjorth_Larsen, May 16, 2005)

However, what this ends up doing is rotating everything on the screen, including my sprite, and background image. 

Thanks for any help!
Offline woogley
« Reply #1 - Posted 2005-06-09 02:53:55 »

since the Graphics is drawing the entire screen, the AffineTransform is also affecting the entire screen.  This means when you set up an anchor point for rotation, you need to set it relative to its X and Y positions on the screen. Also, whenever you do ANY individual transform (for a single sprite or whatever), you should ALWAYS reset Graphics's transform to the original transform. Like so:

AffineTransform orig = g2d.getTransform();
// do rotated drawing here
// continue with normal drawing
// (or loop back to doing another rotated drawing)

Now I'll elaborate more about the "anchor point" I mentioned earler. This is were the center of the "rotation circle" would be. So if you wanted to rotate an image from the image's center (similiar to how a tire or CD spins.. from the center), you need to set the anchor point to:

new Point(imageX+(imageWidth/2),imageY+(imageHeight/2);

if you were to set the anchor point to just the imageX and imageY values (without adding anything else), the image would be rotated from its top-left corner.. kinda as if it was being dragged Wink

I coded an example for you to (hopefully) clear things up.

RotationTest.jar | Source

Hope this helps Cheesy
Offline Zaknafein

Junior Newbie

« Reply #2 - Posted 2005-06-09 18:31:47 »

Ok, nice.  Thanks man - it works well now.

Just one question though:  I don't quite understand how only the sprite is rotated, and not everything else along with it.  I know that setting the transform of the Graphics2D object used to draw everything to the screen to an empty transform does something, but I'm not quite sure what.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Devvie

Java games rock!

« Reply #3 - Posted 2005-06-10 07:13:24 »

Everything you draw with a Graphics2D will be transformed by the Graphics2D's transform. Thus, as you know, if you just call g2d.rotate(something), it will permanently alter the transform. This is why the original transform is backed up in the beginning. Then each sprite can, during its draw method, arbitrarily transform the Graphics2D object. This will NOT alter the backup transform, since the Graphics2D stores its transform elsewhere. When a sprite is finished drawing, resetting the transform to the backup one will simply make the Graphics2D forget any changes which the last sprite might have made to its transform.
Offline trembovetski

Senior Devvie

If only I knew what I'm talking about!

« Reply #4 - Posted 2005-06-14 03:57:01 »

You can also do this:

Graphics2D newG = g2d.create(); // creates a copy of g2d
// do whatever with newG

// no need to restore g2d's attributes

Offline swpalmer

JGO Coder

Exp: 12 years

Where's the Kaboom?

« Reply #5 - Posted 2005-06-14 04:56:27 »

Can you give some idea of where the line is in terms of it being faster to create a new graphics context vs. save and restore value in a single context? E.g. if it is just a single transform, I'm guessing it may not be worth it.  But if you have to save and restore the transform, and the current color, and line stroke, etc... then it probably makes more sense to just create a temporary context.

Am I correct?

Pages: [1]
  ignore  |  Print  

hadezbladez (703 views)
2018-11-16 13:46:03

hadezbladez (349 views)
2018-11-16 13:41:33

hadezbladez (688 views)
2018-11-16 13:35:35

hadezbladez (172 views)
2018-11-16 13:32:03

EgonOlsen (2364 views)
2018-06-10 19:43:48

EgonOlsen (2476 views)
2018-06-10 19:43:44

EgonOlsen (1464 views)
2018-06-10 19:43:20

DesertCoockie (2129 views)
2018-05-13 18:23:11

nelsongames (1906 views)
2018-04-24 18:15:36

nelsongames (2574 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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!