CS 1713 Week 5:
More Object Oriented Design

Objectives:

Activities:

Reading: Chapter 9.1 - 9.5, Chapter 4.6-4.9


The Object class

What does the following do?
   int n = 431;
   System.out.println("This is a number: "+n);
Since a String is to the left of the plus, the plus is a string concatenation operator.
Java will convert the value of n to a String to produce "431" and then concatenate (join) the two strings.
The output will be
This is a number: 431

Suppose that we start with the DrawableRectangle class from gcs02.
What does the following do?

DrawableRectangle rect = new DrawableRectangle(10,20,30,40,Color.red);
System.out.println("This is a drawable rectangle: "+rect);
Java will convert the value of rect to a String.
How will it do this?
It will use the toString method of the DrawableRectangle class.
Where does this come from?
How does Java know that DrawableRectangle has a toString method?

Every object that does not extend something else extends Object.

The Object class has a small number of methods, including toString.
The default toString is not very useful for most objects, but we can replace it by overriding the default toString method of the Object class.

Example: Write a toString method for the DrawableRectangle class that describes the entire state of the object and is suitable for printing with System.out.println.

What would happen if we tried to use this in paintComponent to display the state of a DrawableRectangle?

Note the DrawableRectangle extends Object only indirectly.
DrawableRectangle extends Rectangle extends Object.


Models of Program Execution

Here are some models of how programs execute:
  1. Start the program, it produces output and terminates.
    Example: Write a program to display the square of the integers from 1 to 10.
  2. Start a program, it finds its own input (from the command line or a file), it produces output and terminates.
  3. Start a program, it prompts for input (maybe more than once), it produces output and terminates.
    Example: Keep prompting the user to enter an integer, n, until it is greater than 0,
    and then output the sum of the squares of the first n positive integers.
  4. Start a program, when an event occurs, it performs an action.
    It does not terminate until it receives a termination event.
Most of the programs you wrote in CS 1063 were of the first or third type.
Later, we will learn how to do the second type.
The graphics case studies we have done so far have been simplified versions of the 4th type.
The only event was the termination event.


Graphics Case Study 3: Layouts and Buttons

Create a new project, gcs03 in which all classes are in the layouts package.
Make a class called SimpleLayoutProgram modeled upon SimpleFrameProgram.
Make a class called ButtonPanel which will extend JPanel and make an instance to add to the frame and applet.

You can find a template for this case study here.

Have the constructor of ButtonPanel add several buttons.
You can make a button using:
new JButton("Push Me");

You can also add some labels using:
new JLabel("This program was written by S. Robbins");

Experiment with resizing the frame or applet to see how the added items move around.

What happens when you push a button?

To make a button do something, you need to have something listen for the pushes of the button.
In Java, that "thing" is an object of type ActionListener.

To react to a button push:

  1. Make an object that implements ActionListener.
  2. The ActionListener class has one method:
    public void ActionPerformed(ActionEvent e);
  3. Add the listener to the button by using the JButton method:
    public void addActionListener(ActionListener listener);
  4. The simplest way to do this is to have the panel that contains the button be the listener.
    We will look at other ways later.
If you use the same ActionListener for more than one button, you can use the getSource() method of the ActionEvent to tell which button caused the event to occur.

Modify the program so that it ButtonPanel has an array of DrawableShapes and each time a button is pushed it creates a new DrawableRectangle or DrawableCircle one at a different position.

Have paintComponent start with:

   Diminsion size = getSize();
   g.setColor(Color.lightGray);
   g.fillRect(0,0,size.width,size.height);

Start with an array of 5 elements.


How to make an array larger

Once an array object is created, its length is fixed.
You cannot increase the number of elements it can have. You can run the Java Simulator on this example here.

You can run the Java Simulator on an example that expands the array by on each time here.