-2

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.

Here's my code:

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

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Kurt16
  • 35
  • 2
  • 10

3 Answers3

7

Basically, nothing is moving the ball.

Each time the Swing Timer ticks, all you do is repaint.

You need to move the movement logic to the actionPerformed method of the ActionListener registered to the Timer

More like...

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...

Updated with working example

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

Bounce

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();
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • you're right..it does. I had a wrong placement of a brace when i tried first your method. To make "TimerListener an inner class of Ball"..i'have (never) thought to it!!..and Thanks a lot for helping out. – Kurt16 Jan 18 '14 at 11:10
0
private Timer timer = new Timer(20, new TimerListener());

doesn't that new TimerListener() need to do something useful? shouldn't most of the code inside StartBouncingBallTest be happening every time the timer ticks, intsead of just once when the timer starts?

John Gardner
  • 24,225
  • 5
  • 58
  • 76
  • Yes, but that's only half the problem. If that code were run repeatedly, the ball would move to (-3, 3) and then get stuck. So this won't solve the OP's issue. – Dawood ibn Kareem Jan 18 '14 at 00:39
  • well, the op's original problem was that nothing was happening. that would be not nothing, and hopefully let the OP troubleshoot from there. – John Gardner Jan 18 '14 at 00:44
0

you never declared that the g.fillOval was the ball here do this...

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;

this makes it move...

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
        }`

replace a.vx with your x or dx idk because your variables are unclear and yeah just replace a.vy with either y or dy and do the same for my px... it will work