title: Encode Java Swing animation to Gif
body: <p>This code shows an animation on a window, I'd like to encode that to a Gif file.
  How could I implement that?</p>
  
  <p>I already tried using the Gif89Encoder class but it seems not the best way to do that. Also it only takes 256 colours and I did not find a way to use my images with that.</p>
  
  <pre><code>import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.util.Random;
  
  import javax.imageio.ImageIO;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  public class Animation extends JPanel implements ActionListener{
  
  Timer tm = new Timer(5, this);
      int time = 0;
      int x = 0, velX = 2;
      int yPosition = 0;
      boolean goingDown = true;
  
      BufferedImage img = null;
      BufferedImage img2 = null;
      BufferedImage img3 = null;
      BufferedImage img4 = null;
      BufferedImage img5 = null;
      BufferedImage img6 = null;
  
      String [] user = {"Scissors", "Rock", "Paper", "Air", "Fire", "Sponge", "Water"};       
      String [] com = {"Scissors", "Rock", "Paper", "Air", "Fire", "Sponge", "Water"};
  
      int indx = new Random().nextInt(com.length);
  
      public void paintComponent (Graphics g){
          super.paintComponent(g);
  
  
          try {
              img = ImageIO.read(new File(user[0] + ".gif"));
              img2 = ImageIO.read(new File(com[indx] + ".gif")); 
              img3 = ImageIO.read(new File("expl.gif")); 
              img4 = ImageIO.read(new File("win.gif"));
              img5 = ImageIO.read(new File("lose.gif"));
              img6 = ImageIO.read(new File("draw.gif"));
  
  
              time++;
              tm.start();
          }catch(Exception e){
              System.err.println(e);
          }
  
  
  
          g.drawImage(img, x-75, 50+yPosition, null);
          g.drawImage(img2, -x+1225, 50+yPosition, null);
  
          if (x&gt;590)
              g.drawImage(img3, 475, 25, null);   
  
          if(time &gt;250){              //you win, you lose, draw
  
  
              //case 0:
              //g.drawImage(img4, 500, yPosition, null);
              //case 1:
              g.drawImage(img5, 500, yPosition, null);
              //case 2:
              //g.drawImage(img6, 500, yPosition, null);
          }
      }
  
  
      public void actionPerformed (ActionEvent e)     //Up and down animation
      {
          if (x &gt; 650)
              velX = -10*velX;
          x = x + 2*velX;
          repaint();
  
          if (goingDown == true){
              if (yPosition &lt;= 0){
                  goingDown = false;
                  yPosition++;
              }
              else {
                  yPosition--; 
              }
          }
          else {
              // if we're going up and we reach 0, go down again
              if (yPosition &gt;= 50){
                  goingDown = true;
                  yPosition--;
              }
              else {
                  yPosition++; 
              }
          }
      }
  
      public static void main(String [] args){        //Shows the window
          Animation ani = new Animation();
          JFrame jf = new JFrame();
          jf.setTitle("Fight");
          jf.setSize(1300, 300);
          jf.setVisible(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jf.add(ani);
      }
  }
  </code></pre>