0

I am trying to create a java program, using Netbeans, with two balls (one position at the top and the other bottom) and when executed they move in the opposite direction and go offscreen.

The original code was given to us using one ball and we were asked to add a second panel hence the confusion within the code.

My problem is when I execute the code using BoxLayout.Y_AXIS the balls meet in center and disappear due to the panel arrangement. I want the balls to cross the center into the other panels.

I have tried using border layout, but i loose one ball.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class RandomBall {

    JFrame frame;
    public int x = 0;
    public int y = 0;
    public int z = 300;
    public int deltaX;
    public int deltaY;
    public int deltaZ;
    public int posNeg;
    public int diameter = 50;
    final static public int MULT = 5;
    Ball1DrawPanel drawPanel1;
    Ball2DrawPanel drawPanel2;
    JPanel pan;

    public static void main(String[] args) {
        RandomBall gui = new RandomBall();
    }

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        drawPanel1 = new Ball1DrawPanel();
        drawPanel2 = new Ball2DrawPanel();
        pan = new JPanel();
        pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
        pan.add(BorderLayout.NORTH, drawPanel1);
        pan.add(BorderLayout.SOUTH, drawPanel2);

        frame.getContentPane().add(BorderLayout.CENTER, pan);

        frame.setSize(500, 500);
        frame.setVisible(true);

        deltaX = (int) (Math.random() * MULT);  //randomly set the displacement in the  x direction (across)
        deltaY = (int) (Math.random() * MULT);  //randomly set the displacement in the y direction (down)
        deltaZ = (int) (Math.random() * MULT);  //randomly set the displacement in the y direction (down)

        while ((deltaX == 0) && (deltaY == 0)) {
            deltaX = (int) (Math.random() * MULT); //to prevent both values being zero - ball will not move
            deltaY = (int) (Math.random() * MULT);
            deltaZ = (int) (Math.random() * MULT);
        }

        posNeg = (int) (Math.random() * 2);
        if (posNeg == 0) {
            deltaX = deltaX * -1;  //randomly set the direction to left or right
        }
    }

    public RandomBall() {
        go();
    }

    class Ball1DrawPanel extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
            g.setColor(Color.red);
            g.fillOval((this.getWidth() + (x) - diameter) / 2, (0 + (y)) / 2, diameter, diameter);
            frame.repaint();
            x = x + deltaX;
            y = y + deltaY;
        }
    }

    class Ball2DrawPanel extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
            g.setColor(Color.BLUE);
            g.fillOval((this.getWidth() + (x) - diameter) / 2, (0 + (z)) / 2, diameter, diameter);
            frame.repaint();
            x = x + deltaX;
            z = z - deltaZ;
        }
    }
}

Is there any layout, or any implementation, I can use that will allow the balls to cross into the opposite applet rather than breaking at the center when the collide with the other?

Alexxio
  • 1,091
  • 3
  • 16
  • 38
  • 3
    `public void paintComponent(Graphics g) { try { Thread.sleep(10); } catch (Exception e) { }...`: Don't ever sleep on the EDT. This pattern is just plain wrong. – Guillaume Polet Mar 12 '13 at 18:03
  • I agree, however the question stipulates that we simply edit the code to get another ball to appear without any further editing. – user2162224 Mar 12 '13 at 18:35
  • Could you post the original code that you are editing? May be just create another instance of a ball would suffice. – Manidip Sengupta Mar 12 '13 at 18:44
  • I wanted to do that but the question states that we create another ball, Ball2DrawPanel, submitting one .java file. – user2162224 Mar 12 '13 at 19:12
  • 2
    This is the second time this EXACT question, the EXACT same mistakes, has been asked. Check [here](http://stackoverflow.com/questions/15352969/drawing-2-balls-to-move-in-different-direction-on-java-but-one-disappeared/15353052#15353052) for the answer. – MadProgrammer Mar 12 '13 at 21:09
  • Also, don't use a layout manager, it is responsible for positioning the components and will override you efforts – MadProgrammer Mar 12 '13 at 21:17
  • possible duplicate of [Something seems wrong with the layout, JButton showing unexpected behaviour at resize of the window](http://stackoverflow.com/questions/9849950/something-seems-wrong-with-the-layout-jbutton-showing-unexpected-behaviour-at-r) – trashgod Apr 14 '13 at 23:28

2 Answers2

0

I appreciate all the help, but did some research and managed to find the solution to my question ..

I created a second panel,

pan = new JPanel(); 
pan.setLayout(new OverlayLayout(pan));

After this, I just added the two balls to the "pan" panel and VOILAAAA..

Again thanks for all the help..

-3

My suggestions:

  1. Extend a JApplet if you are writing an applet
  2. Use the Applet's paint method for the graphics
  3. Leave the JFrame in main() for testing only
Manidip Sengupta
  • 3,573
  • 5
  • 25
  • 27
  • My apologies, I mean to write java program (not applet). I would prefer to use Applet, but due to constraints I am not allowed too. – user2162224 Mar 12 '13 at 18:37
  • 3
    -1, do not override the paint() of a JApplet. Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you add the panel to the applet. – camickr Mar 12 '13 at 19:03
  • Normally I'd put a JPanel in getContentPane() and the JApplet's paint(Graphics g) method is a one-liner "super.paint(g);" That is still overriding, isn't it? Just for my reference, this is irrelevant here because the OP is not using an applet. – Manidip Sengupta Mar 12 '13 at 20:29