title: make a ball bounce off a wall java
body: <p>For a school project I need to make a ball bounce off of the walls of the window I have created, but no matter what i try i cant seem to implement it and have it work. code is below and thank you in advance.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import static java.nio.file.Files.move;
  
  
  public class MainClass extends javax.swing.JFrame implements Runnable {
        Thread move = new Thread (this);
        int width;
        int height;
        int posX;
        int posY;
  
  public void run(){
      try {
         while (true){
          posX = posX+1;
          posY = posY+1;
          repaint();
          move.sleep(100);
       }
      }
      catch(Exception ex){
        ex.printStackTrace();
      }
    }
  
      public void paint(Graphics g) {
  
          g.setColor(Color.BLACK);
          g.fillRect(0, 0, width, height);
          g.setColor(Color.WHITE);
          g.fillOval(posX, posY/ 2, 20, 20);
      }
  
      public MainClass() {
          initComponents();
          this.setResizable(false);
          width = getWidth();
          height = getHeight();
          posX = width - 150;
          posY = height - 150;
          move.start(); 
      }
  
  
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 400, Short.MAX_VALUE)
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 300, Short.MAX_VALUE)
          );
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
  
      public static void main(String args[]) {
  
          /* Set the Nimbus look and feel */
          //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
          /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException ex) {
              java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //&lt;/editor-fold&gt;
  
          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new MainClass().setVisible(true);
              }
          });
      }
  </code></pre>