将Java Swing动画编码为Gif

时间:2015-12-10 12:10:27

标签: java swing animation gif

此代码在窗口上显示动画,我想将其编码为Gif文件。 我怎么能实现呢?

我已经尝试过使用Gif89Encoder类,但似乎不是最好的方法。此外,它只需要256种颜色,我没有找到一种方法来使用我的图像。

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);
    }
}

0 个答案:

没有答案