Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Cant get AffineTransform to do what i want  (Read 1978 times)
0 Members and 1 Guest are viewing this topic.
Offline 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:
Offline IronclawsBt

Junior Duke


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
Offline HighFlyCoder

Senior Newbie





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

You sir or ma'am, ARE A GOD
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline IronclawsBt

Junior Duke


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
Offline 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
Offline 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
Offline delt0r

JGO Knight


Medals: 27
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (2 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
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!