Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  Reset 2D Transformation?  (Read 1909 times)
0 Members and 1 Guest are viewing this topic.
Offline gbeebe

Senior Devvie


Medals: 5
Projects: 1



« Posted 2011-11-29 21:15:19 »

If I do this:
1  
2  
g.translate(x, y);
g.rotate(a);

Then draw some images, the only way I know to undo/reset the transformations is to do the same thing in reverse with negative values.
1  
2  
g.rotate(-a);
g.translate(-x, -y);

Every once in a while images, that appear after I undo the transformations, that are not meant to be rotated, will flicker at an angle and with an offset.  This leads me to believe that just doing a negative reverse isn't working properly.  Most of the time it's fine, but it randomly happens a frame here and there.

So is there a (more) proper way to reset 2d transformations?

Offline R.D.

Senior Devvie


Medals: 2
Projects: 1


"For the last time, Hats ARE Awesome"


« Reply #1 - Posted 2011-11-29 21:16:20 »

You talking about Slick2D I guess? If so, you can push the Transform and pop it too. you can also reset the transform via g.resetTransform(); Smiley
Offline gbeebe

Senior Devvie


Medals: 5
Projects: 1



« Reply #2 - Posted 2011-11-29 21:20:29 »

No, just Graphics2D.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2011-11-29 21:29:07 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private LinkedList<Graphics> stack = new LinkedList<Graphics>();

public Graphics push(Graphics g) {
   this.stack.addFirst(g); // save it for later
   return g.create(); // copies the Graphic object
}

public Graphics pop() {
   return this.stack.removeFirst(); // restore it
}


1  
2  
3  
4  
5  
6  
7  
public void paint(Graphics g) {
   ...
   g = this.push(g);
   ...
   g = this.pop();
   ...
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SimonH
« Reply #4 - Posted 2011-11-29 21:35:22 »

You can also do;
1  
2  
3  
4  
5  
6  
7  
8  
9  
public void paint(Graphics g)
{
    Graphics2D g2d=(Graphics2D)g;
    AffineTransform oldTransform=g2d.getTransform();

   ...

   g2d.setTransform(oldTransform);
}

People make games and games make people
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #5 - Posted 2011-11-30 14:20:04 »

The transformations are saved in the current AffineTransform of the Graphics object, so like SimonH said you can just get a copy of it and set it later.

Myomyomyo.
Offline gbeebe

Senior Devvie


Medals: 5
Projects: 1



« Reply #6 - Posted 2011-12-01 05:21:01 »

@SimonH, Thanks.  I haven't seen the glitch since I applied your suggestion.
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.

trollwarrior1 (27 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!