Lab Exercise for Assignment 7

COMP112 2020 Tri 1: Lab Exercise 7

Goals

By the end of the assignment, you will have gained experience in writing programs using with simple event-driven input (mouse, buttons, etc).

Preparation

Download the zip file for lab exercise 7 and extract it to the COMP112-2020T1-Lab7 folder in your home folder. It should contain templates for the exercises program. Read through the exercises and view the demo videos so that you know what you need to do.

Go over the code examples in slides from the lectures on GUI and ArrayLists.

Summary

  • Circle exercise:
    right Complete a program that let the user draw Circles on the graphics pane.
  • Star exercise:
    right Complete a program that let the user draw stars on the graphics pane.

There are two exercises for this Lab, closely related to the MiniPaint assignment.

Circles Exercise

CirclesExercise is a very simple program that lets the user place small and large circles on the graphics pane using the mouse. It needs one field to store the "current size". It has three buttons: Clear, Small, and Large.
  • The Clear button should clear the graphics pane.
  • The Small button should change the "current size" to 10,
  • The Large button should change the current size to 30.
When the user releases the mouse at any point on the graphics pane, the program should draw a blue circle of the current size centered at that point.

Note that the program should not draw anything when the Small or Large buttons are pressed - it should just change the value stored in the field.

You need to
  • declare a field to store the current size. Initialise it with "Small".
  • complete the setupGUI method set the mouse listener, and add the three buttons.
  • define methods which will respond to the Large and Small buttons by setting the current size field to an appropriate value.
  • define a doMouse method which will respond when the user releases the mouse at a point by drawing a circle on the graphics pane of the appropriate size centered at the position the mouse was released.

Star Exercise

StarExercise is a very simple program that lets the user draw "stars" on the graphics pane. Stars consist of a set of lines all drawn from a center point. Using the mouse, the user can select a center point and can then select a series of "tips" - the points of the star. The program needs to remember the position of the current center point and the "state" - whether the user is selecting the center point or the tips.

The program has four buttons: Clear, Set Color, Set Center, and Set Tips.
  • The Clear button should clear the graphics pane.
  • The Set Color button should change the brush color of the UI, using the JColorChooser.showDialog(null, "Choose Color", null) method to ask the user for the new color.
  • The Set Center and Set Tips buttons should change the field storing the current "state" (remembering whether the user is setting the center point or the tips). The buttons should not change the center point or draw anything.

When the user releases the mouse at any point on the graphics pane, the program should either
  • change the fields storing the center point (if the state records that the user is setting the center point), and draw a small circle at the new center point, OR
  • draw a line from the current center point to the mouse event point (if the state records that the user is setting the tips of the star).

You need to
  • declare fields to store the current center point and the "state"
  • complete the setupGUI method to set the mouse listener, and add the four buttons.
  • define methods to respond to the Set Color, Set Center, and Set Tips, buttons.
  • define a doMouse method which will respond when the user clicks the mouse at a point. It must consider the state to determine whether to change the center point or to draw a line