Lab 11 In-Class: Graphical User Interfaces

Vote Counter - Using Buttons

The files VoteCounter.java and VoteCounterPanel.java contain a slightly modified version of the PushCounter program in Listings 4.10 & 4.11 of the text (pages 186-188). VoteCounterPanel.java is the same as in pre-lab. Recall that the program counts the number of times the button is pushed; it assumes ("pretends") each push is a vote for Blue.

  1. Save the files to your directory, then compile the program and run it to see how it works. Resize the window to see the effect on the components.

  2. In your xterm window use the ls command to see the files you have. In addition to the java source code files you should see 3 bytecode files: VoteCounter.class, VoteCounterPanel.class, and VoteCounterPanel$VoteButtonListener.class. The latter is the bytecode for the VoteButtonListener class that is nested inside the VoteCounterPanel class.

  3. Modify the program so that there are two candidates to vote for -- Blue and Red. To do this you need to do the following (most of this you did on paper in pre-lab):
    1. Add instance variables for Red -- a vote counter, a button, and a label. Initialize these variables in the constructor for the panel.
    2. Add the button and label for Red to the panel.
    3. Add the VoteButtonListener object voteListener to the button using the addActionListener method.
    4. Modify the actionPerformed method in the VoteButtonListener class so it checks to see which button generated the event (that is, which button was pushed) and then updates the appropriate counter and displays the results. To do this you need to use the getSource method for event objects. The event object is passed as a parameter to the actionPerformed method (the formal parameter name of the ActionEvent object is event). The getSource method returns a reference to the component that generated the event so you can compare it to your components as in the example on page 260.

  4. Compile and run the program.

  5. Adding Some Color Modify the panel so that its color is blue if the Blue button is ahead in the number of "votes" and red if Red is ahead and magenta if it is a tie. Do the following:
    1. In actionPerformed, after the count has been updated and displayed, add a statement to set the background color of the panel based on who is ahead. (All Java components have a method void setBackground (Color color) that changes the background color -- see the JComponent class on pages 825 and 826).
    2. Also, change the color of the labels so they clearly show up -- use different colors to go with each background (you can choose the colors). Components have a setForeground method that is analogous to the setBackground method. The setForeground method changes the color of the text in labels and in buttons.
    3. Compile and test your program. Be sure your colors look good and that the labels are visible. If you wish you can also change the foreground and/or background colors of the buttons.

Using TextFields - Computing Body Mass Index

The files Fahrenheit.java and FahrenheitPanel.java contain the example program in Listings 4.12 & 4.13 of the text (pages 190 - 192). The program converts temperatures in Fahrenheit to the Celsius equivalent. The user enters a temperature in the text field and when ENTER is pressed the Celsius equivalent is computed and displayed. Pressing ENTER on a text field generates an action event so the program must implement the ActionListener interface. Do the following:

In this exercise you will write a similar program to compute a person's Body Mass Index. Body Mass Index (BMI) is a measure of weight that takes height into account. Generally, a BMI above 25 is considered high, that is, likely to indicate that an individual is overweight. BMI is calculated as follows for both men and women:

           (703 * weight in pounds) / (height in inches)2

  1. File BMI.java contains the main program that creates the frame for the GUI panel and adds the panel to the content pane of the frame. The file BMIPanel.java contains a skeleton for the GUI to calculate BMI. Since there are two input values needed (the height and weight) this program will not respond to the user pressing ENTER on a text field. Instead, we will put a button on the panel for the user to press to trigger the calculation. So, the user enters his or her height and weight and presses a "Calculate BMI" button; the program then displays the user's BMI. Much of the framework has been done for you, but you will need to fill in code. Do the following (follow the instructions and the comments in the program):

    In the constructor for the panel you need to do the following (put your code for each immediately after the corresponding comment in the code):

    In the actionPerformed method of BMIListener:

    Compile the BMI program and run it.

  2. Add code to the BMIPanel to let the user have some idea of what their BMI means. You need to either modify the JLabel that displays the BMI or add a new JLabel. Your program should include a message based on the following: a BMI less than 19 indicates the person is underweight, a BMI between 19 and 25 (inclusive) means a healthy weight, a BMI between 26 and 30 (inclusive) indicates the person is overweight, and a BMI over 30 indicates the person is very overweight. NOTE: Use an if to choose the appropriate message.

Using Buttons to Move a Snowman

Buttons can be used for all sorts of things including modifying a graphics scene. In this exercise you will use buttons to move a snowman around on a graphics panel. The file Snowman.java contains the class defining a graphical snowman (from an earlier lab), the file MoveSnowman.java contains the main program that creates the frame for the GUI and the file SnowmanPanel.java contains the class that sets up the GUI. Save these files to your lab11 directory then do the following:
  1. Note that the SnowmanPanel class has some constants and 5 instance variables - 4 buttons and a Snowman object. Currently the constructor only does a few things: it instantiates the Snowman object and the buttons, creates a separate panel for the buttons to go on, and it sets the size and color of the main panel. Also note that a panel uses a method named paintComponent rather than paint for drawing. Compile and run the program - all you will see is the Snowman.

  2. Complete the GUI by adding the following to the SnowmanPanel constructor (as indicated in the comments):
    1. Declare and instantiate a MoveListener and then add it to listen for events on each button.
    2. Add the buttons to the button panel.
    3. Add the button panel to the main panel.
    4. Compile and run the program to see how things look (your buttons won't do anything yet).

  3. Now we need to make the Snowman move when the buttons are pressed. That requires doing the following:
    1. Add a move method to the Snowman class. The method should have two parameters of type int. The first should represent the amount to add to the middle instance variable (so this will move the Snowman to the right if the actual parameter is positive and move it left if the number is negative); the second should represent the amount to add to the top instance variable (a positive value will move the Snowman down; a negative value will move the Snowman up). The method should return nothing.
    2. Add an if statement to the actionPerformed method in the MoveListener class that checks to see which button was clicked and moves the Snowman 20 pixels in the appropriate direction. Note that the reference to the component that generated the event is in the variable source. To "move" the Snowman you need to invoke your move method with the correct parameters.
    3. Compile and run the program. Make sure all buttons are moving the Snowman in the correct direction.

A GUI-based ATM Machine

In lab 9 we wrote a program that simulated a simple ATM machine. That program had a command-line interface - the user typed reponses in at a command line prompt. In this exercise you will create a simple GUI-based ATM machine. You will need the following files: Account.java, the bank account class; ATM.java, the main program for the GUI; ATMPanel.java, the GUI panel. Open ATMPanel.java in emacs and do the following:
  1. Create the components and listeners as instructed by the comments in the program. When you add the components to the panel add them in the following order: the welcome label, the label telling the user to enter the amount, the text field for the amount, the label instructing the user to click the appropriate button, the buttons, and the label giving the balance. (Note: Don't do actionPerformed yet - we'll get the GUI looking ok first.)

  2. Compile and run the program to see what it looks like. Not too pretty! This is what the default flow layout looks like.

  3. There are many ways to make things look better. The one we will use is nested panels (see Section 3.10 in the text, pages 145-147). We'll divide the panel into 4 subpanels (and specify the size of each), then add those to the main panel (which has a size specified). Do this as follows:
    1. Declare and instantiate a JPanel named welcomePanel. Set its size (the width should be the constant WIDTH - you choose an appropriate height). Add the welcome label to the panel.
    2. Declare and instantiate a JPanel named amtPanel. Set its size and add both the label for the text field and the text field to it.
    3. Similarly declare and instantiate a JPanel, appropriately sized, containing the two buttons and the label instructing the user to click.
    4. Finally declare and instantiate a JPanel, appropriately sized, containing the balance label.
    5. Instead of adding the individual components to the main panel add the four subpanels.
    6. Compile and run the program to see how it looks. Make necessary modifications, including setting colors for the panels, to make things look good.

  4. Implement the actionPerformed method in the ChoiceListener class. The method should get the amount from the text field and convert it to a double using the parseDouble method in the Double class. Then the method needs to see which button was clicked and, using methods from the Account class, perform the appropriate action. Finally use the setText method to update the balance label (remember to use the formatter to format the new balance).

HAND IN

Hand in printouts for VoteCounterPanel.java, BMIPanel.java, SnowmanPanel.java, and ATMPanel.java. Tar your directory and email the tar file to your instructor with a subject of cpsc120 lab11