gpt4 book ai didi

java - 图像不旋转

转载 作者:行者123 更新时间:2023-11-29 03:31:03 25 4
gpt4 key购买 nike

enter image description here enter image description here我正在尝试旋转图像,有些效果,但问题是它无法正常工作。它没有按照我想要的精确旋转。图像以某种混合形式显示。

我的按钮点击代码:

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);

}

知道这段代码有什么错误吗?这里 a 是从图像堆栈加载的缓冲图像,label 是用于显示图像的 JLabel

最佳答案

您正在覆盖用作源的图像 (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);
}

关于java - 图像不旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18274816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com