Event handlingTopGUI Components

GUI Components

As indicated in the textbook, adding graphic user interface (GUI) items to the screen is pretty easy. Here are the steps:

  1. Create the item. E.g.,
       def shapes: Choice = selectBox.options("FramedSquare", "FramedCircle", "FilledSquare")
       def clearButton: Button = button.labeled("Clear screen")
    
  2. Add the items to window. E.g.,
        append(shapes) // add below canvas
        prepend(clearButton) // add above canvas
    

Items that are "append" ed are added as the new last item in the window, while "prepend" ed items are added before all the others.

A simple example of this is the program DrawingProgram. In this program, if you click anywhere in the drawing area, a geometric figure will be drawn in the middle of the screen. The figure to be drawn is dependent on the setting of the figureMenu. The item selected on the menu is obtained by evaluating shapes.selected. The match statement in onMouseClick provides for the construction of the appropriate geometric object corresponding to the kind of item selected in the pop-up menu.

Notice how the empty object allows the program to execute properly if someone tries to interact with the canvas before newShape has been initialized with a proper geometric object. It is initialized with empty, which does not draw anything on the screen, and responds to methods by not doing anything. Essentially those method requests are just ignored. If we had not initialized newShape to empty then the program would crash when it sent a method request to an uninitialized newShape.


Event handlingTopGUI Components