Search code examples
javaimage-processingimage-manipulation

How to render tilted images in Java?


My problem is I have a Java application that renders an image. Now I want to render a tilted image.

One straightforward approach would be to have a tilted image and then render it, but my aim is to tilt the image according to the mouse movement in the window.

One possible solution for this would be to have multiple images at various tilted angles and render them as the mouse moves, but such an implementation is limited in precision and not quite the thing I want.

Is there a way to render the tilted image on the fly?

Thanks in advance.


Solution

  • I think you need to look into AffineTransform it has a method for rotating a shape, as well as other transforms.

    An example of how it can be used can be found here

    Basically:

    AffineTransform at = AffineTransform.getTranslateInstance(width, height); //Create the AffineTransform instance
    at.rotate(someRadianValue); // Apply the transforms you wish
    g2.draw(at.createTransformedShape(myShape)); // Draw the transformed shape