Graphics2D and JLabels

Right now I'm in an AP Java class and I don't know the paint method very well. What I want to do is read an image and then flip it horizontally.

This is what I have: a Sprite class which extends JLabel. The sprite contains arrays of ImageIcons based on its current position. I have an ImageIcon called walkRight and walkLeft. What I want to do is read in walkLeft, flip it, and call it walkRight. This is what I am doing so far.

for (int i = 0; i < walkRight.length; i++) {

BufferedImage img = javax.imageio.ImageIO.read(ClassLoader.

getSystemResource(

"Images//" + name + "walkleft" +

(i + 1) +

".GIF"));

walkLeft = new ImageIcon(img);

Graphics2D g2D = ((Graphics2D) img.getGraphics());

g2D.scale( -1, 1);

walkRight = new ImageIcon(img);

}

How am I supposed to use Graphics2D to flip the image horizontaly so I get a mirror image in walkRight?

[955 byte] By [BossOfTheGamea] at [2007-10-2 21:51:37]
# 1

I'm not positive that I know for sure how this works, but I beleive that you need to do this at painting time. I don't know if modifying the graphics object before painting time will cause the image to flip.

also, I think you need to create a new image from scratch, and use the paint method of the old image.

and finally, if you use the transform you've got there, I think you're going to end up with an image in the second quadrant of your plane, which is not what you want. This is what I would do:

walkLeft = new ImageIcon(img);

Image bimg = someComponentOfYourGUI.createImage(walkLeft.getWidth(), walkLeft.getHeight());

Graphics2D g2d = (Graphics2D) bimg.getGraphics().create() // create a new graphics, for safety.

g2d.setTransform(new AffineTransform(-1, 0, 0, 1, walkLeft.getWidth(), 0)); // Flips the axis, but translates so that the image is still in first quadrant.

walkLeft.paintIcon(someComponentOfYourGUI, g2d, x, y); // paints the image of walkLeft to the graphics of bimg

g2d.dispose();

walkRight = new ImageIcon(bimg); // sets bimg to walk right.

I haven't tested that code, but it should give you an idea of how to go about doing this.

- Adam

Message was edited by:

guitar_man_F

guitar_man_Fa at 2007-7-14 1:07:30 > top of Java-index,Security,Cryptography...
# 2

> I'm not positive that I know for sure how this works,

> but I beleive that you need to do this at painting

> time. I don't know if modifying the graphics object

> before painting time will cause the image to flip.

Close, but a better way is to use the AffineTransformOp, which is a subclass of BufferedImageOp, which lets you create a new Image using an AffineTransform on a source Image.

Niceguy1a at 2007-7-14 1:07:30 > top of Java-index,Security,Cryptography...
# 3

> Close, but a better way is to use the

> AffineTransformOp, which is a subclass of

> BufferedImageOp, which lets you create a new Image

> using an AffineTransform on a source Image.

I've never used that before, but it looks like it should work. @OP: If you use the AffineTransformOp (I would recommend doing so), you will need to need to call getImage on the ImageIcon. This method will return an Image object, but the AffineTransformOp requires a BufferedImage.

When you create an ImageIcon from a file name, it typically returns a BufferedImage, but this is NOT guaranteed, and is dependent on the virtual machine implementation. A safer way to do this would be to load your original image into a BufferedImage FIRST, then create an image icon from it. This way, you KNOW it will be a buffered image.

- Adam

guitar_man_Fa at 2007-7-14 1:07:30 > top of Java-index,Security,Cryptography...