3

I am creating a little game in Java and I have an image which gets rotated.

enter image description here

As you can see in the two images below, there is a giant ship which slowly rotates in the game, but when it gets to a certain point it gets cut off (due to its own little BufferedImage).

Heres my rendering code:

public void drawImageRotated(BufferedImage img, double x, double y, double scale,    double angle) {
        x -= xScroll;
        y -= yScroll;  
        BufferedImage image = new BufferedImage((int)(img.getWidth() * 1.5D), (int)(img.getHeight() * 1.5D), 2);
        Graphics2D g = (Graphics2D)image.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.rotate(Math.toRadians(angle), image.getWidth() / 2, image.getHeight() / 2);
        g.drawImage(img, image.getWidth() / 2 - img.getWidth() / 2, image.getHeight() / 2 - image.getHeight() / 2, null);
        g2d.drawImage(image, (int)(x-image.getWidth()*scale/2), (int)(y-image.getHeight()*scale/2), (int)(image.getWidth()*scale), (int)(image.getHeight()*scale), null);
        g.dispose();      
 }

Back to the matter at hand, how can i work out the maximum x and y size of an image during rotation so I can compensate with my buffered images size?

Shaun Wild
  • 1,237
  • 3
  • 17
  • 34
  • 1
    Rotating the image will change its size, you need take into account this change. Check out [this](http://stackoverflow.com/questions/12165977/java-image-rotation) and [this](http://stackoverflow.com/questions/4156518/rotate-an-image-in-java) for examples – MadProgrammer Feb 02 '13 at 20:45

4 Answers4

1

how can i work out the maximum x and y size of an image during rotation so I can compensate with my buffered images size?

double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int w = image.getWidth();
int h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin);
int newh = (int)Math.floor(h*cos+w*sin);

The above code was taken from this example: Java(SWING) working with Rotation

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
1

If you have a basically rectangular image which is rotated around its center, the maximum width and height during rotation will be when a diagonal of the image rectangle is horizontal or vertical. This diagonal distance could be computed with the Pythagorean Theorem and used for the width and height of the BufferedImage.

    int size = (int) Math.sqrt((img.getWidth() * img.getWidth()) + (img.getHeight() * img.getHeight()));
    BufferedImage image = new BufferedImage(size, size, 2);
    // The rest of your code as before
Alden
  • 837
  • 1
  • 6
  • 15
0

An alternative is to rotate the actual Graphics object, draw the image, and restore the rotation:

AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(angle), x + image.getWidth() / 2, y + image.getWidth() / 2);
g2d.drawImage(image, x, y, null);
g2d.setTransform(old);
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Can you please go into more depth about what that does? If that's what I think it is, it would rotate everything in my scene, this is not what i'm looking for. – Shaun Wild Feb 02 '13 at 20:42
  • @ShaunWild It does not rotate everything. It only rotates the image you are drawing. – tckmn Feb 02 '13 at 21:12
0

Let's consider width being the width of the original image, height its original height and angle the rotation angle value in radians.

According to my calculations, the size of the rotated image is something like this:

rotatedWidth = Math.cos(angle) * width + Math.sin(angle) * height;
rotatedHeight = Math.sin(angle) * width + Math.cos(angle) * height;

You may also need to take a look at this thread as well, as it may help.

Community
  • 1
  • 1
Dan D.
  • 32,246
  • 5
  • 63
  • 79