我想旋转我的gif图像90度并将其显示在一个框架中。我曾尝试在内置类中使用 AffineTransform() 和 AffineTransformOP() 。它的适用于非动画图像,但不适用于gif图像。
我还查看了链接Rotate GIF images in java及其评论,它从未帮助我解决我的问题。
所以,请帮助我。
答案 0 :(得分:0)
你可以设置正确的affineTransform,但你一定要小心!
public static void main(String[] args) throws MalformedURLException {
File file = new File("img/ani.gif");
URL url = file.toURI().toURL();
Icon icon = new ImageIcon(url);
final AffineTransform rot = AffineTransform.getRotateInstance(0.2);
JLabel label = new JLabel(icon){
@Override
public void paint(Graphics arg0) {
Graphics2D gr = (Graphics2D) arg0;
AffineTransform original = gr.getTransform();
gr.setTransform(rot);
super.paint(arg0);
gr.setTransform(original);
}
};
JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
在Label开始任何绘图之前,我们得到Graphics并设置affinetrans(rot 0.2) 然后,图形有这个'设置'来绘制动画。 绘图后我们返回'原始'仿射变换。 你应该能够在其他组件上做到这一点......
警告:永远不应该使用此方法在现有变换之上应用新的坐标变换,因为Graphics2D可能已经具有其他目的所需的变换,例如渲染Swing组件或应用缩放变换来调整用于解决打印机问题。