Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (467)
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 1566 times)
0 Members and 1 Guest are viewing this topic.
Offline Zaknafein

Junior Newbie





« Posted 2005-06-09 03: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:

Quote
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

Code:

AffineTransform emptyXform = g2d.getTransform();
for( all the sprites ){
 g2d.setTransform(emptyXform);
 sprite.draw(g2d)
}


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

Code:
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 04: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:

1  
2  
3  
4  
5  
6  
AffineTransform orig = g2d.getTransform();
g2d.setTransform(...);
// do rotated drawing here
g2d.setTransform(orig);
// 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:

1  
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 20: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 Member




Java games rock!


« Reply #3 - Posted 2005-06-10 09: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 Member




If only I knew what I'm talking about!


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

You can also do this:

1  
2  
3  
4  
5  
6  
7  
Graphics2D newG = g2d.create(); // creates a copy of g2d
newG.setTransform()...
newG.drawImage()..
// do whatever with newG
newG.dispose();

// no need to restore g2d's attributes

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #5 - Posted 2005-06-14 06: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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (84 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (214 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!