Source: Horstmann Lab 10 (1st Edition): Event Handling

Some code from Chapter 10 (1st Edition) of Big Java by Horstmann:
  1. MouseSpy
  2. MouseSpyApplet and MouseSpyFrame
  3. MouseApplet.java + MouseApplet.html; and RectanglePanel.java + RectanglePanelDriver.java
  4. ButtonApplet.java and ButtonPanel.java + RectanglePanel.java + ButtonPanelDriver.java
  5. PanelDriver.java
  6. ButtonApplet2.java
  7. TextAreaTest.java
  8. hand.gif
  9. world.gif
  10. FrameTest.java
Note: In Edition 1 of Big Java, the first graphics examples were with applets. Edition 2, Chapter 5 shows how to use applets, but most examples and emphasis is on frames. In this lab, you can try out the applets if you like,or where the lab requests an applet you can use a panel and a frame. No test material will ask for an applet.

P1. Mouse Events (Goal: EnterExitPanel.java)

P1.1. A user interface component such as a panel, or an applet or a button can detect five types of mouse events. What are these events?

P1.2. In this exercise, we are only interested in the "entered" and "exited" events. Write a mouse listener class that prints "Entered" and "Exited" to System.out when the mouse has entered or exited the component. Do nothing for the other three mouse event types.Call your class EnterExitListener. Remember that you need to implement the MouseListener interface.

P1.3. Write a test applet or frame, called EnterExitApplet or EnterExitPanel that tests your listener.

P1.4. Try out your applet or frame. What do you need to do to get the messages "Entered" and "Exited" to appear in the console?

P1.5. Let's make the applet or panel more interesting. Rather than printing a message, we will change the background color of the applet or panel. In the mouseEntered method, add the line

setBackground(Color.green);
and in the mouseExited method, add the line
setBackground(Color.red);
Make sure that your EnterExitListener is an inner class of the constructor of EnterExitApplet or EnterExitFrame.

Compile and run your test applet or frame. When the mouse enters the applet or frame, its background color changes to green. When the mouse leaves the applet or frame, the background turns red.

What is the code of your test applet or frame?

P1.6. Why does the EnterExitListener have to be an inner class?

Hint: If you aren't sure, make it into a regular class, compile, and study the error message.


P2. Painting (Goal: FourLinePanel.java)

P2.1. Write an applet whose paint method (or a panel whose paintComponent method) draws four lines:

  1. From the point p to the top left corner
  2. From the point p to the bottom left corner
  3. From the point p to the top right corner
  4. From the point p to the bottom right corner

Here, p is the point with the coordinates (50, 100).

Call your applet FourLineApplet or your panel FourLinePanel,

Hint: Call getWidth() and getHeight() to get the x- and y-coordinates of the corner points.

Try out your applet or panel. Its display should look like this:

What is the code for your applet or panel?


P2.2. Now we'll enhance the panel or applet so that the point p can be changed by clicking with the mouse.

Make an instance variable of type Point2D.double. Change the paint or paintComponent method to join that point with the four corner points. (Maybe you had the foresight to do that in the preceding program? If so, pat yourself on the back.)

Write a MousePressListener class that implements the MouseListener interface. Its mousePressed method sets p to the point with x- and y-values of the mouse event.

Make sure that the mousePressed method calls repaint() after changing p.

Add a MousePressListener as mouse listener to the applet or panel.

Run your program. Now you should be able to press the mouse and have the four lines join on the point that you just clicked.

What is the code for your applet or panel?

Why is it important to call repaint in the mouse press listener? What would happen if you don't call repaint?

Try it out. Comment out the call to repaint, recompile, and try your program again. What happens?


P3. Button Listeners (Goal: ColorPanel.java)

P3.1. In this program, you will write an applet or panel whose background color can be controlled with buttons. We'll supply three buttons, labeled Red, Yellow, and Green, in a control panel.

A control panel is an external frame that holds user interface components.

Write the code needed to construct the three buttons, to construct the frame, to add the buttons to the frame, and to show the frame.

P3.2. Now write an applet or panel ColorApplet or ColorPanel whose constructor displays the control panel.

The buttons won't do anything yet. We'll add listeners in the next problem.

What is the code of your applet or panel?

P3.3. Buttons generate action events. To listen to them, you need to install action listeners. When the red button is clicked, we want to change the color to red. The following class will do that:

class RedButtonListener implements ActionListener
{
   public void actionPerformed(ActionEvent event)
   {
      setBackground(Color.red);
   }
}

Now you need to add an object of this type as a listener to the "Red" button.

Try out your program. The "Red" button should change the color to red. The other two buttons should do nothing.

What is the code of your applet or panel now?

P3.4. Fix your program so that the other two buttons change the background color to yellow and green. Test your program.

What is the code of your applet or panel now?


P4. Frames and Text Components (Goal: DicePanel.java)

P4.1. A graphical application is different from an applet in a number of ways. You don't use the applet viewer, and you don't need an HTML file. Just start the program with the Java interpreter.

Every graphical application needs a top-level frame. Then add a component to the content pane of the frame.

In this program, we want to put a scrolling text area into the frame. The code for a scrolling text area is:

JTextArea textArea = new JTextArea(10, 30);
JScrollPane scrollPane = new JScrollPane(textArea);

You can put the scroll pane directly on the content pane, but here I will assume that you write a class DicePanel which holds the scroll pane. You can then use a generic PanelDriver that displays a frame and puts the DicePanel into the frame's content pane.

What is the code of your program?

P4.2. Now modify DicePanel so that the text area contains the string "Hello, World!"

P4.3. Instead of a greeting, we want to show some data in the text area. Modify the DicePanel so that it shows the outcome of ten rolls of a die, such as

1
5
4
4
3
1
4
2
1
6

P4.4. Now we'll modify DicePanel so that you can generate die simulations by clicking a button.

We'll place the button into an external control frame.

Add the code to DicePanel to construct a button, panel and control frame. Place the button into the panel, set the panel as the content pane of the control frame, and show the frame. All this is done in the constructor of DicePanel.

Run the program. Of course, the button doesn't yet do anything.

P4.5. Now add a button listener. Whenever the button is clicked, add a new random value to the text area.

P4.6. The button listener of the preceding problem added the outcome of a single throw of a die to the text area.

Now we'd like to have multiple throws as well. Add a text field to the control panel that allows the user to set the number of throws. Label it "Throws:".

Modify the button handler so that it reads the current value of the text field, converts it to an integer, and adds the requested number of die throws to the text area.

What is the code of your program now?