弹跳的⼩球java课程设计_实例学习:弹跳的⼩球## 题⽬要求 ##
⼩球在⾯板上弹跳。使⽤两个按钮暂停和重新开始⼩球的运动,使⽤⼀个滚动条控制⼩球弹跳的速度。
## 主要步骤 ##
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.d);
// Check boundaries
if (x < 0 || x > getWidth())
dx *= -1;
if (y < 0 || y > getHeight())
dy *= -1;
// Adjust ball position
x += dx;
htmlbordery += 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+d
y).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.d));
jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
ball.Maximum());
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) {
}
});
jsbDelay.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
ball.Maximum() - e.getValue());
}
});
}
}
## 解释 ##
BallControl类拓展JPanel类,显⽰⼀个球,⼀个滚动条。当点击Suspend按钮时,就会调⽤Suspend()⽅法使球暂停运动,及执⾏语句ball.suspend();当点击Resume按钮时,就会调⽤resume()⽅法使球重新开始运动(sume()).可以使⽤滚动条改变⼩球弹跳的速度。
## BounceBallApp.java ##
import java.awt.*;
import javax.swing.*;
public class BounceBallApp extends JApplet{ public BounceBallApp()
{
add(new BallControl());
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。