问题描述
我正在尝试编写一个反弹弹球的代码,但我被困在如何使球弹起来。该代码似乎是正确的,没有错误的消息从日食,但球不动。
I'm trying to write a code a bout a bouncing ball, but i'm stuck in how to make the ball bounced. The code seems correct, no wrong messages from eclipse and yet the ball doesn't move. Any help/hint is appreciate.
这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BouncingBallTest extends JFrame {
private JButton jbtStart = new JButton("Start");
private JButton jbtStop = new JButton("Stop");
private Ball canvas = new Ball();
public BouncingBallTest() {
JPanel panel = new JPanel(); // Use the panel to group buttons
panel.add(jbtStart);
panel.add(jbtStop);
add(canvas, BorderLayout.CENTER); // Add canvas to centre
add(panel, BorderLayout.SOUTH); // Add panel to south
// register listener
jbtStart.addActionListener(new StartBouncingBallTest());
jbtStop.addActionListener(new StopBouncingBallTest());
}
// the main method
public static void main(String[] args) {
JFrame frame = new BouncingBallTest();
frame.setTitle("Bouncing Ball Test");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 300);
frame.setVisible(true);
}
class StartBouncingBallTest implements ActionListener { // inner class
@Override
public void actionPerformed(ActionEvent e) {
canvas.StartBouncingBallTest();
}
}
class StopBouncingBallTest implements ActionListener { // inner class
@Override
public void actionPerformed(ActionEvent e) {
canvas.StopBouncingBallTest();
}
}
class Ball extends JPanel {
private int radius = 10;
private int x;
private int y;
private int dx = 3;
private int dy = 3;
private Timer timer = new Timer(20, new TimerListener());
public void StartBouncingBallTest() {
if (x > 0 && y > 0) {
x += dx;
y += dy;
}
else if (x == 0) {
x -= dx;
y += dy;
}
else if (y + (2 * radius) > getHeight()) {
x += dx;
y -= dy;
}
else if (y == 0) {
x += dx;
y -= dy;
}
else if (x + (2 * radius) > getWidth()) {
x -= dx;
y += dy;
}
repaint();
timer.start();
}
public void StopBouncingBallTest() {
timer.stop();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
}
class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}
推荐答案
基本上没有什么是移动球。
Basically, nothing is moving the ball.
每次Swing 计时器
你所做的只是重绘。
Each time the Swing Timer
ticks, all you do is repaint.
你需要将运动逻辑移动到 actionPerformed
c $ c> ActionListener 注册到计时器
You need to move the movement logic to the actionPerformed
method of the ActionListener
registered to the Timer
更像...
public void StartBouncingBallTest() {
timer.start();
}
//...
class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (x > 0 && y > 0) {
x += dx;
y += dy;
}
else if (x == 0) {
x -= dx;
y += dy;
}
else if (y + (2 * radius) > getHeight()) {
x += dx;
y -= dy;
}
else if (y == 0) {
x += dx;
y -= dy;
}
else if (x + (2 * radius) > getWidth()) {
x -= dx;
y += dy;
}
repaint();
}
}
这样,每次
This way, each time the Timer
ticks, you are making update the position of the ball accordingly...
使用实例更新
我进行了两次更改。我使你的 TimerListener
一个内部类 Ball
,它允许它访问<$ c $的变量和方法c> Ball 并修改您的移动逻辑,以使其正常工作
I made two changes. I made your TimerListener
an inner class of Ball
, which allows it to access the variable and methods of Ball
and modified your movement logic so it works
class Ball extends JPanel {
private int radius = 10;
private int x;
private int y;
private int dx = 3;
private int dy = 3;
private Timer timer = new Timer(20, new TimerListener());
public void StartBouncingBallTest() {
timer.start();
}
public void StopBouncingBallTest() {
timer.stop();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (x < 0 || x + (2 * radius) > getWidth()) {
dx *= -1;
}
if (y < 0 || y + (2 * radius) > getHeight()) {
dy *= -1;
}
x += dx;
y += dy;
repaint();
}
}
}
这篇关于为什么我的代码“弹跳球”不行吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!