java编写动态图片_Java之简单的图片动态显示(实现类似GIF动画结果) | 学步园...

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

*

* Donttai.java

*

* Created on __DATE__, __TIME__

*/

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

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值