Reputation: 25
I am working on a game similar to bricks, and I want something to disappear off the screen when it is hit. I have gotten my collision detection done, here is the code:
Rectangle r1 = new Rectangle(x,y, 100,25);
Rectangle r2 = new Rectangle(120,15, 90,40);
Rectangle r3 = new Rectangle(10,15, 90,40);
Rectangle r4 = new Rectangle(230, 15, 90, 40);
Rectangle r5 = new Rectangle(xb, yb, 30,29);
g.setColor(Color.RED);
g.fillRect(r1.x, r1.y, r1.width, r1.height);
g.setColor(Color.YELLOW);
g.fillRect(r2.x, r2.y, r2.width, r2.height);
g.setColor(Color.YELLOW);
g.fillRect(r3.x, r3.y, r3.width, r3.height);
g.setColor(Color.BLUE);
g.fillRect(r5.x, r5.y, r5.width, r5.height);
g.setColor(Color.YELLOW);
g.fillRect(r4.x, r4.y, r4.width, r4.height);
if (r5.intersects(r1))
{
velxb = -velxb;
velyb = -velyb;
}
if (r5.intersects(r2))
{
g.drawString("Hello", 10, 10);
}
if (r5.intersects(r3))
{
g.drawString("Hello", 10, 10);
}
if (r5.intersects(r4))
{
g.drawString("Hello", 10, 10);
}
As you can see I have made a couple of rectangle and have done the collision detection. However, when r5
intersects with r2
or r3
or r4
, I want it to disappear.
Upvotes: 1
Views: 385
Reputation: 347194
Painting is a destructive process. Each time your paintXxx
method is called, you are expected to rebuild the out put, that is, clear the existing Graphics
context and repaint everything you want.
You engine should make all the decisions about what effects should occur against the model and the UI should render the model...doing collision detection in your paint process really isn't recommended
Upvotes: 2