CS 3230 - Lab 7

Events Lab

In this week's exciting episode, you'll handle a plethora of different events.

I'm not babying you guys with this lab as much as I did with the previous one. I expect to see a little head scratching this time.

Specifications

Screenshot

Here's what the finished product will look like:

You will need to make two classes, Lab7, derived from JFrame and DrawingPanel derived from JPanel. Put both classes in a file called Lab7.java.

Lab7 class

The Lab7 class will extend JFrame and implement the ActionListener interface.

Please see the Lab6 writeup for details on what you should do in the main method to instantiate the frame, and what you should do in the constructor to arrange the components in the frame.

Handling the button click events

Clicking on the "Clear" button should clear the drawing panel (see below). Clicking on the "Quit" button should close the app. (Hint: call System.exit(0))

To make the buttons work, do this:

  1. After you create new buttons (JButtons), call setActionCommand() with a unique command string. (Hint: just use the same string you used as the text for the button.)
  2. Next, call addActionListener(this) to tell the button that the frame will listen to its events.
  3. Last, override the actionPerformed() method that you inherit from the ActionListener interface. Get the action command from the ActionEvent and use an if / else-if / else construct to test which button was clicked.

You might want to look at the UsesActionCommands.java example.

DrawingPanel class

The DrawingPanel class will extend JPanel and implement the MouseListener, MouseMotionListener, and KeyListener interfaces.

Variables

You will need to declare two variables:

Constructor

paintComponent() method

Note that you are actually handling paint events by overriding the paintComponent() method.

Overriding MouseListener methods

You could get by with just the above overrides, but you may want to override the following methods to make your app work a little more effectively.

Even if you're not interested in the other methods in the MouseListener interface (such as mouseEntered()), you still need to override the methods with empty bodies or the compiler will yell at you.

Overriding MouseMotionListener methods

As before, all methods inherited from the interface must be overridden, even if you don't need to handle the event; just give them empty bodies.

Overriding KeyListener methods

Note: If you forget to call setFocusable(true) in the Panel's constructor, it will not listen to key events. (Only the component with the focus will receive key events.)

All other methods from the interface must be overridden with empty bodies. You will probably want to look at Sketch.java for an example of how to handle key events.

Convenience methods

You can get by without these, but you will probably find them useful and they will help eliminate duplication of code.

Questions and Answers

What packages do I need to import?

These:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.ArrayList;

Why do I have to hit TAB before ESC to clear the DrawingPanel?

The alert student will note that after you've clicked on the 'Clear' button, then drawn on the panel, then tried to clear it again by hitting ESC, it will not clear. This is because the 'Clear' button has the focus and is the only control listening for kestrokes.

To cure this problem, put the line drawingPanel.grabFocus() after you call drawingPanel.clear() in the event handler for your clear button.

How can I use this stuff in my Application Project?

So glad you asked. You can continue making headway today on your application project by handling various events generated by the mouse, the keyboard, and some of the controls in your frame.

Those of you that are making a drawing program for your application project should find this lab useful.