lets say i am print a image on screen. with (x1,y1,x2,y2,sx1,xy1,sx2,sy2)

g.drawImage(player_image, x, y, x+width, y+height
        0, 0, 30, 20 ,Sprite_Sheet.m1);

so how would i flipthis image vertically.

4 Years
Discussion Span
Last Post by hwoarang69

Graphics2D (the actual class of the Graphics you draw on) supports a huge set of transforms that modify how subsequent draw operations work. So, for eaxmple the g.rotate method causes all subsequent draws to be rotated by the specified angle. Similarly you can set a scale transform that scales subsequent drawing - and here's the bit you were waiting for - a negative scale factor reflects the image in the correwsponding axis, eg g.scale(1.0, -1.0) keeps the size the same but reflects the drawing vertically. You will probably have to shift the origin of your Graphics2D to keep the reflected image in the right place.
See the Graphics2D API doc for details, the AffineTransform API for the maths behind it.


just want to make sure.
so i should use the:

Graphics2D g2 

than roate the image


than scale the image

 g.scale(1.0, -1.0)

than print image with g2

 g2.drawImage(player_image, x, y, x+width, y+height
        0, 0, 30, 20 ,Sprite_Sheet.m1);
  1. To flip the image you need a negative scale, rotating it looks different (just get a piece of poper and flip or rotate it yurself).
  2. It's not the image that you're flipping/rotating here. The way this works is that you apply a transform to the Graphics itself. Whatever you draw onto the Graphics is passed through the transform before being drawn, so if you apply a g.scale transform everything you draw afterthat will be drawn upside down. Ypour original image is not changed or affected.

Edited by JamesCherrill


AffineTransform tx = AffineTransform.getScaleInstance(1, -1);

tx.translate(0, 0); //translate to center


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.