GUI: Event handler exercises 1314

November 6th, 2013

Exercise 8: Write a program that animates two cars moving across a frame in opposite direc- tions (but at different heights so that they don’t collide.)

Exercise 9: Change the RectangleComponent for the mouse listener program so that a new rectangle is added to the component whenever the mouse is clicked. Hint: Keep an ArrayList and draw all rectangles in the paintComponent method.

Exercise 10: Write a program that prompts the user to enter the x- and y-positions of the center and a radius, using JOptionPane dialogs.When the user clicks a “Draw” button, draw a circle with that center and radius in a component.

Exercise 10: Write a program that allows the user to specify a circle by typing the radius in a JOptionPane and then clicking on the center. Note that you don’t need a “Draw” button.

Exercise 11: Write a program that allows the user to specify a circle with two mouse presses, the first one on the center and the second on a point on the periphery. Hint: In the mouse press handler, you must keep track of whether you already received the center point in a previous mouse press.