Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make these 2 rectangles collide and BOUNCE back

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 330
    Answer it

    For the code below I managed to make these rectangles collide and bound at the Jpanel border, but why is that these rectangles do not bound each other when they collide, instead they overlapped each other?
    I think there is a problem in my actionPerformed method.

    My Questions are,

    Please tell me why they do not bounce back and what should I do to overcome it.
    Below is the code. Thank you.

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Rectangle;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
     
    public class f6 {
         
    public static void main(String args[]) {
            objectFrame frame = new objectFrame();
     
     
    }   
    }
     
    class objectFrame extends JFrame {
     
    objectFrame() { 
         
            objectPanel panel = new objectPanel();
             
            this.setSize(500, 400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            this.add(panel);
             
             
        }
    }
     
     
    class objectPanel extends JPanel implements ActionListener{
        private Timer t;
        private int x, velx, x2, velx2;
        private Rectangle r1, r2;
         
         
     
    objectPanel(){
         
        t = new Timer(5, this);
        x = 100;
        x2 = 400;
        velx = 1;
        velx2 = 1; 
        r1 = new Rectangle(x, 10, 50, 30);
        r2 = new Rectangle(x2, 10, 50, 30);
         
        t.start();
         
    }   
         
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
         
       Rectangle r1 = new Rectangle(x, 10, 50, 30);
        Rectangle r2 = new Rectangle(x2, 10, 50, 30);
         
     
           g.setColor(Color.BLUE);
            g.fillRect(x, 10, 50, 30);
               
            g.setColor(Color.RED);
            g.fillRect(x2, 10, 50, 30);
             
            if(r1.intersects(r2)) {
                     g.drawString("hittt" , 250, 300);
                 
             }
    }
         
     
         
    public void actionPerformed(ActionEvent e) {
     
          
    if (x < 0 || x > 450 || r1.intersects(r2)) {
                velx = -velx;
            }
            if (x2 < 0 || x2 > 450 || r1.intersects(r2)){
                velx2 = -velx2;
            }
                
         
         
           x += velx;
           x2 -= velx2;
         
        repaint();
         
    }

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: