Collision not being detected
I am working on simply getting a message to come up that says "you lose" when my 2 rectangles touch each other. everything s working fine except that part!
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Rectangle;
public class finalp extends JFrame implements KeyListener, ActionListener{
public static final Component frame = null;
ImageIcon ball;
int x1;
int y1;
int x;
int y;
ImageIcon hurt;
Timer clock;
Rectangle r = new Rectangle(200,100,20,30);
Rectangle r2 = new Rectangle(800,550,20,30);
boolean collide, collide2;
public static void main(String args[]){
new finalp();
} // end main
public finalp(){
super("Final");
clock = new Timer(50, this);
this.setSize(1000, 1000);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.addKeyListener(this);
clock.start();
} // end constructor
[COLOR="Red"]public boolean intersects;{
collide = r.intersects (r2);
collide2 = r2.intersects(r);
if (collide)
JOptionPane.showMessageDialog(frame, "You Lose");
if(collide2)
JOptionPane.showMessageDialog(frame, "You Lose"); [/COLOR]
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.GREEN);
g.fillRect(r2.x,r2.y,r2.width,r2.height);
g.setColor(Color.WHITE);
g.fillRect(x, y, r.width, r.height);
} // end paint
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e){
int theKey = e.getKeyCode();
System.out.println(theKey);
if (theKey == KeyEvent.VK_LEFT){
x -= 20;
} else if (theKey == KeyEvent.VK_RIGHT){
x += 20;
} else if (theKey == KeyEvent.VK_UP){
y -= 20;
} else if (theKey == KeyEvent.VK_DOWN){
y += 20; // end if
} else if (theKey == KeyEvent.VK_DOWN){
y += 20; // end if
} repaint();
}
public void keyReleased(KeyEvent e){
}
public void actionPerformed(ActionEvent e) {
r2.y +=5;
if (r2.y > getHeight()){
r2.y = 0;
}
repaint();
}
}