Fliping image horizontally

Hi guys, i need help, im a java newbi and i have been trying this for days now and with out any luck.
I need to flip an image horizontally about its x axis. I tried to negative the width (-image.getWidth())
but i get really wierd results, the colour gets all messed up.

Im using Accelerated Java 2D, based on this tutorial found here.
http://www.cokeandcode.com/node/6

I just need to figure out to flip images that are Accelerated. See the tutorial for more info.
thx for your help guys, i really appreciate it.

Hm. Well, you can flip horizontally with that drawImage method that takes two rect coords.

javadoc

Nope, tried that already, the colour gets all messed up again.

Colors were fine here with that. I used that technique in JM4k.

I’ve seen the problem before on windows using NVidia drivers. I believe it was in 1.5.0, but I’m not sure. On the same system I’ve used AffineTransforms that worked quite well, so I’m really not sure what the problem might be. There is something called java.awt.AlphaComponsite , it could have something to do with that. I think that was one of the differences between the case when it worked and when it didn’t.

So theres nothing i can do about this? I am using a nividia driver. I really need help with this.

try this:


g.drawImage(myImage, AffineTransform.getScaleInstance(1,-1), null);

if it doesn’t work, please provide your source code, so we can have a closer look.

I cant i get an error message.
G:\JFiles\WorkingTitle\Sprite.java:55: cannot find symbol
symbol : method drawImage(java.awt.Image,java.awt.geom.AffineTransform,)
location: class java.awt.Graphics
g.drawImage(image,AffineTransform.getScaleInstance(1,-1),null);
^

This is in the Sprite class.


public class Sprite {
	/** The image to be drawn for this sprite */
	private Image image;
	
	/**
	 * Create a new sprite based on an image
	 * 
	 * @param image The image that is this sprite
	 */
	public Sprite(Image image) {
		
		this.image = image;
	}
	
	/**
	 * Get the width of the drawn sprite
	 * 
	 * @return The width in pixels of this sprite
	 */
	public int getWidth() {
		return image.getWidth(null);
	}

	/**
	 * Get the height of the drawn sprite
	 * 
	 * @return The height in pixels of this sprite
	 */
	public int getHeight() {
		return image.getHeight(null);
	}
	
	/**
	 * Draw the sprite onto the graphics context provided
	 * 
	 * @param g The graphics context on which to draw the sprite
	 * @param x The x location at which to draw the sprite
	 * @param y The y location at which to draw the sprite
	 */
	public void draw(Graphics g,int x,int y) {
		
		g.drawImage(image,x,y,image.getWidth(null),image.getHeight(null),null);
	   	
	}
}




This, the Sprite storage


import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

/**
 * A resource manager for sprites in the game. Its often quite important
 * how and where you get your game resources from. In most cases
 * it makes sense to have a central resource loader that goes away, gets
 * your resources and caches them for future use.
 * <p>
 * [singleton]
 * <p>
 * @author Kevin Glass
 */
public class SpriteStore {
	/** The single instance of this class */
	private static SpriteStore single = new SpriteStore();
	
	/**
	 * Get the single instance of this class 
	 * 
	 * @return The single instance of this class
	 */
	public static SpriteStore get() {
		return single;
	}
	
	/** The cached sprite map, from reference to sprite instance */
	private HashMap sprites = new HashMap();
	
	/**
	 * Retrieve a sprite from the store
	 * 
	 * @param ref The reference to the image to use for the sprite
	 * @return A sprite instance containing an accelerate image of the request reference
	 */
	public Sprite getSprite(String ref) {
		// if we've already got the sprite in the cache

		// then just return the existing version

		if (sprites.get(ref) != null) {
			return (Sprite) sprites.get(ref);
		}
		
		// otherwise, go away and grab the sprite from the resource

		// loader

		BufferedImage sourceImage = null;
		
		try {
			// The ClassLoader.getResource() ensures we get the sprite

			// from the appropriate place, this helps with deploying the game

			// with things like webstart. You could equally do a file look

			// up here.

			URL url = this.getClass().getClassLoader().getResource(ref);
			
			if (url == null) {
				fail("Can't find ref: "+ref);
			}
			
			// use ImageIO to read the image in

			sourceImage = ImageIO.read(url);
		} catch (IOException e) {
			fail("Failed to load: "+ref);
		}
		
		// create an accelerated image of the right size to store our sprite in

		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
		
		// draw our source image into the accelerated image

		image.getGraphics().drawImage(sourceImage,0,0, sourceImage.getWidth(),sourceImage.getHeight(),null);
		
		// create a sprite, add it the cache then return it

		Sprite sprite = new Sprite(image);
		sprites.put(ref,sprite);
		
		return sprite;
	}
	
	/**
	 * Utility method to handle resource loading failure
	 * 
	 * @param message The message to display on failure
	 */
	private void fail(String message) {
		// we'n't available
		// we dump the message and exit the game

		System.err.println(message);
		System.exit(0);
	}
}


Oh, I didn’t mean to say there’s nothing to do about this. On the contrary, it worked on my computer though I didn’t record the change in circumstances that made it work. So there exists a solution :slight_smile:

oh, and the above errorMSG likely stems from the Graphics object not being cast to Graphics2D.


import java.awt.Graphics2D

....
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(...)
....

I believe that the messed-up colors thing is a bug which should have been fixed since 5.0.

There’s pleny of samples on the net on how to do this…

For example, run Java2Demo from the jdk demo/jfc directory and go to Transform tab. Check out
the duke image being flipped when “Scale” button is selected.
Or go to Images tab. The source for the demo is in the same dir.

Thanks,
Dmitri