I use paintComponent() to draw a gif-animated image in the JPanel backgound. It displays a gif, but does not animate. I am using java 1.5 and I know that I can use the icon shortcut.
Does anyone know why and how to fix it?
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(); }
This is a gif image.

Edited: just add an image observer to the g2d.drawImage () method.
g2d.drawImage(imageIcon.getImage(), getWidth() / 2, getHeight() / 2, this);
itro
source share