Write a program in java that forms a green square inside a JPanel. Once you press space, the square should move from one side to the other horizontally (for example from left top to the right top). When the square hits the edge of the Panel, it should move the negative direction to reach to the other side. The square must be updated using a Runnable and therefore, make sure that it is utilizing concurrency properly. You know that you need to override the method run, coming from a Runnable to implement basic concurrency. Another trick, when the user presses S key, a new square is created and they move simultaneously (as they are concurrent objects). Make sure they don't stand on top of each other; change their Y coordinates a bit to make the other squares a bit lower in the screen. We should be able to create 5 squares in your environment. Beyond 5, you should prevent creation of additional squares.
Write a program in java that forms a green square inside a JPanel. Once you press space, the square should move from one side to the other horizontally (for example from left top
to the right top).
When the square hits the edge of the Panel, it should move the negative direction to reach to the other side. The square must be updated using a Runnable and therefore,
make sure that it is utilizing concurrency properly. You know that you need to override the method run, coming from a Runnable to implement basic concurrency.
Another trick, when the user presses S key, a new square is created and they move simultaneously (as they are concurrent objects). Make sure they don't stand on top of each
other; change their Y coordinates a bit to make the other squares a bit lower in the screen. We should be able to create 5 squares in your environment. Beyond 5, you should
prevent creation of additional squares.
Step by step
Solved in 3 steps with 1 images