⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animateframe.java

📁 里面包含了多个java的编程示例!而且举出初学者常常遇到的错误!
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;

public class AnimateFrame extends JFrame {
    public AnimateFrame() {
        super("Lighthouse");
        setSize(215, 298);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        AnimatePanel canvas = new AnimatePanel();
        add(canvas);
        setVisible(true);
    }

    public static void main(String[] arguments) {
        AnimateFrame af = new AnimateFrame();
    }
}

class AnimatePanel extends JPanel implements Runnable {
    Image[] picture = new Image[6];
    int totalPictures = 0;
    int current = 0;
    Thread runner;
    int pause = 800;
    String[] imageNames = { "lh0.gif", "lh1.gif", "lh2.gif", "lh3.gif" };
    
    public AnimatePanel() {
        super();
        Toolkit kit = Toolkit.getDefaultToolkit();
        for (int i = 0; i < imageNames.length; i++) {
            picture[i] = kit.getImage(imageNames[i]);
        }
        runner = new Thread(this);
        runner.start();
    }

    public void paintComponent(Graphics screen) {
        Graphics2D screen2D = (Graphics2D) screen;
        if (picture[current] != null) {
            screen2D.drawImage(picture[current], 0, 0, this);
        }
    }

    public void start() {
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (runner == thisThread) {
            repaint();
            current++;
            if (current > imageNames.length) {
                current = 0;
            }
            try {
                Thread.sleep(pause);
            } catch (InterruptedException e) { }
        }
    }

    public void stop() {
        if (runner != null) {
            runner = null;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -