Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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  
  rotating and placing images by points OTHER than their upper left corner  (Read 2037 times)
0 Members and 1 Guest are viewing this topic.
Offline tone

Junior Newbie




Java games rock!


« Posted 2007-08-18 13:45:29 »

I've looked at several tutorials, and they offer me no insight in how to do this.

Say I have a square BufferedImage, and I want to place it somewhere so its CENTER (not upper left corner) is at a given location on the underlying component, no matter its rotation.  How do I accomplish this?  I really don't care to do explicit 2D math to figure out a false position where its upper left corner is to wind up.

Thanks in advance.

tone
Offline Jamison

Junior Devvie




We're all idiots in one way or another.


« Reply #1 - Posted 2007-08-18 15:30:28 »

Okay, that's quite easy to change the position's anchor to the center or any other arbitrary number. To center the image, just subtract half of the image's width from the x-position and half of the height from the y-position. Here's an example:
1  
2  
3  
   public void paint(Graphics g) {
      g.drawImage(img, x-(img.getWidth()/2), y-(img.getHeight()/2), null);
   }


Now to rotate an image, it gets a little more complex. You first need to create a completely empty BufferedImage, with the same width/height of the orignal image and the same transparency (ex. TYPE_INT_ARGB). Then you obtain the Graphics2D object of the empty image by calling createGraphics() on it. You then create an AffineTransform object (located in the java.awt.geom package). Then you call rotate(double, double, double) on your AffineTransform object. The first parameter is your angle to rotate upon, in radians (convert degrees to radians with Math.toRadians(int)). The second parameter is your x-anchor to rotate on. The last parameter is your y-anchor to rotate on. The next step is to call setTransform(AffineTransform) on the Graphics2D object you obtained. The last step is to draw the orignal image onto the empty image at 0,0 (using the Graphics2D again).

Here's the code to rotate:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public BufferedImage rotate(Image source, int angle) {
   int w = source.getWidth(null);
   int h = source.getHeight(null);
   BufferedImage image = new BufferedImage(w, h, source.getTransparency());
   Graphics2D g = image.createGraphics();

   AffineTransform geom = new AffineTransform();
   geom.rotate(Math.toRadians(angle), w/2, h/2);
   g.setTransform(geom);

   g.drawImage(source, 0, 0, null);
   g.dispose();

   return image;
}

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline tone

Junior Newbie




Java games rock!


« Reply #2 - Posted 2007-08-18 15:50:32 »

Thanks.  I think I just don't "get" Java2D's psychology.
I did this my own way:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
 
public void drawImage(BufferedImage img, Vec2 origin, Point p, double scale, double rotDegrees) {
        Vec2 offset = origin.scale(-(scale *img.getWidth()), -(scale *img.getHeight()));
        offset.yawBy(-rotDegrees);
        Point offsetPoint = new Point((int)Math.round(offset.x), (int)Math.round(offset.y));
        offsetPoint.translate(p.x,p.y);

        AffineTransform x = AffineTransform.getTranslateInstance(offsetPoint.x, offsetPoint.y);
        x.scale(scale, scale);
        x.rotate(Math.toRadians(rotDegrees));

        g().drawImage(img, x, null);
    }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jamison

Junior Devvie




We're all idiots in one way or another.


« Reply #3 - Posted 2007-08-18 16:01:36 »

Well, whatever works for you.

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline keldon85

Senior Devvie


Medals: 1



« Reply #4 - Posted 2007-08-18 20:47:54 »

You can either map the bitmap textures to the screen's pixels, or the screen's pixels to the bitmaps textures. Hardware prefers to map from its screen space to texture space (for obvious reasons).

Offline broumbroum

Junior Devvie





« Reply #5 - Posted 2007-08-19 00:34:22 »

I'dsuggest you to use float values for the scale factor and to makes it simplier to translate by using translate directly on Graphics:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
 
public void drawImage(Graphics g, Component obs, BufferedImage img, Vec2 origin, Point p, double scale, double rotDegrees) {
        Graphics2D g2 = (Graphics2D)g;        
       // scale may fail if the scale factor is a quotient, i.e. 3/4 will lost precision or behave in an undeterminated way if not calculated with doubles or floats
       Vec2 offset = origin.scale(-((double)scale * (double)img.getWidth(obs)), -((double)scale * (double)img.getHeight(obs)));
        offset.yawBy(-rotDegrees);
        // use Graphics2D instances to make those transforms, that produces faster results
        Point offsetPoint = new Point((int)Math.round(offset.x), (int)Math.round(offset.y));
        /*offsetPoint.translate(p.x,p.y);*/
        g2.translate(p.x, p.y);

        AffineTransform x = AffineTransform.getScaleInstance(scale, scale);
        x.rotate(Math.toRadians(rotDegrees));

        // use an observer! updates are then more accurate
        g().drawImage(img, x, obs);
        // translate back to original coords!
        g2.translate(-offsetPoint.x, -offsetPoint.y);
    }
Cheesy

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (149 views)
2017-09-30 02:57:34

theagentd (214 views)
2017-09-26 18:23:31

cybrmynd (301 views)
2017-08-02 12:28:51

cybrmynd (288 views)
2017-08-02 12:19:43

cybrmynd (297 views)
2017-08-02 12:18:09

Sralse (291 views)
2017-07-25 17:13:48

Archive (968 views)
2017-04-27 17:45:51

buddyBro (1094 views)
2017-04-05 03:38:00

CopyableCougar4 (1670 views)
2017-03-24 15:39:42

theagentd (1430 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!