当前位置: 首页 > 面试题库 >

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

长孙永思
2023-03-14
问题内容

我正在尝试编写关于弹跳球的代码,但是我仍然沉迷于如何
使弹跳球。该代码似乎是正确的,日食没有错误消息
,但是球还是没有动。任何帮助/提示表示赞赏。

这是我的代码:

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

}

问题答案:

基本上,什么都没有移动。

每次SwingTimer滴答作响,您要做的就是重新粉刷。

您需要将移动逻辑移至actionPerformedActionListener注册的方法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为的内部类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();
        }
    }
}


 类似资料:
  • 我想了两个小时,为什么这段代码不能产生预期的结果。如果我输入3个整数,比如3、4和5,它应该给出所有27个可能的和(假设数字可以是正的、负的或零) 因此,它应该产生以下内容: -3-4-5=-12 -3-4 0 = -7 -4-4 5=3 等等

  • Stage.close()对我不起作用。 我查看了:JavaFX2.0:关闭一个舞台(窗口) 这是我的代码: 下面是调用消息框类的代码:

  • 我正在尝试检测我的两个精灵何时发生碰撞。我做的第一件事是在我的播放器周围创建一个矩形(称为player.img),然后在我想检测的树周围创建另一个矩形(称为背景.treesrect)。我将玩家矩形的坐标设置为等于当用户按下键移动时更新的坐标,但玩家矩形不移动。然后我使用精灵.碰撞(精灵)函数来检测它们是否碰撞并且没有检测到。有人可以向我展示为什么我的播放器矩形没有更新以及其他任何可能错误的内容吗?

  • 问题内容: 我在这里有点困惑。如果我将变量传递给json_decode,它将不起作用: 第一个回显正确显示了我传递的JSON字符串,例如 第二个回显显示NULL。因此,我从第一个回显中获取了字符串,并编写了以下代码: 你怎么说,它向我展示了正确解码的数组。字符串绝对相同,我什至保留转义字符。也许是问题所在? 问题答案: 看起来您的服务器已启用。无论是将其禁用或运行通过使用它之前。

  • 因此,下面的代码,从txt文件中取序列号作为参数,在我的计算机上正常工作。每个数字都写在一行上。下面是代码: 但它在CodeEval中不起作用。站点编译器是这么说的: Fontconfig错误:无法加载默认配置文件线程“main”java.awt.HeadLessException:未设置X11显示变量,但此程序执行了需要它的操作。在java.awt.GraphicsEnvironment.Che