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

📄 j_animatortimer.java.bak

📁 一个十分好的java基础学习的课件
💻 BAK
字号:
// ////////////////////////////////////////////////////////
// 
// J_AnimatorTimer.java
// 
// Created by Jun-Hai Yong,    on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// Readme:
// A simple animation with a timer.
// ////////////////////////////////////////////////////////
// Using this example, please explicitly refer to the book:
//     Jun-Hai Yong. Programming in Java. 
//     Beijing: Tsinghua University Press, 2004.
// 使用本例子,请注明引用:
//     雍俊海. Java 程序设计. 北京: 清华大学出版社, 2004.
// Please note that the author and publisher make no 
// warranty of any kind on the examples provided.
// ////////////////////////////////////////////////////////

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class J_AnimatorTimer extends JApplet implements ActionListener
{

    int  m_frame = -1;     // The number of the current frame in the animation.
    Timer  m_timer;          // The timer for controlling the animation.
    boolean  m_frozen = false; // Status of stopping animation.
    JLabel  m_label = new JLabel("Frame   ", JLabel.CENTER);

    public void init( )
    {
        int delay = 50;
        m_timer = new Timer(delay, this);
        m_timer.setInitialDelay(0);
        m_timer.setCoalesce(true);

        getContentPane( ).add(m_label, BorderLayout.CENTER);
        m_label.addMouseListener(new MouseAdapter( )
        {
            public void mousePressed(MouseEvent e)
            {
                m_frozen = !m_frozen;
                if (m_frozen)
                     mb_stopAnimation( );
                else mb_startAnimation( );
            }
        });
    } // End of method: init

    public void start( )
    { 
        mb_startAnimation( );
    } // End of method: start

    public void stop( )
    { 
        mb_stopAnimation( );
    } // End of method: stop

    public void actionPerformed(ActionEvent e)
    {
        m_frame++; //Advance the animation frame.
        //Request that the frame be painted.
        m_label.setText("Frame " + m_frame);
    } // End of method: actionPerformed

    public synchronized void mb_startAnimation( )
    {
        if (!m_frozen && !m_timer.isRunning( ))
            m_timer.start( );
    } // End of method: mb_startAnimation

    public synchronized void mb_stopAnimation( )
    {
        if (m_timer.isRunning( ))
            m_timer.stop( );
    } // End of method: mb_stopAnimation

} // End of class: J_AnimatorTimer

⌨️ 快捷键说明

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