Computer Science 2311
Spring 2005
Laboratory Assignment 9

Handed out: 3/30/05

Read this entire assignment before beginning to work on it.


The assignment

This assignment has you create a simple graphics application. Chapter 7, page 553, shows a graphics application that creates a screen with two buttons that the user can push. You will create a screen with one button.

Your application should paint a disk (a filled circle) with diameter 20 pixels of a random color, and at a random position on the canvas. The random position should be chosen within limits so that the disk will be entirely on the canvas, and will not cover the button. There should be a few color options, such as red, blue, yellow and green. You can choose any set of three or more colors, as long as they are easy to distinguish from one another.

Each time the button is pushed, a color and position should be chosen at random. The old disk should be erased, and the new disk shown.


Class heading

Create a class that extends JFrame and that extends ActionListener, so that a frame object can do its own event (button push) handling. Use the following heading, but select whatever name for your class you like.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Assignment9 extends JFrame implements ActionListener
{
  ...
}


Variables

You will create an object that is a special kind of JFrame object. So your main program might look like this.

    public static void main(String[] args) 
    {
	JFrame frame = new Assignment9();
	frame.setVisible(true);
    }
Your frame object will want to remember some information, such as the current disk color and the current disk position (x-coordinate and y-coordinate). So create variables to hold that information. The x- and y-coordinates are integers. The color has type Color.

Do not declare your variables to be static. If you have more than one of these frames, then each should remember its own information.


Buttons and listeners

Create a button as shown in the book. Here is an example.

  JButton button = new JButton("move");
  button.setBackground(Color.GREEN);
  button.setSize(50, 30);
Now you will want to make the button do something. If you write
  button.addActionListener(this);
then you are saying that, when this button is pushed, the program should perform the method called actionPerformed in the current object. Just write that method, with the following heading.
  public void actionPerformed(ActionEvent e)
You will not need to look at the ActionEvent value e, since you only have one event. At the end of actionPerformed, do
  repaint();
to ensure that the window is repainted. This will call the paint method. Do not call the paint method directly.


Adding the button to your window

Create a panel to add to the window, and put the button in the panel. Here are steps to do that.

  JPanel buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout());
  buttonPanel.setBackground(Color.WHITE);
  buttonPanel.setSize(BUTTON_PANEL_WIDTH, BUTTON_PANEL_HEIGHT);
Now put the button on the panel.
  buttonPanel.add(button);
Now add the panel to the window by adding it to the content pane of the window.
  Container contentPane = getContentPane();
  contentPane.setBackground(Color.WHITE);
  contentPane.setLayout(new BorderLayout());
  contentPane.add(buttonPanel, BorderLayout.SOUTH);


Drawing a disk

Write a paint method with the following heading in your class.

  public void paint(Graphics canvas)
Your class is a special kind of JFrame, and before you paint on the canvas, you should ask the JFrame to paint the background. Do that using line
  super.paint(canvas);
at the beginning of the paint method. (Super is a way of refering to the thing that you are a special case of.) Now use the fillOval method of the Graphics class to draw the disk. Use the position and color variables in your class to decide where to draw the disk, and what color it should have.


Erasing the old disk

If you just keep drawing disks, more and more of your canvas will have disks painted on it. Before drawing a new disk, erase the old one. You can erase it by painting it the same color as the background.

The paint method will need to know not only where the new disk is to be painted, but where the old disk is, so that it can erase the old disk. Just keep variables that tell you where the disk used to be.


Getting the program to terminate when you hit the x button.

JFrame operation

   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
will tell the JFrame to cause the program to stop when you push the x button. You will want this; otherwise, the program will keep going.

The book shows how to do this using class WindowDestroyer. That is a new class in Java 1.5, and is not available in Java 1.4, which we are using. Use setDefaultCloseOperation instead.


What to turn in

When your program is working, print it. Then demonstrate it in the lab to me or to one of the graduate assistants. If you get a graduate assistant to check it, also get him or her to write a note on the printout that it was checked, and to initial it.