
September 2nd, 2006, 09:13 AM
|
|
Contributing User
|
|
Join Date: Aug 2006
Location: belgium
Posts: 101
Time spent in forums: 1 Day 27 m 7 sec
Reputation Power: 2
|
|
|
Collision detection
hi,
i've made a new program to check if there is a collision.
It works .... but not complety.
It only works if my oval is in the rectangle and then if i move him(rectangle) the colision is detected that isn't right.
It has to detect by his self.
The first thing you think of you read this...ha he just forgot repaint(); nope that isn't true i checked for that can someone please help me. Here is my code
Code:
import java.awt.*;
import java.applet.*;
public class col extends Applet implements Runnable
{
Image Buffer;
Graphics gBuffer;
int x, y;
boolean mouseInside, collide;
Rectangle r1, r2;
public void init()
{
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
r1=new Rectangle(110,115,60,50);
r2=new Rectangle(40,100);
}
public void drawStuff()
{
gBuffer.setColor(Color.white);
gBuffer.fillRect(0,0,size().width,size().height);
gBuffer.setColor(Color.red);
gBuffer.fillOval(r1.x, r1.y, r1.width, r1.height);
gBuffer.setColor(Color.blue);
gBuffer.fillRect(r2.x, r2.y, r2.width, r2.height);
gBuffer.setColor(Color.black);
if(collide)
r1.y = 0;
}
public boolean mouseEnter(Event evt,int x,int y)
{
mouseInside=true;
repaint();
return true;
}
public boolean mouseExit(Event evt,int x,int y)
{
mouseInside=false ;
repaint();
return true;
}
public boolean mouseMove(Event evt,int x,int y)
{
this.x=x;
this.y=y;
r2.move(x-r2.width/2, y-r2.height/2);
collide=r1.intersects(r2);
drawStuff();
repaint();
return true;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
drawStuff();
g.drawImage (Buffer,0,0, this);
}
public void start(){
Thread tbal = new Thread (this);
tbal.start();
}
public void run (){
while (true){ r1.x++; repaint();
try{
Thread.sleep (20);
}
catch (InterruptedException ex){}
repaint();
}
}
}
|