我使用paintComponent()
在JPanel
的背景上绘制gif动画图像。 它显示的GIF,但不动画。 我使用java 1.5,我知道我可以使用带有图标的标签。为什么gif动画在paintComponent()中使用它时没有动画?
有没有人知道为什么以及如何解决它?
private static class CirclePanel extends JPanel {
ImageIcon imageIcon = new ImageIcon(BarcodeModel.class.getResource("verify.gif"));
Point point = f.getLocation();
protected void paintComponent(Graphics g) {
Graphics gc = g.create();
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
g2d.setColor(Color.BLUE);
g2d.drawImage(imageIcon.getImage(), getWidth()/2, getHeight()/2, null);
g2d.drawRect(0, 0, getWidth(), getHeight());
g2d.setStroke(new BasicStroke(10f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
g2d.setFont(g.getFont().deriveFont(Font.BOLD | Font.ITALIC,15f));
g2d.drawString("Wait Please ...",getWidth()/2-imageIcon.getIconHeight()/3,getHeight()/2+imageIcon.getIconHeight()+15);
g2d.dispose();
}
这是gif图像。
被修改:只需添加图像观察者到g2d.drawImage()方法。
g2d.drawImage(imageIcon.getImage(), getWidth()/2, getHeight()/2, this);
可能的重复[在Swing中显示动画BG](http://stackoverflow.com/questions/10836832/show-an-animated-bg-in-swing) – 2012-07-25 12:11:22