Swing中Timer 的一個(gè)使用例子
Posted on 2009-05-22 16:19 guanminglin@gmail.com 閱讀(36324) 評論(6) 編輯 收藏 所屬分類: JavaSE 最近在學(xué)習(xí)Swing中的動(dòng)畫繪制,用到了Timer 這個(gè)類,于是寫一點(diǎn)筆記,和大家分享。大家有什么好的例子不妨共享出來吧!!
計(jì)時(shí)器在java.swing包中的Timer類來創(chuàng)建,它可以看做是GUI的一個(gè)組件。與其他組件不一樣的是,它沒有可以顯示在屏幕上的直觀的外觀。正如名字所表達(dá)的,它只幫我們來計(jì)時(shí)。
計(jì)時(shí)器對象按相等的時(shí)間間隔來產(chǎn)生動(dòng)作事件。執(zhí)行動(dòng)畫程序時(shí),可以設(shè)置計(jì)時(shí)器來定期產(chǎn)生動(dòng)作事件,然后在動(dòng)作監(jiān)聽器中更新動(dòng)畫圖形。
下面來點(diǎn)實(shí)際的例子,例子要完成的是:一張圖片在窗口內(nèi)按一定的角度滑行,碰到窗口的邊界時(shí)在彈回來。(類似XP下的某個(gè)屏保)
ReboundPanel.java
在程序中,類ReboundPanel 類的構(gòu)造方法創(chuàng)建了一個(gè)定時(shí)器(Timer)對象,Timer構(gòu)造方法的第一個(gè)參數(shù)是延遲時(shí)間(也就是間隔時(shí)間),第二個(gè)參數(shù)是要處理計(jì)時(shí)器動(dòng)作事件的監(jiān)聽器。構(gòu)造方法中還設(shè)置了圖像的初始位置,及每次移動(dòng)是垂直和水平方向的像素變化量。
監(jiān)聽器的actionPerformed()方法更新當(dāng)前的x和y坐標(biāo),然后檢查它是否會(huì)令圖像撞到面板的邊界。如果撞上,則調(diào)整運(yùn)動(dòng)方向,讓圖像按與原水平、垂直或者兩者的反方向運(yùn)動(dòng)。更新坐標(biāo)值后,actionPerformed() 方法調(diào)用repaint方法重繪組件。調(diào)用repaint方法最終又會(huì)調(diào)用paintComponent方法,它在新的位置重繪圖像。
最終效果如下:

計(jì)時(shí)器在java.swing包中的Timer類來創(chuàng)建,它可以看做是GUI的一個(gè)組件。與其他組件不一樣的是,它沒有可以顯示在屏幕上的直觀的外觀。正如名字所表達(dá)的,它只幫我們來計(jì)時(shí)。
計(jì)時(shí)器對象按相等的時(shí)間間隔來產(chǎn)生動(dòng)作事件。執(zhí)行動(dòng)畫程序時(shí),可以設(shè)置計(jì)時(shí)器來定期產(chǎn)生動(dòng)作事件,然后在動(dòng)作監(jiān)聽器中更新動(dòng)畫圖形。
下面來點(diǎn)實(shí)際的例子,例子要完成的是:一張圖片在窗口內(nèi)按一定的角度滑行,碰到窗口的邊界時(shí)在彈回來。(類似XP下的某個(gè)屏保)
ReboundPanel.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* 演示如何使用Timer 這個(gè)類來完成Swing的動(dòng)畫效果。
* @author guanminglin <guanminglin@gmail.com>
*/
public class ReboundPanel extends JPanel {
private final int panelWidth = 300, panelHeight = 200;
private final int DELAY = 20, IMAGE_SIZE = 35;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY;
/**
* 面板構(gòu)造函數(shù),初始化面板。包括Timer 的場景。
*/
public ReboundPanel() {
timer = new Timer(DELAY, new ReboundListener());
image = new ImageIcon("images/1.gif");
x = 0;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(panelWidth, panelHeight));
setBackground(Color.BLACK);
timer.start();
}
/**
* 繪出圖像在面板中的位置。
* @param page
*/
@Override
public void paintComponent(Graphics page) {
super.paintComponent(page);
image.paintIcon(this, page, x, y);
}
//Swing 動(dòng)畫,不斷的更新圖像的位置,已達(dá)到動(dòng)畫的效果。
private class ReboundListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
x += moveX;
y += moveY;
if (x <= 0 || x >= panelWidth - IMAGE_SIZE) {
moveX = moveX * (-1);
}
if (y <= 0 || y >= panelHeight - IMAGE_SIZE) {
moveY = moveY * (-1);
}
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rebound");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ReboundPanel());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* 演示如何使用Timer 這個(gè)類來完成Swing的動(dòng)畫效果。
* @author guanminglin <guanminglin@gmail.com>
*/
public class ReboundPanel extends JPanel {
private final int panelWidth = 300, panelHeight = 200;
private final int DELAY = 20, IMAGE_SIZE = 35;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY;
/**
* 面板構(gòu)造函數(shù),初始化面板。包括Timer 的場景。
*/
public ReboundPanel() {
timer = new Timer(DELAY, new ReboundListener());
image = new ImageIcon("images/1.gif");
x = 0;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(panelWidth, panelHeight));
setBackground(Color.BLACK);
timer.start();
}
/**
* 繪出圖像在面板中的位置。
* @param page
*/
@Override
public void paintComponent(Graphics page) {
super.paintComponent(page);
image.paintIcon(this, page, x, y);
}
//Swing 動(dòng)畫,不斷的更新圖像的位置,已達(dá)到動(dòng)畫的效果。
private class ReboundListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
x += moveX;
y += moveY;
if (x <= 0 || x >= panelWidth - IMAGE_SIZE) {
moveX = moveX * (-1);
}
if (y <= 0 || y >= panelHeight - IMAGE_SIZE) {
moveY = moveY * (-1);
}
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rebound");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ReboundPanel());
frame.pack();
frame.setVisible(true);
}
}
在程序中,類ReboundPanel 類的構(gòu)造方法創(chuàng)建了一個(gè)定時(shí)器(Timer)對象,Timer構(gòu)造方法的第一個(gè)參數(shù)是延遲時(shí)間(也就是間隔時(shí)間),第二個(gè)參數(shù)是要處理計(jì)時(shí)器動(dòng)作事件的監(jiān)聽器。構(gòu)造方法中還設(shè)置了圖像的初始位置,及每次移動(dòng)是垂直和水平方向的像素變化量。
監(jiān)聽器的actionPerformed()方法更新當(dāng)前的x和y坐標(biāo),然后檢查它是否會(huì)令圖像撞到面板的邊界。如果撞上,則調(diào)整運(yùn)動(dòng)方向,讓圖像按與原水平、垂直或者兩者的反方向運(yùn)動(dòng)。更新坐標(biāo)值后,actionPerformed() 方法調(diào)用repaint方法重繪組件。調(diào)用repaint方法最終又會(huì)調(diào)用paintComponent方法,它在新的位置重繪圖像。
最終效果如下:
