CS 187/487
Homework # 9

Due November 30, 2001

Remember to use header comments and comments throughout your code as required in previous assignments.

This lab is designed to give you practice with radio buttons, Button groups, and drawing in JPanels

Submit your program(s). using the web page submission system.  If you use a separate external class, such as DrawingPanel.java, you need to submit the .java file for it, also.

.



1.  Use the your program from lab 8 as a starting point.  Change the JButton to say "Clear" and each time the user clicks the button, erase the contents of the drawing panel.

2.  Add a MouseListener to the drawing panel.  Define the mousePressed and mouseReleased methods as follows:

     mousePressed:  store the x and y coordinates of the mouse event.  These coordinates are (x1, y1).

    mouseReleased:  store the x and y coordinates of the mouse event.  These coordinates are (x2 , y2).  Then use (x1,y1) and (x2, y2) to draw an object of the selected color, shape, and fill.  As soon as the mouse is released your program should call the draw method to draw the selected shape.

3.  Modify the parameters to your drawing panel's panel draw method so that you can pass the x1, y1, x2, and y2 values to it.  Here's how to draw each of the shapes:

Circle:  Use (x1, y1) as the center and (x2, y2) as a point on the circle.  Then the radius is the distance between the two points.  Here's the code:

  r = (int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

The upper left corner of the bounding rectangle is then (x1 - r, y1 - r) and the width and height are both 2 * r.

Square:  Use the minimum of x1 and x2 with the minimum of y1 and y2 as the upper left corner.  Use the absolute value of (x2 - x1) as the width and the height of the square.

Triangle:  Use (x1,y1) as one point, (x2, y2) as the second, and (x1, y2) as the third point of the triangle.



The result should be a simple paint-type program that lets the user determine where circles, squares or triangles are to be drawn by dragging the mouse over the custom panel.
 
 The user can clear the shapes that have been drawn by clicking the "Clear" button.