Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 2.50 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Multiple instances of the same animated GIF in a Canvas (Java)
  2. public Tnt(int x, int y){
  3.     this.x = x;
  4.     this.y = y;
  5.     ImageIcon ii = new ImageIcon("src/main/resources/modelObjects/tnt.gif");
  6.     image = ii.getImage();
  7.     image.flush();
  8. }
  9.        
  10. public void draw(Graphics2D g2d, JPanel board){
  11.     for(Tnt tnt: listTnt){          
  12.         g2d.drawImage(tnt.getImage(), tnt.getX(), tnt.getY(), board);
  13.     }
  14. }
  15.        
  16. public Tnt(int x, int y){
  17.     this.x = x;
  18.     this.y = y;
  19.     Toolkit t = Toolkit.getDefaultToolkit ();
  20.     image = t.createImage("src/main/resources/modelObjects/tnt.gif");
  21. }
  22.        
  23. import java.awt.Toolkit;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28.  
  29. import javax.swing.ImageIcon;
  30. import javax.swing.JFrame;
  31. import javax.swing.JLabel;
  32. import javax.swing.JPanel;
  33. import javax.swing.SwingUtilities;
  34. import javax.swing.Timer;
  35.  
  36. public class TestAnimatedGif {
  37.  
  38.     private static final int IMAGE_COUNT = 9;
  39.  
  40.     protected void initUI() {
  41.         JFrame frame = new JFrame(TestAnimatedGif.class.getSimpleName());
  42.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.         final JPanel panel = new JPanel();
  44.         frame.add(panel);
  45.         frame.setSize(600, 400);
  46.         frame.setVisible(true);
  47.         final Timer t = new Timer(1000, null);
  48.         t.addActionListener(new ActionListener() {
  49.  
  50.             int count = 0;
  51.  
  52.             @Override
  53.             public void actionPerformed(ActionEvent e) {
  54.                 if (count < IMAGE_COUNT) {
  55.                     try {
  56.                         JLabel image = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().createImage(
  57.                                 new URL("http://www.sitevip.net/gifs/bomba/BOMB-B_animado.gif"))));
  58.                         panel.add(image);
  59.                         count++;
  60.                         panel.revalidate();
  61.                         panel.repaint();
  62.                         System.err.println("image added");
  63.                     } catch (MalformedURLException e1) {
  64.                         // TODO Auto-generated catch block
  65.                         e1.printStackTrace();
  66.                     }
  67.                 } else {
  68.                     t.stop();
  69.                 }
  70.             }
  71.         });
  72.         t.start();
  73.     }
  74.  
  75.     public static void main(String[] args) {
  76.         SwingUtilities.invokeLater(new Runnable() {
  77.  
  78.             @Override
  79.             public void run() {
  80.                 new TestAnimatedGif().initUI();
  81.             }
  82.         });
  83.     }
  84.  
  85. }