dukeplayapplet.java

来自「几个简单的java学习代码」· Java 代码 · 共 92 行

JAVA
92
字号
import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;


public class DukePlayApplet extends JApplet
                                implements ActionListener {
    JLabel imageLbl;
    ImageIcon[] images ;
    static int frameNumber =-1;
    int delay;
    Thread animatorThread;
    static boolean frozen = false;
    Timer timer;

    //Invoked only when this is run as an applet.
    public void init() {
        //Get the images.
        images = new ImageIcon[10];
        for (int i = 1; i <= 10; i++) {
            images[i-1] = new ImageIcon("images/T"+i+".gif");
        }


        int fps = 10;

        //How many milliseconds between frames?
        delay = (fps > 0) ? (1000 / fps) : 100;

        //Set up a timer that calls this object's action handler
        timer = new Timer(delay, this);
        timer.setInitialDelay(0);
        timer.setCoalesce(true);

        imageLbl = new JLabel(" " ,JLabel.CENTER  );//new ImageIcon("images/T1.gif"));

        getContentPane().add(imageLbl, BorderLayout.CENTER);

        imageLbl.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (frozen) {
                    frozen = false;
                    startAnimation();
                } else {
                    frozen = true;
                    stopAnimation();
                }
            }
       });
    }

    public void start() {
        startAnimation();
    }

    public void stop() {
        stopAnimation();
    }

    public synchronized void startAnimation() {
        if (frozen) {
            //Do nothing.  The user has requested that we
            //stop changing the image.
        } else {
            //Start animating!
            if (!timer.isRunning()) {
                timer.start();
            }
        }
    }

    public synchronized void stopAnimation() {
        //Stop the animating thread.
        if (timer.isRunning()) {
            timer.stop();
        }
    }

    public void actionPerformed(ActionEvent e) {
        //Advance the animation frame.
        frameNumber++;

        //Display it.
        imageLbl.setIcon(images[frameNumber%10]) ;

    }


}

⌨️ 快捷键说明

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