为什么我的代码“Bouncing ball”不起作用?

我正在尝试编写一个关于弹跳球的代码,但我仍然坚持如何让球反弹。 代码似乎是正确的,没有来自eclipse的错误消息,但球不动。 任何帮助/提示都很感激。

这是我的代码:

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

基本上,没有任何动球。

每次Swing Timer滴答时,你所做的只是重绘。

您需要将移动逻辑移动到注册到TimerActionListeneractionPerformed方法

更像…

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

这样,每次Timer滴答时,你都会相应地更新球的位置……

更新了工作示例

我做了两处改动。 我让你的TimerListener成为Ball的内部类,它允许它访问Ball的变量和方法并修改你的运动逻辑,使它工作

弹跳

 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(); } } } 
 private Timer timer = new Timer(20, new TimerListener()); 

不是new TimerListener()需要做一些有用的事情吗? 不应该在每次计时器滴答时发生StartBouncingBallTest内的大多数代码,在计时器启动时只有一次intsead?

你从来没有宣称g.fillOval是球在这里做这个…

 g.drawOval((int)applex, (int)appley, (int)appleradius, appleradius); public double applex = ArandomX; //replace the random with values.. public double appley = ArandomY; //same here public int appleradius = 10; 

这让它移动……

 a.px += a.vx; // check for boundaries if (a.px >= this.getWidth() || a.px <= 0) { // reverse vel and apply a.vx = -a.vx; a.px += -a.vx; // to prevent sticking } //a.vx += -gravity; // add this constant to accelerate things down a.py += a.vy; // check for boundaries if (a.py >= this.getHeight() || a.py <= 0) { // reverse vel and apply a.vy = -a.vy; a.py += a.vy; // to prevent sticking }` 

用你的x或dx idk替换a.vx,因为你的变量不清楚,是的,只需用y或dy替换a.vy,并为我的px做同样的事情......它会起作用