Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) 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 4795 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 Wizard

Medals: 143
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

 DesertCoockie (22 views) 2018-05-13 18:23:11 nelsongames (74 views) 2018-04-24 18:15:36 nelsongames (68 views) 2018-04-24 18:14:32 ivj94 (751 views) 2018-03-24 14:47:39 ivj94 (81 views) 2018-03-24 14:46:31 ivj94 (605 views) 2018-03-24 14:43:53 Solater (97 views) 2018-03-17 05:04:08 nelsongames (178 views) 2018-03-05 17:56:34 Gornova (388 views) 2018-03-02 22:15:33 buddyBro (1048 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-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