Rotating image using Java 2D AffineTransform class : Tranformation « 2D Graphics « Java Tutorial






import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import javax.swing.JPanel;

class ImagePanel extends JPanel {
  int offset = 5;

  private Image image;

  private int angle;

  private int w, h;

  private AffineTransform transform;

  public ImagePanel(Image i) {
    image = i;
    w = image.getWidth(this);
    h = image.getHeight(this);
    transform = new AffineTransform();
  }

  public void paintComponent(Graphics grp) {
    Rectangle rect = this.getBounds();
    Graphics2D g2d = (Graphics2D) grp;
    transform.setToTranslation((rect.width - w) / 2,
        (rect.height - h) / 2);

    transform.rotate(Math.toRadians(angle), w / 2,
        h / 2);

    g2d.drawImage(image, transform, this);
  }

  public void rotate() {
    angle -= offset;
    if (angle <= 0) {
      angle = 360;
    }
    repaint();
  }
}








16.3.Tranformation
16.3.1.Transform LabTransform Lab
16.3.2.Coordinate Translation.
16.3.3.Rotation and coordinate translation
16.3.4.Rotate a line of character (String)
16.3.5.Scaling an object
16.3.6.Tranformation with AffineTransform.getScaleInstanceTranformation with AffineTransform.getScaleInstance
16.3.7.Tranformation with AffineTransform.getTranslateInstanceTranformation with AffineTransform.getTranslateInstance
16.3.8.Tranformation with AffineTransform.getShearInstanceTranformation with AffineTransform.getShearInstance
16.3.9.Calculate Rotation Transform with Math.PICalculate Rotation Transform with Math.PI
16.3.10.Rotating image using Java 2D AffineTransform class
16.3.11.Translate and rotate all objects
16.3.12.Move and scale graphical objects with a mouse on the panel
16.3.13.Rotates a shape about the specified coordinates.