-1

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.

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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    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();
    }// </editor-fold>                        


    public static void main(String args[]) {

        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* 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);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainClass().setVisible(true);
            }
        });
    }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
kami
  • 1
  • 1
  • 1
  • 1
    Instead of `posX = posX+1;` you will have to do something like `posX = posX + dx;` and when you detect a collision (x too small or big) then you do `dx = -dx;` – maraca Mar 21 '17 at 23:45
  • Before you go to much further, you need to have a look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) and [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) to better understand how painting actually works. You'll also want to have a look at [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [How to use Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for a better solution to your `while-loop` – MadProgrammer Mar 21 '17 at 23:48
  • And [this example](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788) for a possible runnable example – MadProgrammer Mar 21 '17 at 23:50
  • @MadProgrammer he is calling `move.start()` in the constructor of the `MainClass`. – maraca Mar 21 '17 at 23:52
  • @maraca Ah, that's were it was - great, now we have thread violation issues to add to the list – MadProgrammer Mar 21 '17 at 23:54

2 Answers2

1

You have to detect the collision and change the speed to other direction. To give you the direction I have changed the code and continue in the same direction.

int ballSpeedX = 1;
int ballSpeedY = 1;
int ballRadius = 20;

public void run() {
    try {
        while (true) {
            // Calculate the ball's new position
            posX += ballSpeedX;
            posY += ballSpeedY;
            if (posX < 0) {
                ballSpeedX = -ballSpeedX; // Reflect along normal
                posX = 0; // Re-position the ball at the edge
            } else if (posX + ballRadius > width) {
                ballSpeedX = -ballSpeedX;
                posX = width - ballRadius;
            }
            // May cross both x and y bounds
            if (posY < 0) {
                ballSpeedY = -ballSpeedY;
                posY = 0;
            } else if (posY + ballRadius > height) {
                ballSpeedY = -ballSpeedY;
                posY = height - ballRadius;
            }
            repaint();
            move.sleep(10);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Sumit Gulati
  • 665
  • 4
  • 14
0

I think the key is to replace the line

posX = posX+1;

with

posX = posX + velocityX;

if you set velocityX to -1, the ball moves left. if you set velocityX to 1, the ball moves right.

when it hits a wall, reverse the direction with

velocityX *= -1;

...do the same for Y too, of course.