Como detecto colisão e escrevo na tela que ocorreu uma colisão ?
package Colisao;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class Desenho {
private static Desenho singleton;
public void desenhar() {
JanelaDesenho objeto = JanelaDesenho.getSingleton();
Canvas c = objeto.getCanvas();
Graphics g = c.getGraphics();
int i = 0;
int retangulo1 = 0;
int retangulo2 = 560;
int pos1 = 0;
int pos2 = 0;
for(i = 0; i < 560;i++) {
try {
g.setColor(Color.BLACK);
g.fillRect(retangulo1 + i, 0, 100, 100);
pos1 = retangulo1 + i;
g.setColor(Color.RED);
g.fillRect(retangulo2 - i, 0, 100, 100);
pos2 = retangulo2 - i;
Thread.sleep(20);
g.setColor(Color.WHITE);
g.fillRect(retangulo1 + i, 0, 100, 100);
g.setColor(Color.WHITE);
g.fillRect(retangulo2 - i, 0, 100, 100);
System.out.println("pos1 " + pos1 + " " + " pos2 " + pos2);
if(pos1 == pos2) {
System.out.println("Colisão !");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static Desenho getSingleton(){
if(singleton == null) {
singleton = new Desenho();
}
return singleton;
}
}