[JAVA100例]023、滑动杆

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* <p>Title: 滑动杆演示</p>
* <p>Description: 使用滑动杆控制定时器,来控制图片的播放速度</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: SliderDemo.java</p>
* @version 1.0
*/
public class SliderDemo extends JPanel
            implements ActionListener,
                  WindowListener,
                  ChangeListener {
  //设置图片的参数
  static final int FPS_MIN = 0; //设置最小值
  static final int FPS_MAX = 30; //设置最大值
  static final int FPS_INIT = 15; //初始数值
  int frameNumber = 0;
  int NUM_FRAMES = 14;
  ImageIcon[] images = new ImageIcon[NUM_FRAMES];
  int delay;
  Timer timer;
  boolean frozen = false;

//这个标签用来显示这只小狗
  JLabel picture;

public SliderDemo() {
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

delay = 1000 / FPS_INIT;

//信息提示标签
    JLabel sliderLabel = new JLabel("调整滑动杆,改变播放速度!", JLabel.CENTER);
    sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

//创建一个滑动杆,定义了最小值和最大值以及初始值
    JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,
                       FPS_MIN, FPS_MAX, FPS_INIT);
    framesPerSecond.addChangeListener(this);

//定义滑动杆参数
    framesPerSecond.setMajorTickSpacing(10);//每10刻度标注一次
    framesPerSecond.setMinorTickSpacing(1);//最小刻度为1
    framesPerSecond.setPaintTicks(true);//绘制滑动杆上的刻度
    framesPerSecond.setPaintLabels(true);//在滑动过程中绘制滑动块
    framesPerSecond.setBorder(
        BorderFactory.createEmptyBorder(0,0,10,0));

//定义一个用来显示图片的标签
    picture = new JLabel();
    picture.setHorizontalAlignment(JLabel.CENTER);
    picture.setAlignmentX(Component.CENTER_ALIGNMENT);
    picture.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createLoweredBevelBorder(),
        BorderFactory.createEmptyBorder(10,10,10,10)));
    updatePicture(0); //显示第一张图

//将成员添加到面板上
    add(sliderLabel);
    add(framesPerSecond);
    add(picture);
    setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

//设置一个定时器来触发这个事件
    timer = new Timer(delay, this);
    timer.setInitialDelay(delay * 7); //在每轮循环停顿时间
    timer.setCoalesce(true);//设置重复循环
  }
/**
*<br>方法说明:添加一个窗体监听
*<br>输入参数:
*<br>返回类型:
*/
  void addWindowListener(Window w) {
    w.addWindowListener(this);
  }
  public void windowIconified(WindowEvent e) {
    stopAnimation();
  }
  public void windowDeiconified(WindowEvent e) {
    startAnimation();
  }
  public void windowOpened(WindowEvent e) {}
  public void windowClosing(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
/**
*<br>方法说明:对滑动杆进行监听
*<br>输入参数:ChangeEvent e 滑动杆变动事件
*<br>返回类型:
*/
  public void stateChanged(ChangeEvent e) {
    JSlider source = (JSlider)e.getSource();
    if (!source.getValueIsAdjusting()) {
      int fps = (int)source.getValue();//获得滑动杆的值
      if (fps == 0) {
        if (!frozen) stopAnimation();
      } else {
        delay = 1000 / fps;
        timer.setDelay(delay);
        timer.setInitialDelay(delay * 10);
        if (frozen) startAnimation();
      }
    }
  }
/**
*<br>方法说明:开始动画
*<br>输入参数:
*<br>返回类型:
*/
  public void startAnimation() {
    timer.start();
    frozen = false;
  }
/**
*<br>方法说明:停止动画
*<br>输入参数:
*<br>返回类型:
*/
  public void stopAnimation() {
    timer.stop();
    frozen = true;
  }
/**
*<br>方法说明:事件监听
*<br>输入参数:
*<br>返回类型:
*/
  public void actionPerformed(ActionEvent e) {
    //改变图片帧
    if (frameNumber == (NUM_FRAMES - 1)) {
      frameNumber = 0;
    } else {
      frameNumber++;
    }

updatePicture(frameNumber); //显示下张图

if ( frameNumber==(NUM_FRAMES - 1)
     || frameNumber==(NUM_FRAMES/2 - 1) ) {
      timer.restart();
    }
  }
/**
*<br>方法说明:绘制当前帧
*<br>输入参数:int frameNum 图片帧数数
*<br>返回类型:
*/
  protected void updatePicture(int frameNum) {
    if (images[frameNumber] == null) {
      images[frameNumber] = createImageIcon("images/doggy/T"
                         + frameNumber
                         + ".gif");
    }

//绘制图片
    if (images[frameNumber] != null) {
      picture.setIcon(images[frameNumber]);
    } else { //如果没有发现图片
      picture.setText("image #" + frameNumber + " not found");
    }
  }
/**
*<br>方法说明:获取图片
*<br>输入参数:String path 图片路径
*<br>返回类型:ImageIcon 图片对象
*/
  protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = SliderDemo.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    } else {
      System.err.println("Couldn´t find file: " + path);
      return null;
    }
  }
/**
*<br>方法说明:主方法
*<br>输入参数:
*<br>返回类型:
*/
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);

//定义窗体
    JFrame frame = new JFrame("SliderDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//实例化本类
    SliderDemo animator = new SliderDemo();
    animator.setOpaque(true);
    frame.setContentPane(animator);

//显示窗体
    frame.pack();
    frame.setVisible(true);
    animator.startAnimation();
  }
}

时间: 2024-10-02 00:40:15

[JAVA100例]023、滑动杆的相关文章

JAVA100例专题

[JAVA100例]041.执行系统命令 [JAVA100例]078.播放声音 [JAVA100例]077.应用JAR包 [JAVA100例]076.在Applet中使用Swing [JAVA100例]075.检测键盘事件 [JAVA100例]074.检测鼠标事件 [JAVA100例]073.传递参数 [JAVA100例]072.欢迎进入Applet世界 [JAVA100例]071.使用JDOM操作XML [JAVA100例]070.使用JDOM解析XML [JAVA100例]069.使用SAX

[JAVA100例]041、执行系统命令

import java.io.*; /** * <p>Title: 运行系统命令</p> * <p>Description:运行一个系统的命令,演示使用Runtime类.</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: CmdExec.java</p> * @version 1.0 */ public class CmdExec { /** *

[JAVA100例]039、生成PDF文件

import com.lowagie.text.*; import com.lowagie.text.pdf.*; import java.io.*; import java.util.*; import java.awt.Color; /** * <p>Title: 生成PDF文件</p> * <p>Description: 本实例通过使用iText包生成一个表格的PDF文件</p> * <p>Copyright: Copyright (c)

[JAVA100例]071、使用JDOM操作XML

import org.jdom.*; import org.jdom.output.*; import java.io.*; import java.util.List; /** * <p>Title: 使用JDOM操作XML文件.</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: JDOMOption.java

[JAVA100例]070、使用JDOM解析XML

import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; import java.io.*; import java.util.List; /** * <p>Title: 使用JDOM解析XML</p> * <p>Description: 通过使用JDOM接口解析book.xml文件</p> * <p>Copyright: Copyright (c) 2003<

[JAVA100例]069、使用SAX解析XML

ReadXmlDemo.java import java.io.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class ReadXmlDemo extends DefaultHandler { public static void main(String[] arguments) { if (arguments.length > 0){ ReadXmlDemo

[JAVA100例]068、使用DOM解析XML

import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.*; public class DOMPageList { public static void main(String[] args) { try { //创建解析工厂 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); //指定DocumentBuilder Docume

[JAVA100例]067、线程优先级

import java.util.*; /** * <p>Title: 提高线程优先级</p> * <p>Description: 通过修改线程的优先级,是线程获得优先处理.</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: upPRIThread.java</p> * @version 1.0 */ public class upPRIThre

[JAVA100例]066、线程控制

/** * <p>Title: 线程控制</p> * <p>Description: 实现对线程的控制,中断.挂起.恢复.停止</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: threadCtrl.java</p> * @version 1.0 */ public class threadCtrl{ public static void mai