加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:42:52 所属栏目:Java 来源:网络整理
导读:就是每隔一定的时间显示一张图片,全部图片文件位于:“工作空间项目名称bin动态图花好月圆”文件夹下。文件名类似:1001.jpg,1002.jpg,一直到1016.jpg: 复制代码 代码如下: /* * Donttai.java * * Created on __DATE__,__TIME__ */ package 动态图; im

就是每隔一定的时间显示一张图片,全部图片文件位于:“工作空间项目名称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:WorkspacesMyEclipse_9.020120731bin动态图110";
    //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,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 2d画图示例分享(用java画图)
  • Java拖曳鼠标实现画线功能的方法
  • Java版画板的实现方法
  • JAVA GUI自定义JPanel画板背景
  • java和matlab画多边形闭合折线图示例讲解
  • Java画笔的简单实用方法
  • Java实现两人五子棋游戏(二) 画出棋盘
  • Java实现两人五子棋游戏(三) 画出棋子
  • java实现图像转码为字符画的方法
  • Java实现画线、矩形、椭圆、字符串功能

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读