0

I'm trying to develop a game, it's one of my assignments. In this game, I have several ball draw using pain and I have 3 panels. One plays the role of paddle and 2 others play the roles of obstacles. So I have an issue when trying to detect the collision between the balls and the panel (paddle and obstacle), and another issue is when the collision occurs the ball must move in a different position like the reflection of light on an object.

Find down my code:

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BallWindow extends JFrame implements ActionListener{
JButton btnStop = new JButton("STOP");
JButton btnSave = new JButton("SAVE");
Vector<BallP> ballVector = new Vector<BallP>();

JPanel p1 = createPanel(280, 200, 200, 20, Color.gray);
JPanel p2 = createPanel(280, 300, 200, 20, Color.gray);


JPanel lborder = createPanel(10, 10, 2, 560, Color.black);
JPanel rborder = createPanel(720, 10, 2, 560, Color.black);
JPanel tborder = createPanel(10, 10, 710, 2, Color.black);

//Movement
private static final int Y = 500;
private static final int WIDTH = 60;
private static final int HEIGHT = 10;
int x = 10;
int xa = 0;
JPanel bottomp = createPanel(x, Y, WIDTH, HEIGHT, Color.black);
public BallWindow() {
addKeyListener(new KeyListener()
{

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        if (e.getKeyCode() == KeyEvent.VK_LEFT)
            bottomp.setLocation(bottomp.getLocation().x -6, bottomp.getLocation().y);
            
        if (e.getKeyCode() == KeyEvent.VK_RIGHT)
            bottomp.setLocation(bottomp.getLocation().x + 6, bottomp.getLocation().y);
    
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    
        
    }

});
setFocusable(true);


setLayout(null);
btnStop.setBounds(12, 15, 100, 30);
btnStop.addActionListener(this);
add(btnStop);
btnSave.setBounds(12, 50, 100, 30);
//btnSave.addActionListener(this);
add(btnSave);
Random r = new Random();
for(int i=0; i<5; i++){
    BallP bp = new BallP(r.nextInt(740), r.nextInt(590));
    Thread t = new Thread(bp);
    ballVector.add(bp);
    t.start();
    add(bp);
}


 add(p1);
 add(p2);
 Rectangle b = bottomp.getBounds();

 add(bottomp);
 //    add(lborder);
 //    add(rborder);
 //    add(tborder);

 setSize(740, 570);
 setVisible(true);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setResizable(false);
 repaint();
 }


 JPanel createPanel(int x, int y, int width, int height, Color pColor){
 JPanel temp = new JPanel();
 temp.setBackground(pColor);
 temp.setBounds(x, y, width, height);
 return temp;    
 }



 public static void main(String[] args) {
 new BallWindow();

 }


 @Override
 public void actionPerformed(ActionEvent arg0) {
 for (BallP ball : ballVector) {
        ball.pleaseWait = !ball.pleaseWait;
    }

    if( btnStop.getText().equalsIgnoreCase("STOP"))
        btnStop.setText("START");
    else
        btnStop.setText("STOP");

 //  if(arg0.getSource())
 }

 }

Here is the code for the ball

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JPanel;

public class BallP extends JPanel implements Runnable {

int RED, GREEN, BLUE;
int Xdirection = 1, Ydirection = 1;
boolean pleaseWait = false;

BallP(int X, int Y){
locateBall(X, Y, 30, 30);               
/*  Random r = new Random();
RED = r.nextInt(255);
GREEN = r.nextInt(255);
BLUE = r.nextInt(255);
*/
}   
public void paint(Graphics g){
int panelWidth = this.getWidth();
int panelHeight = this.getHeight();     

//  g.setColor( new Color(RED, GREEN, BLUE ));
g.setColor(Color.ORANGE);

g.fillOval(panelWidth/2, panelHeight/2,panelWidth/2, panelHeight/2);
}

public void locateBall(int x, int y, int width, int height){

setBounds(x, y, width, height);
repaint();
}

public void run() {

int width = this.getWidth();
int height = this.getHeight();
Random r = new Random();

while(true){
    if(!pleaseWait){
        int lastX = this.getX();
        int lastY = this.getY();

        if (lastX > 675) Xdirection = -1;
        if (lastY > 485) Ydirection = -1;
        if (lastX < -5) Xdirection = 1;
        if (lastY < -5) Ydirection = 1;

/*      if(lastX > 280 && lastY > 170){
            Xdirection = -1;
            Ydirection = -1;
        }
*/      
        locateBall(lastX + Xdirection*r.nextInt(3), 
                   lastY + Ydirection*r.nextInt(3), 
                   width, height );
    }
    try{
        Thread.sleep(5);
    }catch(Exception e){};
}
}
}
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
  • 1
    Many painting issue: 1) override `paintComponent(...)`, not paint() 2) don't invoke repaint() from a painting method. 3) Don't use a "while (true)" loop. Instead use a `Swing Timer` for animation. Don't use a null layout. Swing was designed to be used with layout managers. Check out https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681 which can help address these issues. The basic idea is keep an ArrayList of objects to paint. – camickr May 17 '22 at 14:18

0 Answers0