Search code examples
javarotationappletbufferedimagegraphics2d

Why does my Image not rotate?


This code displays the logo, but there's no rotation. Am I missing something? I've been trying to rotate for a long time. Everything displays fine, and the calculations are correct, but the image displays as a normal image

    import javax.swing.JApplet;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Font;
import java.awt.image.*;
import java.awt.Image;
import java.awt.geom.*;
import java.awt.Color;
import java.io.*;
import javax.imageio.ImageIO;


public class Display extends Applet
{
static Image logo;
static double rotation;


public void init()
{
    logo = getImage( getCodeBase() , "logo.jpg" );
    rotation = 90;
    setBackground(Color.WHITE);

}

public void paint(Graphics g)
{
    g.drawImage( rotateGear(this), 0 , 0 , this);

}

public static Image rotateGear(ImageObserver o)
{
    BufferedImage bf = new BufferedImage( 400 , 400 , BufferedImage.TYPE_INT_ARGB );
    Graphics2D g2d = bf.createGraphics();
    g2d.drawImage(logo, 0, 0, o);
    g2d.setColor(Color.red);
    g2d.fillRect(390, 390, 10, 10);
    AffineTransform at = new AffineTransform();
    at.rotate(rotation, 178, 178);
    g2d.setTransform(at);
    System.out.println( Math.toRadians(rotation));
    return bf;


}
}

Solution

  • Because you're setting the transform after you have drawn the image. Do it the other way around!

    (I tried it and your code works fine otherwise.)