Search code examples
javaanimationimage-rotation

Image rotation animation


Perhaps a question with a simple answer. I'm trying to animate a ship (asteroids style). Right now I'm just using the Graphics2D rotate method. However, right now it's just blurring my image. What's the simplest way of going about this and getting it to work correctly? (the getImage method gets called every time the screen refreshes, and the rotate method gets called every time the user presses left or right. bi1 is the bufferedimage read in previously)

public void rotate(double rot) {
    g = bi1.getGraphics();
    Graphics2D g2d=(Graphics2D)g;
    g2d.rotate(rot, 15, 15);
    g2d.drawImage(bi1, 0, 0, null);     
}

public BufferedImage getImage() {   
    return bi1;
}

Solution

  • try it using an affine transformation:

      public void paint(Graphics g) {
          // clear off screen bitmap
          super.paint(g);
          // cast graphics to graphics 2D
          Graphics2D g2 = (Graphics2D) g;
          AffineTransform tfm = new AffineTransform();
          tfm.rotate(0,0,0);
          g2.setTransform(tfm);
          g2.drawImage(backImage, 0, 0, this);
          tfm.rotate(Math.toRadians(player.angle+90), player.x+32, player.y+32);
          g2.setTransform(tfm);
          g2.drawImage(tank, player.x, player.y, this);       
      }
    

    http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/AffineTransform.html