e581. Animating an Array of Images in an Application

Posted borter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于e581. Animating an Array of Images in an Application的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #581 (Div. 2)-E. Natasha, Sasha and the Prefix Sums-动态规划+组合数学

[GeeksForGeeks] Convert an array to reduced form

codeforces581D

leetcode1394. Find Lucky Integer in an Array

k_mean.fit 返回 ValueError: setting an array element with a sequence

215. Kth Largest Element in an Array (have better solution )