collision - How do you create 2 Rectangle objects that will collide and be detected in Java? -


also, possible collide 2 oval objects?

   public void paint(graphics g){             g.setcolor(color.green);<br/>             g.fillrect(k, l, 100, 100);<br/>                 g.setcolor(color.blue);<br/>                 g.fillrect(n, m, 100, 100);<br/>             g.setcolor(color.red);<br/>             for(int i=0; i<2; i++){<br/>                g.filloval((int)x[i],(int)y[i],diameter,diameter);<br/>             }<br/>         } 

it appears using swing graphics. although providing code question rather complex task, try guide in correct direction.

it might best encapsulate rectangles (or ovals) own classes. give objects method drawself(graphics g) , pass in graphics object. also, give classes fields keep track of current positions , sizes.

then, collisions, might try making class along lines of collisiondetector. of shape objects have-a collisiondetector, , everytime shape moves ask shapes check collisiondetectors, in turn check other detectors see if overlap.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -