2

I have a method getImage() that needs to rotate an Image, store it in a new variable, then return that new Image. Here is my attempt, the image appears to be empty or something. It just does not show up on the screen:

public Image getImage() {
    buffImage.createGraphics().rotate(direction);
    return buffImage;
}

When I take out the buffImage.createGraphics().rotate(direction); the image draws on the screen just fine with no problems but of course, not rotated.

chrispytoes
  • 1,714
  • 1
  • 20
  • 53
  • `rotate` sets up the translation matrix, so that when you paint something to the `Graphics` context, it will "rotated" by the specified amount, it won't rotate the existing image – MadProgrammer May 13 '15 at 01:31
  • This [example](http://stackoverflow.com/questions/20275424/rotating-image-with-affinetransform/20280225#20280225) generates a rotated version of the source image, but it also ensures that the resulting image size is capable of fitting the newly rotated image. – MadProgrammer May 13 '15 at 01:34
  • This [example](http://stackoverflow.com/questions/21841582/getting-the-right-image-observer-for-rotating-an-image/21841683#21841683) rotates an image within another `Graphics` context, which might be simpler, but that depends on what is you're trying to achieve... – MadProgrammer May 13 '15 at 01:35
  • IMO, you need to draw the image again after rotate. – akhil_mittal May 13 '15 at 01:35
  • Here is an example: http://sanjaal.com/java/401/java-graphics-2d/rotating-an-image-in-java-graphics-2d-by-specified-degree/ – akhil_mittal May 13 '15 at 01:35
  • Also, rotation, by default occurs at position 0x0, which may not be what you want, instead, you may need to specify a "anchor" position around which the rotation can occur, like the centre of the image... – MadProgrammer May 13 '15 at 01:36
  • @akhil_mittal With the way I have this set up, I can't draw the image with this method, I draw it somewhere else. I simply need this method to rotate an image, and return the rotated image. – chrispytoes May 13 '15 at 01:46
  • @chrispytoes Then take a look at the two examples I linked previously, which demonstrate two ways to rotate an image, either by generating a new image or rotating the final `Graphics` context... – MadProgrammer May 13 '15 at 02:07
  • @MadProgrammer This is where that got me, it still does the same thing as before, am I on the right track here? ` public Image getImage() { Graphics2D g2d = buffImage.createGraphics(); AffineTransform at = new AffineTransform(); at.rotate(direction); g2d.setTransform(at); g2d.dispose(); return buffImage; }` – chrispytoes May 13 '15 at 02:26
  • 1- You're not rotating around the centre of the image, you're rotating around the `0x0` point; 2- You're not providing a new image, adjusted to the size required to contain the newly rotated image; 3- You're not painting the original image to the "rotated" image context... – MadProgrammer May 13 '15 at 02:28
  • @MadProgrammer I know how to do 1. The other 2 I cannot seem to find, I'm new to using Graphics2D. Is there any way you can give me the exact code I need to use for what I need? I looked through both the examples thoroughly and tried everything I could and I can't seem to figure it out. – chrispytoes May 13 '15 at 02:50

2 Answers2

3

So, based on the example in this answer, you should be able to devise a rotation method which can rotate a source image by a given number of degrees, for example...

  // Make sure you actually load some image and assign it to this
  // variable, otherwise you will have a NullPointerException to 
  // deal with
  private BufferedImage source;

  public Image rotateBy(double degrees) {

    // The size of the original image
    int w = source.getWidth();
    int h = source.getHeight();
    // The angel of the rotation in radians
    double rads = Math.toRadians(degrees);
    // Some nice math which demonstrates I have no idea what I'm talking about
    // Okay, this calculates the amount of space the image will need in
    // order not be clipped when it's rotated
    double sin = Math.abs(Math.sin(rads));
    double cos = Math.abs(Math.cos(rads));
    int newWidth = (int) Math.floor(w * cos + h * sin);
    int newHeight = (int) Math.floor(h * cos + w * sin);

    // A new image, into which the original can be painted
    BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = rotated.createGraphics();
    // The transformation which will be used to actually rotate the image
    // The translation, actually makes sure that the image is positioned onto
    // the viewable area of the image
    AffineTransform at = new AffineTransform();
    at.translate((newWidth - w) / 2, (newHeight - h) / 2);

    // And we rotate about the center of the image...
    int x = w / 2;
    int y = h / 2;
    at.rotate(rads, x, y);
    g2d.setTransform(at);
    // And we paint the original image onto the new image
    g2d.drawImage(source, 0, 0, null);
    g2d.dispose();

    return rotated;
  }
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You could just use a Rotated Icon and display the Icon in a JLabel.

The Rotated Icon is a reusable class so you don't need to worry about about adding the rotation code to every class where you need this functionality.

camickr
  • 321,443
  • 19
  • 166
  • 288