I'm currently developing a simulation involving several thousand 1x1 pixel 2D Rectangles moving around a JPanel
. The rectangles move and can collide and join together.
I have created an Event Dispatch Thread
which in turn creates my GUI. I am then creating an instance of the simulation and using a game-loop to control the system with move()
, detectCollision()
and repaint()
methods, with all of the rectangles stored in a global ArrayList
. move()
shifts each rectangle by 1 pixel while detectCollision()
checks to see if two rectangles are next to each other and joins them together if applicable.
The system currently works, however it runs incredibly slowly. Placing a timer around each method showed that my detectCollision()
method can take up to 1000ms to complete. My question is, can I use a worker thread inside the detectCollision()
method to improve the efficiency of the program?