Skip to main content
GameDev.net gamedev.net
🔒 Locked

Flip sprite in sprite sheet using Graphics2D

Started by MatejaS Dec 7, 2014 at 12:08 PM 4 replies 7.1k views
Original Post
MatejaS
MatejaS

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

MatejaS
MatejaS

Anyone please??

ArrTaffer
ArrTaffer

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);
Nicholas Kong
Nicholas Kong

If you want to flip horizontally or flip vertically, just edit the image in the program called "Paint". It can flip horizontal or vertically images and then save as another image file.

Glass_Knife
Glass_Knife

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.