Making illustrations interactive

An example
Using buttons
Mouse Events

Based on notes Making illustrations interactive

An example

Using Buttons

We need two things:

  • something to initiate the action (the button in the example above),

    Java provides a class JButton that, when pressed, will can take some specified action.

    To instantiate a JButton, we can simply specify the piece of text we want displayed on the button:

    	JButton button = new JButton("Move");

  • and a piece of code, a method, to respond to the action. This method will be contained in an implementation of an interface.


Java methods will only accept other objects as parameters. Sometimes, however, we would like to be able to pass a method. For instance, we would like to pass to our JButton a method for it to execute when it is pressed.

Instead, we need to pass it an object with a method inside it that the JButton will recognize to run when pressed.

An interface accomplishes this: it gives a way to declare that an object has a method with a particular signature.


java.awt.event provides an interface called ActionListener. We say that a class implements ActionListener if it has a method with the signature:

public void actionPerformed(ActionEvent event);

Here is an example

    public class TakeAction implements ActionListener {
	public void actionPerformed(ActionEvent event) {
	    System.out.println("Action taken");

An instance of TakeAction implements ActionListener since it will have the method with the proper signature.


The code we wish executed when the JButton is pressed is placed in the method actionPerformed(ActionEvent) required of an implementation of ActionListener.

We then register the implementation with the JButton. For example, if listener is an object that implements ActionListener, we can say


When pressed, the JButton will execute the actionPerformed method of every ActionListener registered with it.

An example


The method call repaint() is important: it causes the paintComponent method to be run again. Consequently, if the figure has been updated, a call to repaint() displays the new figure.


Up to this point, we've constructed applications with a single JPanel. Now we want to add a JButton into the program as well.

How the button is added and where is described using a LayoutManager. By default, the content pane of a JFrame or JApplet is set to an instance of BorderLayout You may think of this as dividing the pane up into five regions: one in the center and four around the sides.

In the example above, we place our JPanel containing the rectangle in the center:

	getContentPane().add(panel, BorderLayout.CENTER);

To have the button appear below, we place it in the southern location. First, however, we put the button into a JPanel and then put the JPanel into the content pane.

	JPanel buttonPanel = new JPanel();
	getContentPane().add(panel, BorderLayout.CENTER);	

Without using buttonPanel, the button would be as wide as the content pane.


When pressed, the JButton creates an ActionEvent object. This object contains relevant information about its creation: for instance, it contains a reference to the object that created it (our JButton in the example above), the piece of text on the button and whether the Ctrl key was held down at the time the button was pressed.

Adding a reset button

It is usually a good idea to have some way for the user to restore the program to its original configuration. We will do this by adding another button.

Mouse Events

Rather than responding to a button being pressed, we sometimes want to respond to actions initiated by the mouse, say, by clicking and dragging.

Mouse events

JButtons generate ActionEvents. Components, such as JPanels, generate MouseEvents. The component will pass such an event on to any registered mouse listener.

In the same way as before, we respond to events by implementing MouseListener and/or MouseMotionListener and registering them with the component we are interested in.

  • MouseListener

        public void mouseClicked(MouseEvent);
        public void mouseEntered(MouseEvent);
        public void mouseExited(MouseEvent);
        public void mousePressed(MouseEvent);
        public void mouseReleased(MouseEvent);

  • MouseMotionListener

        public void mouseDragged(MouseEvent);
        public void mouseMoved(MouseEvent);

Moving points


To make an animation, we need something other than the user to trigger ActionEvents at a prescribed interval. An instance of javax.swing.Timer does this.

To instantiate a Timer, we give it a time interval (expressed in milliseconds) and an ActionListener

    javax.swing.Timer timer = new Timer(100, listener);

Use timer.start() to start the Timer and timer.stop to stop it.

Animation Example