15-110 Sections M-Q / Fall 2009 / Quiz 3
40 Minutes
 

·         You may use conditionals, but you may not use loops or String methods except length() and charAt().

·         On quizzes, you are not graded for style, just correctness.

·         For each problem, you only have to provide the necessary instance variables, event handlers, paint methods, start methods,  and any helper methods you may need.

·         You do not have to provide import statements, class declarations, main methods, or anything else.  You should assume each class extends JComponentWithEvents.

·         Do not worry about windows resizing.

 

1.      Write an animation that is initially blank, but changes according to where the user clicks the mouse.

·        If the screen is blank, a red circle of radius 50 is centered on the click.

·        Otherwise…

o       If the click is inside the circle, it changes color (from red to blue, or blue to red).

o       If the click is outside the circle, it disappears.
 

2.      Write an animation that starts with a black circle of radius 50 in the top-left corner.  Then, the circle takes 5 steps to the right (one step per timer event), then 5 steps down, and repeats this pattern until the entire circle is not visible, at which point it starts over from the beginning.  The circle should move 10 pixels at each step.

 

3.      Write an animation that draws a red square (with sides of length 25) horizontally centered at the top, and a blue square (with sides of length 50) vertically centered on the left, then follows these rules:

·        The red square moves down 10 pixels in response to the down arrow.

·        The blue square moves right 20 pixels in response to the right arrow.

·        Both squares always remain entirely within the visible region, so any arrow press that would move a square off the screen is ignored (except that the program beeps in that case).

·        If the user presses ‘r’, the squares reset to their original positions.

·        If the squares overlap, the red square is painted on top of the blue square, and the background (which is normally yellow) is green.