CVYC CVYC
CVYC CVYC

Reputation: 75

Reversing JFrame Bounds

I am attempting to make a bouncing ball. The bounds are set that allows the ball to begin from the starting point once it exceeds the frame bounds. I can not get the ball to bounce. How do I get the ball to bounce once the bounds (outside edges of the frame) are hit? I believe the problem is in the moveBall() method.

Main Class import javax.swing.JFrame;

public class MainForm {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(220, 270);
        mainFrame.setVisible(true);
    }

}

2nd class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {

    private CustomPanel _panel;

    public MainFrame()
    {
        super("Bouncing Ball");

        _panel = new CustomPanel();
        add(_panel, BorderLayout.CENTER);
    }
}

3rd class

import java.awt.Graphics;
import javax.swing.JPanel;


public class CustomPanel extends JPanel{

    private final BouncingBall _ball1;
    private final BouncingBall _ball2;
    private final BouncingBall _ball3;

    public CustomPanel ()
    {
        _ball1 = new BouncingBall(this, 20, 3);
        Thread thread1 = new Thread(_ball1);
        _ball2 = new BouncingBall(this, 40, 6);
        Thread thread2 = new Thread(_ball2);
        _ball3 = new BouncingBall(this, 60, 9);
        Thread thread3 = new Thread(_ball3);

        thread1.start();
        thread2.start();
        thread3.start();
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        _ball1.draw(g);
        _ball2.draw(g);
        _ball3.draw(g);
    }
}

4th class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;


public class BouncingBall implements Runnable {

    private JPanel _panel;
    private int _xComponent;
    private int _yComponent;
    private int _sleepDelay;

    public BouncingBall(JPanel panel, int startingYPosition, int sleepDelay)
    {
        _panel = panel;
        _xComponent = 0;
        _yComponent = startingYPosition;
        _sleepDelay = sleepDelay;
    }

    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(_sleepDelay);

                moveBall();
                _panel.repaint();
            }

            catch (InterruptedException ex)
            {
                Logger.getLogger(BouncingBall.class.getName()).log(Level.SEVERE,null, ex);
            }
        }
    }

    public void draw(Graphics g)
    {
        g.setColor(new Color(255, 0, 0));
        g.fillOval(_xComponent, _yComponent, 20, 20);
    }

    private void moveBall()
    {
        _xComponent += 2;

        Rectangle bounds = _panel.getBounds();

        if(_xComponent >= bounds.width)
            _xComponent = -_xComponent;
    }
}

Upvotes: 1

Views: 257

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109593

You need to reverse the speed (steps).

Assuming the bounds are relative, at (0, 0), and one starts inside the bounds.

    x += dx;
    y += dy;

    if (x < 0 || x > bounds.width) {
        dx = -dx;
        x += dx;
    }
    if (y < 0 || y > bounds.height) {
        dy = -dy;
        y += dy;
    }

(x short hand for _xComponent)

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32535

_xComonent is top left corner of ball component box. So to "detect collision" from right and botton of bound you would have to

if(_xComponent+ball.width >= bounds.width)

insteed of

if(_xComponent >= bounds.width).

The same would have to be done with height to "collide" with bottom. Top and left colliisions can be detected in straightforward way.

Now another thing is when u invert your _xComponentinsteed of subtracting from /add to it, you will move your "ball" of of bound in no time. Remember that (0,0) is the left top corner of compoent/screen, and not its center. So basicly all negative components of point will result in out-of-the-screen drawing.

Here you got some info about Swing's coordinate system straight from Oracle. https://docs.oracle.com/javase/tutorial/2d/overview/coordinate.html

Upvotes: 0

Related Questions