Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: AffineTransform not working right

  1. #1
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default AffineTransform not working right

    AffineTransform not working right. iam not sure what i am missing.

    so iam trying to flip the image.

    i first set up paint method so i can print my image
    public void paint(Graphics g)
    Graphics2D g2d = (Graphics2D) g;

    than i set up AffineTransform
    AffineTransform tx = new AffineTransform();

    than i scale it neg,pos. so it will flip image.
    tx.scale(-1, 1);

    than i set transform

    lastly i am print the image.
    g2d.drawImage(player_image, x, y, x+width, y+height, 
    	50*(int)frame, 147, 50*(int)frame+50, 189 ,Sprite_Sheet.m1);

    it should be flip but its not. any idea what i missed?

  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    My Mood
    Thanked 139 Times in 134 Posts

    Default Re: AffineTransform not working right

    Usually when you try to flip an image like this using a scale transform, you will have to translate it as well. Think of a single image being flipped horizontally. When this happens, the flip occurs about the y axis, and so the image which used to go from 0 to the image's width now goes from 0 to the negative of the image's width. To correct for this, you would need to translate the image over. For example, my slightly larger than an sscce code:

    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    public class FlipViaTransform {
       private static final String SPRITE_SHEET_SPEC = "http://www.funorb.com/img/images/game/"
             + "central/dev_diary/sprite_sheet_full.gif";
       private static final int TIMER_DELAY = 200;
       private static final int SPRITE_ROWS = 8; // an 8 x 8 sprite sheet
       public static void main(String[] args) {
          try {
             URL spriteSheetUrl = new URL(SPRITE_SHEET_SPEC);
             BufferedImage spriteSheet = ImageIO.read(spriteSheetUrl);
             final ImageIcon[] iconsA = new ImageIcon[64];
             final ImageIcon[] iconsB = new ImageIcon[64];
             double wD = (double) spriteSheet.getWidth() / SPRITE_ROWS;
             double hD = (double) spriteSheet.getHeight() / SPRITE_ROWS;
             int w = (int) wD;
             int h = (int) hD;
             AffineTransform at = AffineTransform.getScaleInstance(-1, 1);
             at.translate(-wD, 0);              // *********** the key is here!!! ***********
             for (int i = 0; i < SPRITE_ROWS; i++) {
                for (int j = 0; j < SPRITE_ROWS; j++) {
                   int x = (int) (i * wD);
                   int y = (int) (j * hD);
                   BufferedImage imgA = spriteSheet.getSubimage(x, y, w, h);
                   BufferedImage imgB = new BufferedImage(imgA.getWidth(),
                         imgA.getHeight(), BufferedImage.TYPE_INT_ARGB);
                   Graphics2D g2 = imgB.createGraphics();
                   g2.drawImage(imgA, 0, 0, null);
                   iconsA[j * SPRITE_ROWS + i] = new ImageIcon(imgA);
                   iconsB[j * SPRITE_ROWS + i] = new ImageIcon(imgB);
             final JLabel labelA = new JLabel("Image");
             final JLabel labelB = new JLabel("Mirror Image");
             final JPanel panel = new JPanel(new GridLayout(1, 0));
             Timer spriteTimer = new Timer(TIMER_DELAY, new ActionListener() {
                int spriteIndex = 0;
                public void actionPerformed(ActionEvent arg0) {
                   spriteIndex %= iconsA.length;
             JOptionPane.showMessageDialog(null, panel, "AffineTransform Example", JOptionPane.PLAIN_MESSAGE);
          } catch (MalformedURLException e) {
          } catch (IOException e) {

    The key bit of code is the second line below:

             AffineTransform at = AffineTransform.getScaleInstance(-1, 1);
             at.translate(-wD, 0);              // *********** the key is here!!! ***********

Similar Threads

  1. Something i've been working on.
    By jbrower95 in forum Java IDEs
    Replies: 0
    Last Post: July 17th, 2012, 04:45 PM
  2. NOT WORKING!!!!!
    By zezverige in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 19th, 2012, 10:30 AM
  3. Working with $
    By whome in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 11th, 2012, 03:00 PM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM