title: Java - drawing image with Timer
body: <p>I'm creating an animation that looks like this.</p>
  
  <p><img src="https://i.stack.imgur.com/tx5VZ.png" alt="enter image description here"></p>
  
  <p>I'd like the swirly icon on the left (which is an ImageIcon) to display for 3 seconds and disappear.  However, the swirly icon does not disappear.</p>
  
  <p>Here's my code.</p>
  
  <pre><code>import javax.imageio.ImageIO;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.io.File;
  import java.io.IOException;
  
  public class Main {
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });
      }
  
      private static void createAndShowGUI() {
  
          JFrame f = new JFrame("random title");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.setResizable(false);
          f.add(new MyPanel());
          f.pack();
          f.setVisible(true);
      }
  }
  
  class MyPanel extends JPanel {
  
      private static final long serialVersionUID = 1L;
      public int x;
      public int y;
      public int remoteControllerX = 473;
      public int remoteControllerY = 340;
      public int buttonX = 166;
      public int buttonY = 208;
      Image img;
      Image remoteController;
      ImageIcon button = new ImageIcon("graphics/button.gif");
      Component buttonTrigger = this;
  
      public MyPanel() {
  
          try {
              img = ImageIO.read(new File("graphics/close_0.jpg"));
          } catch (IOException e) {
  
              e.printStackTrace();
          }
          try {
              remoteController = ImageIO.read(new File("graphics/pilot.png"));
          } catch (IOException ex) {
              ex.printStackTrace();
          }
          setBorder(BorderFactory.createLineBorder(Color.black));
  
          new Timer(3000, paintTimer).start();
  
          addMouseListener(new MouseAdapter() {
              public void mousePressed(MouseEvent e) {
                  x = e.getX();
                  y = e.getY();
                  // Here goes action on background, which is unrelated to this example.
              }
          });
      }
  
      public Dimension getPreferredSize() {
          return new Dimension(1048, 484);
      }
  
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          g.drawImage(img, 0, 0, null);
          g.drawImage(remoteController, remoteControllerX, remoteControllerY, null);
          Toolkit.getDefaultToolkit().sync();
          button.paintIcon(buttonTrigger, g, buttonX, buttonY);
      }
      Action paintTimer = new AbstractAction() {
          private static final long serialVersionUID = -2121714427110679013L;
  
          public void actionPerformed(ActionEvent e) {
              buttonTrigger = null;
              repaint();
          }
      };
  }
  </code></pre>
  
  <p>You'll also need these 3 images for the code to run:</p>
  
  <p><a href="http://ajks.pl/graveyard/close_0.jpg" rel="nofollow noreferrer">http://ajks.pl/graveyard/close_0.jpg</a></p>
  
  <p><a href="http://ajks.pl/graveyard/pilot.png" rel="nofollow noreferrer">http://ajks.pl/graveyard/pilot.png</a></p>
  
  <p><a href="http://ajks.pl/graveyard/button.gif" rel="nofollow noreferrer">http://ajks.pl/graveyard/button.gif</a></p>
  
  <p>They are placed in a graphics folder under the main Java project.</p>