Share this page 

Rotate an imageTag(s): AWT


The following snippet rotates an image (90 degrees). The applet assumes the dimension 32x32 for the image. You may want to grap this image for testing purpose.
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.net.*;

public class RotateGumby extends Applet {
  Image img = null;
  Image rot = null;

  int buffer[] = new int[32 * 32];
  int rotate[] = new int[32 * 32];

  public void init() {
   try {
      MediaTracker tracker = new MediaTracker (this);
      img = getImage(new URL(getDocumentBase(), "gumby.gif"));
      tracker.addImage (img, 0);
      tracker.waitForAll();
      PixelGrabber grabber =
        new PixelGrabber(img, 0, 0, 32, 32, buffer, 0, 32);
        try {
        grabber.grabPixels();
          }
      catch(InterruptedException e) {
         e.printStackTrace();
         }
      for(int y = 0; y < 32; y++) {
        for(int x = 0; x < 32; x++) {
          rotate[((32-x-1)*32)+y] = buffer[(y*32)+x];
          }
        }
      rot = createImage(new MemoryImageSource(32, 32, rotate, 0, 32));
      }
   catch (Exception e) {
      e.printStackTrace();
      }
   }

  public void update( Graphics g) {
   paint(g);
   }

  public void paint(Graphics g) {
     g.drawImage(img, 0, 0,this);
     g.drawImage(rot,0, 40, this);
    }
}
The next example will rotate a picture 5 degrees at a time. We are using the Java2D package (and Swing).
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class RotatePanel extends JPanel {
   private Image image;
   private double currentAngle;

   public RotatePanel(Image image) {
     this.image = image;
     MediaTracker mt = new MediaTracker(this);
     mt.addImage(image, 0);
     try {
       mt.waitForID(0);
     }
     catch (Exception e) {
       e.printStackTrace();
     }
   }

   public void rotate() {
     //rotate 5 degrees at a time
     currentAngle+=5.0;
     if (currentAngle >= 360.0) {
       currentAngle = 0;
     }
     repaint();
   }
   protected void paintComponent(Graphics g) {
     super.paintComponent(g);
     Graphics2D g2d = (Graphics2D)g;
     AffineTransform origXform = g2d.getTransform();
     AffineTransform newXform = (AffineTransform)(origXform.clone());
     //center of rotation is center of the panel
     int xRot = this.getWidth()/2;
     int yRot = this.getHeight()/2;
     newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
     g2d.setTransform(newXform);
     //draw image centered in panel
     int x = (getWidth() - image.getWidth(this))/2;
     int y = (getHeight() - image.getHeight(this))/2;
     g2d.drawImage(image, x, y, this);
     g2d.setTransform(origXform);
   }

   public Dimension getPreferredSize() {
     return new Dimension (image.getWidth(this), image.getHeight(this));
   }


   public static void main(String[] args) {
     JFrame f = new JFrame();
     Container cp = f.getContentPane();
     cp.setLayout(new BorderLayout());
     Image testImage =
         Toolkit.getDefaultToolkit().getImage("c:/temp/gumby.gif");
     final RotatePanel rotatePanel = new RotatePanel(testImage);
     JButton b = new JButton ("Rotate");
     b.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae) {
           rotatePanel.rotate();
       }
     });
     cp.add(rotatePanel, BorderLayout.CENTER);
     cp.add(b, BorderLayout.SOUTH);
     f.pack();
     f.setVisible(true);
   }
}