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++;
}