• e581. Animating an Array of Images in an Application


    This is the simplest application to animate an array of images.

        import java.awt.*;
        import javax.swing.*;
        
        public class AnimApp extends JComponent implements Runnable {
            Image[] images = new Image[2];
            int frame = 0;
        
            public void paint(Graphics g) {
                Image image = images[frame];
                if (image != null) {
                    // Draw the current image
                    int x = 0;
                    int y = 0;
                    g.drawImage(image, x, y, this);
                }
            }
        
            public void run() {
                // Load the array of images
                images[0] = new ImageIcon("image1.gif").getImage();
                images[1] = new ImageIcon("image2.gif").getImage();
        
                // Display each image for 1 second
                int delay = 1000;    // 1 second
        
                try {
                    while (true) {
                        // Move to the next image
                        frame = (frame+1)%images.length;
        
                        // Causes the paint() method to be called
                        repaint();
        
                        // Wait
                        Thread.sleep(delay);
                    }
                } catch (Exception e) {
                }
            }
        
            public static void main(String[] args) {
                AnimApp app = new AnimApp();
        
                // Display the animation in a frame
                JFrame frame = new JFrame();
                frame.getContentPane().add(app);
                frame.setSize(300, 300);
                frame.setVisible(true);
        
                (new Thread(app)).start();
            }
        }
    
    Related Examples
  • 相关阅读:
    【分区】使用 MBR 分区表分区并格式化
    微信小程序公司开发前必读
    Delphi 经典书籍
    sybase 通过select into创建新表
    sybase 创建触发器
    delphi 判断exe重复执行
    git 的诞生
    git 常用命令
    mvn spring-boot:run运行不了的解决办法
    git 提交代码
  • 原文地址:https://www.cnblogs.com/borter/p/9575633.html
Copyright © 2020-2023  润新知