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

时间:2014-01-17 23:47:51

标签: java swing events user-interface actionlistener

我正在尝试编写一个关于弹跳球的代码,但我仍然坚持如何让球反弹。代码似乎是正确的,没有来自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();
        }
    }

}

3 个答案:

答案 0 :(得分:7)

基本上,没有任何动球。

每次Swing Timer勾选时,您所做的就是重新绘制。

您需要将移动逻辑移动到注册到actionPerformed的{​​{1}}的{​​{1}}方法

更像是......

ActionListener

这样,每次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(); } } 成为Timer的内部类,它允许它访问TimerListener的变量和方法并修改你的移动逻辑以使其有效

Bounce

Ball

答案 1 :(得分:0)

private Timer timer = new Timer(20, new TimerListener());

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

答案 2 :(得分:0)

你从来没有声明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做同样的事情......它会起作用