## 题目要求 ##
小球在面板上弹跳。使用两个按钮暂停和重新开始小球的运动,使用一个滚动条控制小球弹跳的速度。
## 主要步骤 ##
1)创建一个JPanel的子类,将它命名为Ball,用来显示小球的弹跳,
2)创建一个JPanel的子类,将它命名为BallControl包含一个小球,一个滚动条,两个控制按钮,
## Ball.java ##
import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ball extends JPanel {
private int delay = 10;
// Create a timer with delay 1000 ms
private Timer timer = new Timer(delay, new TimerListener());
private int x = 0; private int y = 0; // Current ball position
private int radius = 5; // Ball radius
private int dx = 2; // Increment on ball's x-coordinate
private int dy = 2; // Increment on ball's y-coordinate
public Ball() {
timer.start();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
// Check boundaries
if (x < 0 || x > getWidth())
dx *= -1;
if (y < 0 || y > getHeight())
dy *= -1;
// Adjust ball position
x += dx;
y += dy;
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
}
public void suspend() {
timer.stop(); // Suspend timer
}
public void resume() {
timer.start(); // Resume timer
}
public void setDelay(int delay) {
this.delay = delay;
timer.setDelay(delay);
}
}
## 解释 ##
Ball类拓展JPanel类来显示一个移动的小球,定时器监听器ActionListener 来监听ActionEvent事件,(TimerListener).语句private Timer timer =nerw Timer(delay,new TimerListener())的功能是穿件一个Timer对象,当构造一个Ball的实例时就会启动语句timer.start()的定时器,这个定时器以固定的频率触发ActionEvent事件。监听器在语句repant()做出相应重画小球,已得到小球运动的动画。球的中心坐标(X,Y)处,它在下一次显示时就会变为(x+dx,y+dy).suspend方法和resume方法可以用来停止和启动定时器。setDelay(int) 方法设置新的延时。
## BallControl.java ##
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class BallControl extends JPanel {
private Ball ball = new Ball();
private JButton jbtSuspend = new JButton("Suspend");
private JButton jbtResume = new JButton("Resume");
private JScrollBar jsbDelay = new JScrollBar();
public BallControl() {
// Group buttons in a panel
JPanel panel = new JPanel();
panel.add(jbtSuspend);
panel.add(jbtResume);
// Add ball and buttons to the panel
ball.setBorder(new javax.swing.border.LineBorder(Color.red));
jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
ball.setDelay(jsbDelay.getMaximum());
setLayout(new BorderLayout());
add(jsbDelay, BorderLayout.NORTH);
add(ball, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
// Register listeners
jbtSuspend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ball.suspend();
}
});
jbtResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ball.resume();
}
});
jsbDelay.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
ball.setDelay(jsbDelay.getMaximum() - e.getValue());
}
});
}
}
## 解释 ##
BallControl类拓展JPanel类,显示一个球,一个滚动条。当点击Suspend按钮时,就会调用Suspend()方法使球暂停运动,及执行语句ball.suspend();当点击Resume按钮时,就会调用resume()方法使球重新开始运动(ball.resume()).可以使用滚动条改变小球弹跳的速度。
## BounceBallApp.java ##
import java.awt.*;
import javax.swing.*;
public class BounceBallApp extends JApplet{
public BounceBallApp()
{
add(new BallControl());
}
}