java gif动画_java实现gif动画效果代码参考

java实现gif动画效果代码参考

复制代码 代码如下:

/*

* 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, 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

//

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

}//

//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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java在窗口上加载显示GIF动画图像,将多个独立的GIF图像串联在一起显示,形成GIF特有的动画形式。主要代码如下:   ImageIcon[] images; //用于动画的图标数组   Timer animationTimer;   int currentImage = 0; //当前图像编号   int delay = 500; //图像切换延迟   int width; //图像宽度   int height; //图像高度   public AnimatorIcon() //构造函数   {    setBackground(Color.white);    images = new ImageIcon[2]; //初始化数组    for (int i=0;i   images[i]=new ImageIcon(getClass().getResource("image" i ".gif")); //实例化图标    width = images[0].getIconWidth(); //初始化宽度值    height = images[0].getIconHeight(); //初始化高度值   }   public void paintComponent(Graphics g) { //重载组件绘制方法    super.paintComponent(g); //调用父类函数    images[currentImage].paintIcon(this,g,70,0); //绘制图标    currentImage=(currentImage 1)%2; //更改当前图像编号   }   public void actionPerformed(ActionEvent actionEvent) {    repaint();   }   public void startAnimation() { //开始动画    if (animationTimer==null) {    currentImage=0;    animationTimer=new Timer(delay, this); //实例化Timer对象    animationTimer.start(); //开始运行    } else if (!animationTimer.isRunning()) //如果没有运行    animationTimer.restart(); //重新运行   }   public void stopAnimation() {    animationTimer.stop(); //停止动画   }   public static void main(String args[]) {    AnimatorIcon animation = new AnimatorIcon(); //实例化动画图标    JFrame frame = new JFrame("动画图标"); //实例化窗口对象    frame.getContentPane().add(animation); //增加组件到窗口上    frame.setSize(200, 100); //设置窗口尺寸    frame.setVisible(true); //设置窗口可视    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    animation.startAnimation(); //开始动画
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值