Sign in to follow this  

Flip sprite in sprite sheet using Graphics2D

This topic is 1105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am using Java Graphics2D to make my game.I haven't worked with it for a while, and I don't know how to do this..

I got a sprite sheet (that has multiple sprites on it). I know how to draw a sprite (using this https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawImage(java.awt.Image,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver) )... But I need to flip (horizontally or/and vertically) only one sprite on the image, not the whole image.

How can I do this?

Thanks,

MatejaS

Share this post


Link to post
Share on other sites

 

Use the scaling parameters of the drawImage method to and scale it by a negative amount on the axis you want:

g2.drawImage(image, x, y, -width, height, null);

 

+1  This will work to mirror.  If you need to rotate the image use an Affine Transform.

 

http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/RotatingimageusingJava2DAffineTransformclass.htm

Share this post


Link to post
Share on other sites

This topic is 1105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this