title: Java Graphics "Bouncing Ball" Program
body: <p>This is my first Graphical Java program and what I'm trying to do is re-create a simple classic program where I have multiple balls bouncing in a <code>JFrame</code> window.</p>
  
  <p>So far I have successfully been able to get one ball to bounce around using code inside the <code>run()</code> method. That works for one ball object that I create, but now I want to have many balls so I'm trying to create a method in my <code>Ball</code> class that will make each ball object that I create bounce in my "ball world" independently.</p>
  
  <p>Right now all I care about is them bouncing off the walls, not each other (I will figure that out later).</p>
  
  <p><strong>The problem:</strong> In my <code>ballMove(int, int, int, int)</code> method I have four <code>int</code> parameters where the first two parameters are the <code>width</code> and <code>height</code> of the <code>window</code>, and the last two parameters are the <code>Xspeed</code>, and <code>Yspeed</code>. When I go through my <code>if statements</code> it will temperately set the <code>x</code> and <code>y speed</code> parameters to negative when the ball hits the right or bottom wall, but when the <code>run()</code> method executes the <code>ballMove(int, int, int, int)</code> method again, they go back to being positive and the balls disappear from the window. I have tried using a bunch of <code>getter</code> and <code>setter</code> methods in my ball class. I have tried temporary variables within my <code>ballMove(int, int, int, int)</code> method. Nothing I've tried works.</p>
  
  <p><strong>Question:</strong> By using my <code>Ball</code> class method, how do I prevent my parameters <code>Xspeed</code> and <code>Yspeed</code> from reinitializing my instance speed variables to positive when the ball(s) collide with the walls?</p>
  
  <p>Because I'm new to graphical programming any helpful suggestions would be greatly appreciated. </p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.util.Random;
  
  import javax.swing.JFrame;
  
  public class Main extends JFrame implements Runnable {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private int width = 800;
      private int height= 600;
      private int ballRadius = 50;
  
      private Random rand = new Random();
  
      //Create and initialize a ball object
      public Ball ball = new Ball(Color.BLUE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
      //public Ball ball2 = new Ball(Color.RED, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
      //public Ball ball3 = new Ball(Color.GREEN, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
      //public Ball ball4 = new Ball(Color.ORANGE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
      //public Ball ball5 = new Ball(Color.YELLOW, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
      //constructor
      public Main(){
  
          setSize(width, height);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
  
      }
      //Paint the ball(s)
      public void paint(Graphics g){
          super.paint(g);
          g.setColor(ball.getColor());
          g.fillOval(ball.getBallX(), ball.getBallY(), ball.getWidth(), ball.getHeight());
          //g.setColor(ball2.getColor());
          //g.fillOval(ball2.getBallX(), ball2.getBallY(), ball2.getWidth(), ball2.getHeight());
          //g.setColor(ball3.getColor());
          //g.fillOval(ball3.getBallX(), ball3.getBallY(), ball3.getWidth(), ball3.getHeight());
          //g.setColor(ball4.getColor());
          //g.fillOval(ball4.getBallX(), ball4.getBallY(), ball4.getWidth(), ball4.getHeight());
          //g.setColor(ball5.getColor());
          //g.fillOval(ball5.getBallX(), ball5.getBallY(), ball5.getWidth(), ball5.getHeight());
      }
      //Run the program
      public static void main(String [] args){
          Main main = new Main();
          main.setVisible(true);
          main.run();
  
      }
  
      @Override
      public void run() {
          // TODO Auto-generated method stub
  
  
          while(true){
              try {
                  Thread.sleep(10);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              ball.ballMove(width, height, 20, 5);
              repaint();
              //ball2.ballMove(width, height, 15, 3);
              //repaint();
              //ball3.ballMove(width, height, 3, 20);
              //repaint();
              //ball4.ballMove(width, height, 10, 10);
              //repaint();
              //ball5.ballMove(width, height, 10, 20);
              //repaint();
  
  
          }
      }
  
  
  }
  </code></pre>
  
  <p>Here is my <code>Ball</code> class</p>
  
  <pre><code>import java.awt.Color;
  
  import javax.swing.JFrame;
  
  
  public class Ball extends JFrame {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private int width, height, ball_X, ball_Y;
      private int Xspeed;
      private int Yspeed;
      private Color color;
      public Ball(Color color, int width, int height, int ball_X, int ball_Y){
          this.width = width;
          this.height = height;
          this.color = color;
          this.ball_X = ball_X;
          this.ball_Y = ball_Y;
      }
      public Color getColor(){
          return this.color;
      }
      public int getWidth(){
          return this.width;
      }
      public int getHeight(){
          return this.height;
      }
      public int getBallX(){
          return this.ball_X;
      }
      public int getBallY(){
          return this.ball_Y;
      }
      public void setSpeedX(int x){
          this.Xspeed = x;
      }
      public void setSpeedY(int x){
          this.Yspeed = x;
      }
      public int getSpeedX(){
          return this.Xspeed;
      }
      public int getSpeedY(){
          return this.Yspeed;
      }
      public void setBallX(int x){
          this.ball_X = x;
      }
      public void setBallY(int y){
          this.ball_Y = y;
      }
      public void ballMove(int X, int Y, int xSpeed, int ySpeed){
  
          //initialize Xspeed and Yspeed with the parameters of the function
          this.setSpeedX(xSpeed);
          this.setSpeedY(ySpeed);
          //Moves the balls by adding the set speed to the position of the balls each time thread is executed
          this.setBallX(this.getBallX() + this.getSpeedX());
          this.setBallY(this.getBallY() + this.getSpeedY());
          //When the balls hit the walls they are suppose to bounce back until they hit another wall.
          if(this.getBallX() + 50 &gt;= X){
              this.setSpeedX(-xSpeed);
          }
          if(this.getBallY() + 50 &gt;= Y){
              this.setSpeedY(-ySpeed);
          }
          if(this.getBallX() + 25 &lt;= 0){
              this.setBallX(xSpeed);
          }
          if(this.getBallY() + 25 &lt;= 0){
              this.setSpeedY(ySpeed);
          }
      }
  }
  </code></pre>