java实现gif动画效果(java显示动态图片)

时间:2023-10-19 07:56:38 

就是每隔一定的时间显示一张图片,全部图片文件位于:“工作空间\项目名称\bin\动态图\花好月圆\”文件夹下。文件名类似:1001.jpg,1002.jpg,一直到1016.jpg:


/*
 * Donttai.java
 *
 * Created on __DATE__, __TIME__
 */

package 动态图;

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;

/**
 *
 * @author  __USER__
 */
public class Donttai extends javax.swing.JFrame implements Runnable
{

    /** Creates new form Donttai */
    public Donttai()
    {
        initComponents();
    }

    //String path = "E:\\Workspaces\\MyEclipse_9.0\\20120731\\bin\\动态图\\1\\10";
    //Java默认文件路径是项目的根目录,所以要手动加上bin及其以下的路径
    String path="bin/动态图/花好月圆/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();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //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

    /**
     * @param args the command line arguments
     */
    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();
            }

        }
    }

}

标签:java,gif,动画
0
投稿

猜你喜欢

  • spring boot中配置hikari连接池属性方式

    2022-11-13 06:06:44
  • java hasNextInt判断是否为数字的方法

    2023-05-17 01:51:12
  • SpringMVC执行步骤、Model的使用详解

    2022-02-13 05:38:45
  • android获取屏幕宽高与获取控件宽高(三种方法)

    2022-07-11 11:25:42
  • Android 7.0中拍照和图片裁剪适配的问题详解

    2022-09-27 07:29:57
  • Android开发之DatePicker和TimePicker实现选择日期时间功能示例

    2022-10-06 05:35:21
  • 关于maven全局配置文件settings.xml解析

    2023-01-08 09:04:04
  • springboot整合Shiro

    2022-11-13 11:07:52
  • java制作复制文件工具代码分享

    2022-08-05 05:30:22
  • 使用Spring组合自定义的注释 mscharhag操作

    2023-02-14 11:18:01
  • ScrollView与ListView合用(正确计算Listview的高度)的问题解决

    2021-12-30 07:14:58
  • 解决springboot生成bean名称冲突(AnnotationBeanNameGenerator)

    2023-01-09 22:27:11
  • 线程阻塞唤醒工具 LockSupport使用详解

    2023-11-29 17:16:10
  • 详解Java中List的正确的删除方法

    2021-09-19 13:20:42
  • c#实现md5加密示例

    2023-04-19 16:37:11
  • java实现秒表功能

    2023-07-06 05:55:13
  • Java设计模式编程中的工厂方法模式和抽象工厂模式

    2023-03-19 05:50:48
  • mybatis 插件: 打印 sql 及其执行时间实现方法

    2023-05-29 16:49:19
  • Android自定view画圆效果

    2021-06-05 21:45:52
  • c# 委托的常见用法

    2021-08-11 13:44:05
  • asp之家 软件编程 m.aspxhome.com