java實(shí)現(xiàn)gif動(dòng)畫效果(java顯示動(dòng)態(tài)圖片)
就是每隔一定的時(shí)間顯示一張圖片,全部圖片文件位于:“工作空間\項(xiàng)目名稱\bin\動(dòng)態(tài)圖\花好月圓\”文件夾下。文件名類似:1001.jpg,1002.jpg,一直到1016.jpg:
/*
* Donttai.java
*
* Created on __DATE__, __TIME__
*/
package 動(dòng)態(tài)圖;
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\\動(dòng)態(tài)圖\\1\\10";
//Java默認(rèn)文件路徑是項(xiàng)目的根目錄,所以要手動(dòng)加上bin及其以下的路徑
String path="bin/動(dòng)態(tài)圖/花好月圓/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();
}
}
}
}
相關(guān)文章
使用Springboot打成jar包thymeleaf的問(wèn)題
這篇文章主要介紹了使用Springboot打成jar包thymeleaf的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java多態(tài)的向上轉(zhuǎn)型的概念及實(shí)例分析
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java多態(tài)的向上轉(zhuǎn)型的概念及實(shí)例分析,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-05-05
Spring中的spring.factories文件用法(Spring如何加載第三方Bean)
這篇文章主要介紹了Spring中的spring.factories文件用法(Spring如何加載第三方Bean),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java 常見(jiàn)異常(Runtime Exception )詳細(xì)介紹并總結(jié)
這篇文章主要介紹了Java 常見(jiàn)異常(Runtime Exception )詳細(xì)介紹并相關(guān)資料,大家在開(kāi)發(fā)Java 應(yīng)用軟件的時(shí)候經(jīng)常會(huì)遇到各種異常這里幫大家整理了一部分,并解釋如何解決,需要的朋友可以參考下2016-10-10
Spring Security OAuth2 授權(quán)碼模式的實(shí)現(xiàn)
這篇文章主要介紹了Spring Security OAuth2 授權(quán)碼模式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
java list中包含某個(gè)字符串的兩種方法實(shí)現(xiàn)
在Java開(kāi)發(fā)中,經(jīng)常需要判斷一個(gè)List中是否包含特定的字符串,包括使用contains()方法和循環(huán)遍歷判斷,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Java實(shí)現(xiàn)讀取Word模板文檔并替換內(nèi)容生成新文檔
在實(shí)際開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要根據(jù) Word 模板生成特定文檔的需求,下面小編就來(lái)為大家介紹一下如何使用 Apache POI 庫(kù)來(lái)讀取 Word 模板文檔,然后替換其中的指定內(nèi)容最后生成新的文檔吧2025-02-02

