Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Cant get AffineTransform to do what i want  (Read 2241 times) 0 Members and 1 Guest are viewing this topic.
HighFlyCoder

Senior Newbie

 « Posted 2011-03-17 00:20:00 »

Ok well i was playing around with images and attempted to create an isometric tile basically by rotating the image 45 degrees and scaling it's y by 50%, sadly the original image would scale by half instead of when it is rotated, here is the code

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35 `package isometrictest;import javax.swing.*;import java.awt.*;import java.awt.geom.AffineTransform;import java.io.File;import javax.imageio.ImageIO;public class GUI extends JFrame{    Image tile;    JPanel pane;    public GUI(){        super("Isometric Test");        try{        tile = ImageIO.read(new File("C:/Users/Yosef/Documents/NetBeansProjects/isometricTest/grasstxt2.png"));        }catch(Exception ae){            ae.printStackTrace();        }        pane = new JPanel();        getContentPane().add(pane);    }    @Override    public void paint(Graphics g){        Graphics2D g2 = (Graphics2D)g;        AffineTransform tf = new AffineTransform();        //AffineTransform tx = new AffineTransform();        tf.translate(300, 0);        tf.rotate(Math.toRadians(45));        tf.scale(1d, 0.5d);        g2.drawImage(tile, tf, pane);    }}`

And this is what is happening:
IronclawsBt

Junior Devvie

Medals: 1

 « Reply #1 - Posted 2011-03-17 00:46:18 »

Try reversing the order of your scale and rotate.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
HighFlyCoder

Senior Newbie

 « Reply #2 - Posted 2011-03-17 01:59:46 »

You sir or ma'am, ARE A GOD
IronclawsBt

Junior Devvie

Medals: 1

 « Reply #3 - Posted 2011-03-17 02:08:31 »

When concatenating AffineTransforms, just remember that they are applied to the object in the opposite order as you put them together. If you want to rotate the sprite, scale it, then translate it, you want to construct it by AffineTransform.translate().scale().rotate().

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
HighFlyCoder

Senior Newbie

 « Reply #4 - Posted 2011-03-17 04:05:27 »

ahh thanks, also im having a bit of an issue with the AffineTransform.scale() method...it seems to push the image up for no real reason. i know you can use the graphics2D class to scale but is there another way. Or maybe a way to get these displacments to be predictable?

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47 `package isometrictest;import javax.swing.*;import java.awt.*;import java.awt.geom.AffineTransform;import java.io.File;import javax.imageio.ImageIO;public class GUI extends JFrame{    Image tile;    JPanel pane;    int width, height;    int imgX = 200;    int imgY = 200;    int imgX2 = 500; //This is just for displacment of the second image to show you what i mean    public GUI(){        super("Isometric Test");        try{        tile = ImageIO.read(new File("C:/Users/Yosef/Documents/NetBeansProjects/isometricTest/grasstxt2.png"));        width = tile.getWidth(pane);        height = tile.getHeight(pane);        System.out.println("Image Width: "+width + "\n"+"Image Height: " + height);        }catch(Exception ae){            ae.printStackTrace();        }        pane = new JPanel();        getContentPane().add(pane);    }    @Override    public void paint(Graphics g){        Graphics2D g2 = (Graphics2D)g;        AffineTransform tf = new AffineTransform(); //First image's AffineTransform        AffineTransform tx = new AffineTransform(); //Second image's AffineTransform        tf.scale(1, 1);        tf.rotate(Math.toRadians(45), imgX+width/2, imgY+height/2);        tf.translate(imgX, imgY);        g2.drawImage(tile, tf, pane);        tx.scale(1, 0.5);        tx.rotate(Math.toRadians(45), imgX2+width/2, imgY+height/2);        tx.translate(imgX2, imgY);        g2.drawImage(tile, tx, pane);    }}`

Here is a visual picture of what is happening, the left one is without a scale and the second one has a 0.5 scale on the Y axis
SimonH
 « Reply #5 - Posted 2011-03-17 05:15:40 »

This will do what you want (assuming you want the center of the scaled, rotated image at imgX,imgY)
 1  2  3  4 `        tx.translate(imgX-width/2, imgY-height/2+height/4);          tx.scale(1,0.5);        tx.rotate(Math.toRadians(45),width/2, height/2);        g2.drawImage(tile, tx, pane);`

This is a fiddle which will only work in this case, ie: if you want isometric.
Read up on AffineTransform if you need to do more!

People make games and games make people
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #6 - Posted 2011-03-17 09:28:57 »

Scaling moves *all* points closer to the origin, the 0,0 point. So if the image is not centered the way you want, it will also "move" it closer to 0,0

I have no special talents. I am only passionately curious.--Albert Einstein
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Gibbo3771 (7 views) 2015-05-29 06:54:17 Ralphanese (19 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (35 views) 2015-05-27 22:21:08 CopyableCougar4 (25 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18
 Rayvolution 24x theagentd 20x Spasi 19x Riven 18x BurntPizza 17x Drenius 15x ra4king 13x EgonOlsen 12x princec 12x orangepascal 12x opiop65 12x DavidBVal 11x Husk 11x KevinWorkman 10x kevglass 9x scanevaro 8x
 Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02
 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