Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Image Rotations  (Read 967 times)
0 Members and 1 Guest are viewing this topic.
Offline wopkins

Senior Newbie





« Posted 2005-05-16 22:52:12 »

How would I go about rotating a jpg sprite image I have on my Graphics2D Canvas?
Would I use an AffineTransform? How?
There are a lot of these images so I can't just rotate the Canvas, I have to rotate each individually.
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #1 - Posted 2005-05-16 23:41:39 »

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

1  
2  
3  
4  
5  
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

1  
g2d.rotate(angle, centerX, centerY);


inside the draw() methods of the sprites.
Offline wopkins

Senior Newbie





« Reply #2 - Posted 2005-05-17 21:42:37 »

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

Senior Newbie




Java games rock!


« Reply #3 - Posted 2005-05-31 10:09:37 »

So why will it not work? I’m really new to this, so it is probably either a major mistake or just some silly typo.

this is what i call i my game loop:
1  
2  
3  
4  
5  
6  
            AffineTransform emptyXform = g.getTransform();
            for (int i=0;i<entities.size();i++) {
                Entity entity = (Entity) entities.get(i);
                g.setTransform(emptyXform);
                entity.draw(g);
            }


and then I draw my sprites in the entity class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
      public void draw(Graphics g) {
          if (this.getHeading() == 1) {
              g.rotate(45,0,0);
              sprite.draw(g,(int) x,(int) y);
          }
                      //and so on, I need to cycle around the eight different headings and rotate
         else {
              sprite.draw(g,(int) x,(int) y);
          }
      }

It wont compile
Thanks
Offline kevglass

JGO Kernel


Medals: 118
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #4 - Posted 2005-05-31 10:13:32 »

Normally best, if it won't compile, to post the compile error?

I'd suspect though, looking at your code, its because the rotate() method does not exist on Graphics. You'd need to cast it to a Graphics2D first.

Kev

Offline MegaWolt

Senior Newbie




Java games rock!


« Reply #5 - Posted 2005-06-02 21:17:11 »

I got it now, and it great. thx
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.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (31 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!