3

This code shows an animation on a window, I'd like to encode that to a Gif file. How could I implement that?

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.

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>590)
            g.drawImage(img3, 475, 25, null);   

        if(time >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 > 650)
            velX = -10*velX;
        x = x + 2*velX;
        repaint();

        if (goingDown == true){
            if (yPosition <= 0){
                goingDown = false;
                yPosition++;
            }
            else {
                yPosition--; 
            }
        }
        else {
            // if we're going up and we reach 0, go down again
            if (yPosition >= 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);
    }
}
belvederef
  • 2,195
  • 19
  • 16
  • 2
    Any GIF image, whether animated or not, has no more than 256 colors. It's a very old format. If your source images have more than 256 individual colors in them, you need to transform them into a selection of 256 colors that best represents them. Sometimes it's advised to use techniques like dithering to mix colors. All of them should be mapped to the same 256 colors and only then saved in the GIF. This is always true regardless of the fact that you are capturing animation from a Swing application, a movie, or whatever other source. – RealSkeptic Dec 10 '15 at 12:28
  • 1
    This can help http://stackoverflow.com/questions/777947/creating-animated-gif-with-imageio – Petter Friberg Dec 10 '15 at 15:20

0 Answers0