P1. Mouse Events

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


Answer:


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.


Answer:


3. Write a test applet, called EnterExitApplet that tests your listener.

What is the code of your test applet?

Answer:


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


Answer:


5. Let's make the applet more interesting. Rather than printing a message, we will change the background color of the applet. 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 EnterExitApplet constructor.

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

What is the code of your test applet?


Answer:


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.


Answer:


P2. Painting

7. Write an applet whose paint 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.

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

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

What is the code for your applet?


Answer:


8. Now we'll enhance the FourLineApplet so that the point p can be changed by clicking with the mouse.

Make an instance variable of type Point2D.double. Change the paint program 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.

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?


Answer:


9. 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?


Answer:


P3. Button Listeners

10. In this program, you will write an applet 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.


Answer:


11. Now write an applet ColorApplet 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?


Answer:


12. 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 now?


Answer:


13. 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 now?


Answer:


14. It is silly to write three separate but extremely similar classes to change the background to red, yellow, and blue.

Factor out common code as follows: Provide a method

public JButton makeButton(String label, final Color c)

that constructs a button with the given label and attaches an action listener that changes the background color to the given color.

Then remove the button constructors and listeners from your applet constructor and replace it with three calls to the makeButton method.

What is the code of your applet now?


Answer:


15. In the makeButton method, the Color parameter was declared final. Why?

Hint: If you don't know the answer, remove the final keyword, recompile, and study the error message.

Answer:

P4. Frames and Text Components

16. 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);

Write a graphical application that shows a frame whose content pane is this scrolling text area.

Call your program Lab4.

What is the code of your program?


Answer:


17. Now modify your program so that the text area contains the string "Hello, World!"

What modification did you make to your program?


Answer:



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

1
5
4
4
3
1
4
2
1
6

What is the code of your program now?


Answer:


19. Now we'll modify the program 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 construct a button, panel and control frame. Place the button into the panel, set the panel as the content pane of the frame, and show the frame.

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

What is the code of your program now?


Answer:


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

What modification did you make to your program?


Answer:


21. 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?


Answer: