【发布时间】:2015-11-28 17:13:04
【问题描述】:
我的代码每次“更新”将图像旋转 0.4 度,图像在 [-10,+10] 范围内连续旋转。 问题是缓冲图像在旋转时在边缘被切断,似乎旋转改变了 bufferedImage 需要的大小但大小永远不会更新,有什么想法可以让它工作吗?
protected double rotation = 0;
private double rotDir = 0.4;
private BufferedImage getRotatedSprite(){
if(Math.abs(rotation)>10)
rotDir=rotDir*(-1);
rotation+=rotDir;
ImageIcon icon = new ImageIcon(bImage);
BufferedImage bImg = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d= (Graphics2D)bImg.getGraphics();
g2d.rotate(Math.toRadians(rotation), icon.getIconWidth()/2, icon.getIconHeight()/2);
g2d.drawImage(bImage, 0, 0, null);
return bImg;
}
public void drawSprite(Graphics g) {
BufferedImage image = getRotatedSprite();
if(customWidth != -1 && customHeight != -1){
g.drawImage(image, (int)locX, (int)locY, customWidth, customHeight, this);
}else
g.drawImage(image, (int)locX, (int)locY, this);
}
【问题讨论】:
-
如果你对数学感兴趣,可以看看这个example
标签: java swing rotation bufferedimage graphics2d