Java to achieve GIF animation effect of Java display dynamic pictures

  • 2020-04-01 03:19:57
  • OfStack

Is every once in a while to display a picture, all the picture files are located in: "workspace \ item name \bin\ dynamic graph \ good month \" folder. File names like: 1001.jpg,1002.jpg, all the way to 1016.jpg:



package  Dynamic figure ;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Donttai extends javax.swing.JFrame implements Runnable
{
    
    public Donttai()
    {
        initComponents();
    }
    //String path = "E:\Workspaces\ myeclipse_9.0 \20120731\bin\ 1 10";
    //The Java default file path is the root directory of the project, so manually add the bin and the following paths
    String path="bin/ Dynamic figure / Happy together /10";
    int idx = 1;
    public void paint(Graphics g)
    {
        ImageObserver imageObserver = new ImageObserver()
        {
            @Override
            public boolean imageUpdate(Image img, int infoflags, int x, int y,
                    int width, int height)
            {
                // TODO Auto-generated method stub
                return false;
            }
        };
        try
        {
            //g.drawImage(ImageIO.read(new File(Donttai.class.getResource("1.png").toString())), 20, 20, imageObserver);
            String temp = "";
            if (idx <= 9)
                temp = path + "0" + idx + ".jpg";
            else if (idx >= 10)
            {
                temp = path + idx + ".jpg";
            }
            g.drawImage(ImageIO.read(new File(temp)), 100, 50, 400, 300,
                    imageObserver);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents()
    {
        jButton1 = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setText("jButton1");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup()
                        .addContainerGap(483, Short.MAX_VALUE)
                        .addComponent(jButton1).addGap(35, 35, 35)));
        layout.setVerticalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addGap(37, 37, 37)
                        .addComponent(jButton1)
                        .addContainerGap(392, Short.MAX_VALUE)));
        pack();
    }// </editor-fold>
    //GEN-END:initComponents
    
    public static void main(String args[])
    {
        //        java.awt.EventQueue.invokeLater(new Runnable()
        //        {
        //            public void run()
        //            {
        //                new Donttai().setVisible(true);
        //                
        //            }
        //        });
        Donttai donttai = new Donttai();
        donttai.setVisible(true);
        donttai.run();
    }
    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
    @Override
    public void run()
    {
        // TODO Auto-generated method stub
        while (true)
        {
            repaint();
            if (idx < 16)
                idx++;
            else
                idx = 1;
            try
            {
                Thread.sleep(60);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}


Related articles: