Results 1 to 2 of 2
  1. #1
    VortexSpin is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default Rotating Buffered Image distorts image

    SOLVED: Changed the program to save a copy of the original, unrotated image at the beginning of execution. Then I made each rotation an absolute rotation from 0 degrees instead of incremental 1 degree rotations. (So if the player hits the key five times, on the fifth rotation the sprite is shifting from the original position 5 degrees instead of rotating five times by one degree a piece.) The sprite stays intact.

    I have a perfectly square PNG image that I am trying to rotate with keyboard input for a simple game.

    This is what I'm doing:

    Upon the key press:

    Java Code:
    player.rotate(player.getImage(), 5);
    This fetches the BufferedImage from the player object and passes it to my generic rotate method:

    Java Code:
    public void rotate(BufferedImage img, int angle) {
            int w = img.getWidth();
            int h = img.getHeight();
            BufferedImage dimg = dimg = new BufferedImage(w, h, img.getType());
            Graphics2D g = dimg.createGraphics();
            g.rotate(Math.toRadians(angle), w / 2, h / 2);
            g.drawImage(img, null, 0, 0);
            sprite = dimg;
    I then paint with:

    Java Code:
    backGFX.drawImage(player.getImage(), player.getX(), player.getY(), null);

    This is what I'm getting:

    BEFORE: imgur: the simple image sharer
    AFTER (about 40 degrees): imgur: the simple image sharer

    Any ideas on how to beat this?
    Last edited by VortexSpin; 02-13-2011 at 12:22 AM. Reason: Solved

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    Firstly, when you create a graphics context, you need to dispose of it when you're done by calling g.dispose(). Also, you have an extraneous "= dimg" on the 4th line. That's not you're problem though. It's possible that your drawImage command is causing the problem. You are using the drawImage that expects a BufferedImageOp as the 2nd parameter, and you are passing null. The API does not define what happens if you pass null. I'm guessing you meant to use

    Java Code:
    g.drawImage(img, 0, 0, null);
    If that isn't it, then it may be that you're failing to clear the clip rectangle associated with the sprite before redrawing it, and so you're seeing several sprites at different angles all drawn on top of eachother.

Similar Threads

  1. rotating a rectangular image
    By ighor10 in forum Java 2D
    Replies: 1
    Last Post: 10-24-2010, 08:22 PM
  2. Rotating image based on the direction of a line
    By YellowPainting in forum New To Java
    Replies: 4
    Last Post: 08-06-2010, 09:32 AM
  3. Rotating an image
    By lackofcolor in forum Java 2D
    Replies: 3
    Last Post: 02-28-2009, 12:54 AM
  4. Rotating Image?
    By sciguy77 in forum Java Applets
    Replies: 9
    Last Post: 02-17-2009, 02:47 AM
  5. Rotating and flipping an image in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts