Write a program called MovingShapes.java with a 500 x 500 JFrame
labelled “Homework ”

(b) Write two inner classes that extend JPanel. The first should be called
FirstDrawPanel and should draw a blue oval with the coordinates
0,0,50,50, the second should be called SecondDrawPanel and should
draw an orange rectangle with the coordinates 0,0,50,50

(c) Using BorderLayout manager add a FirstDrawPanel object to the
CENTER region

(d) Using the BorderLayout manager add a SecondDrawPanel object to
the NORTH region

(e) Make sure that the component in the NORTH region gets its preferred
height of 50

(f) Make the rectangle move in a straight line across the frame while at the
same time the oval moves down the frame diagonally. Both shapes
should appear to move at the same time, and should continue moving
until they exit the frame. You should make sure that they move slowly
enough to be seen by the user.

(g) Resave your program as MovingShapesButton.java and extend the
animation so that it is continuous – ie so that the shapes keep moving
while the program is running. You may choose to make the animation
continuous in any way you choose. You could, for example, start the
animation again from the beginning once the shapes have exited the
frame, or you may have the shapes retrace their movements or even
have them start moving randomly.

(h) Add a JButton to the frame, label the button “Click me to stop the

(i) When the button is clicked the animation should stop.

(j) Explain why it is that despite the oval and the rectangle having the
same coordinates, the circle is displayed below the rectangle.

Want an answer?