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

github:https://github.com/sxei
博客园:http://www.cnblogs.com/liuxianan
copyright ©2012-2020 小茗同学
【转载文章务必保留出处和署名,谢谢!】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?