码迷,mamicode.com
首页 > 移动开发 > 详细

e581. Animating an Array of Images in an Application

时间:2018-09-02 23:52:50      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:delay   cell   current   wim   get   color   public   this   run   

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

e581. Animating an Array of Images in an Application

标签:delay   cell   current   wim   get   color   public   this   run   

原文地址:https://www.cnblogs.com/borter/p/9575633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!