java实现gif动画效果(java显示动态图片)
就是每隔一定的时间显示一张图片,全部图片文件位于:“工作空间\项目名称\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();
}
}
}
}