我正在尝试旋转a BufferedImage并将其显示在a JLabel(位于内JPanel)中。当前结果产生一个白色正方形,相对于黑色背景旋转了10度,但正方形内部不存在图像。我知道myPicture不是空白,因为myPicture它本身JPanel在未旋转时会正确显示在内。这是代码:int w = myPicture.getWidth(); int h = myPicture.getHeight(); BufferedImage newImage = new BufferedImage(w, h, myPicture.getType());Graphics2D graphic = newImage.createGraphics();graphic.rotate(Math.toRadians(10), w/2, h/2);graphic.drawImage(myPicture, null, 0, 0);picLabel.setIcon(new ImageIcon(newImage));
2 回答

幕布斯6054654
TA贡献1876条经验 获得超7个赞
我解决了自己的问题。问题在于代码:
myPicture.getType()
由于可以放入程序中的图像类型存在很多可变性,因此当您开始绘制到新的BufferedImage中时,结果将是不可预测的。我通过显式设置类型来解决问题,在我的情况下需要
BufferedImage.TYPE_INT_ARGB
因此,完整的语句为:
BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
添加回答
举报
0/150
提交
取消