为什么我的图像不旋转?
Why does my Image not rotate?
此代码显示徽标,但没有旋转。我错过了什么吗?我一直在尝试旋转很长时间。一切正常,计算正确,但图像显示为正常图像
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;
}
}
因为您是在绘制图像之后设置变换。反过来做!
(我试过了,你的代码在其他方面工作正常。)
此代码显示徽标,但没有旋转。我错过了什么吗?我一直在尝试旋转很长时间。一切正常,计算正确,但图像显示为正常图像
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;
}
}
因为您是在绘制图像之后设置变换。反过来做!
(我试过了,你的代码在其他方面工作正常。)