我正在尝试旋转图像,有点可行,但问题是它无法正常工作。它不是我想要的旋转。图像以某种混合形式显示。
我点击按钮的代码:
RT90.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
degrees+=90;
rotateIMG(degrees);
repaint();
}
});
rotateIMG()代码:
public void rotateIMG(double d)
{
BufferedImage b ;
b=a;
Graphics g;
g=b.createGraphics();
Graphics2D g2d = (Graphics2D)g;
System.out.println(b.getWidth());
System.out.println(b.getHeight());
g2d.rotate(Math.toRadians(d), b.getWidth()/2, b.getHeight()/2);
g2d.drawImage(b,0,0,null);
ImageIcon rtimg = new ImageIcon(b);
label.setIcon(rtimg);
}
知道这段代码中的wrong
是什么?
这里a
是缓冲图像,从图像堆栈加载,label
是JLabel
,用于显示图像。
答案 0 :(得分:2)
您正在覆盖用作源的图像(b == a)。你需要创建一个新的。
public void rotateIMG(double d) {
// Consider also using GraphicsConfiguration.createCompatibleImage().
// I'm just putting here something that should work
BufferedImage b = new BufferedImage(a.getHeight(), a.getWidth(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = b.createGraphics();
g2d.rotate(Math.toRadians(d), a.getWidth()/2, a.getHeight()/2);
// Note the a instead of b here
g2d.drawImage(a, 0, 0, null);
// Do you want to keep the old a or not?
// a = b;
ImageIcon rtimg = new ImageIcon(b);
label.setIcon(rtimg);
}
答案 1 :(得分:2)
问题是图像的某些部分被裁剪
结帐Rotated Icon。它会在不同程度旋转时计算图标的正确尺寸。