程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> [JAVA100例]023、滑動桿

[JAVA100例]023、滑動桿

編輯:關於JAVA

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();
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved