CPSC 170 Lab 4: Polygons and GUIs

As usual, create a lab4 subdirectory for today's lab, open this document in Netscape, and start emacs.

  1. A polygon is a multisided closed figure; a polyline is a line with an arbitrary number of segments. Both polygons and polylines are defined by a set of points, and Java provides graphics methods for both that are based on arrays. Carefully read pages 359-362 in the book about polygons and polylines, and study the Rocket example.

    File DrawPerson.java contains an applet that creates a Person object and draws it. File Person.java contains the Person class, where a Person currently consists only of a blue shirt. Copy these files and the associated html file to your directory, compile DrawPerson.java, and run it using the appletviewer to see what it does. Now modify Person.java so that it draws a whole person as follows:

    1. Add pants to go with the shirt (they should be a different color). You will need to declare pantsX and pantsY arrays like the shirtX and shirtY arrays and figure out what should go in them. (Note that these are instance variables so they are visible to all of the methods. This may not seem necessary now but will be useful later.) Create a drawPants method and make the draw method call it as well as the existing drawShirt method.

    2. Add a head. This can just be a circle (or oval), so you won't need to use the Polygon methods. Declare variables headX and headY to hold the position of the head (its upper lefthand corner), and use them in the drawHead method.

    3. Add hair to the head. This is probably best done with a polygon, so again you'll need two arrays to hold the points.

    4. Add a zigzag across the front of the shirt. (You can draw this in the drawShirt method if you want.) Use a polyline.

    5. Write a method move(int x, int y) that moves the person by the given number of pixels in the x and y direction. To keep it clean, write separate methods moveShirt, movePants, etc, that go through the appropriate arrays (or in the case of the head, the x and y coords) and increment all of the coordinates by the x or y value as appropriate.

    6. Now modify your applet (in the DrawPerson class) so that it draws the person three times, moving him/her in both the x and y directions each time (you decide how far).

    Print DrawPerson.java and Person.java to turn in.

  2. Last semester you worked with GUIs in lab 12, where you wrote event-driven programs that did things when the user pushed a button (remember Vote For Joe?). Recall that these programs used ActionListeners to carry out certain actions when a button was pressed. In today's exercise you will see two new GUI components (checkboxes and radio buttons) along with their listeners (ItemListeners and ActionListeners respectively).

    The files StyleOptions.java and StyleGUI.java are adapted from Listings 6.18 and 6.19 of the text. (A variable fontSize is used rather than the constant FONT_SIZE and the variable style is an instance variable rather than local to the itemStateChanged method). Save these files to your directory and compile and run StyleOptions.java to see how it works. This is the driver; StyleGUI.java contains the code for the GUI.

    Now you will add a set of 3 radio buttons to let the user choose among three font sizes. The method of adding the radio buttons will be very similar to that in the QuoteGUI class (Listing 6.21 of the text), so study this example before you continue.

    Do the following to add the radio buttons to the GUI:

    1. Declare three objects small, medium, and large of type JRadioButton.

    2. Instantiate the button objects labeling them "Small Font," "Medium Font," "Large Font." Initialize the large font button to true. Set the background color of the buttons to cyan.

    3. Instantiate a button group object and add the buttons to it.

    4. Radio buttons produce action events so you need to add an inner class (name it SizeListener) to implement ActionListener and listen for radio button clicks. The code for actionPerformed can be similar to that in the QuoteListener in Listing 6.21. (Or if you prefer, you can use the isSelected method to see which button was selected instead of getting and checking the source.) You need to set the fontSize variable (use 12 for small, 24 for medium, and 36 for large) in the if statement, then call the setFont method to set the font for the saying object.

    5. In StyleGUI() instantiate a SizeListener and add it to each button. Also add each button to the panel.

    6. Compile and run the program. Note that as the font size changes the checkboxes and buttons re-arrange themselves in the panel. You will learn how to control layout later in the course.