COMP 303 - Lecture Notes for Week 4 - Interfaces and Polymorphism

Chapter Topics

Displaying an Image

.

Displaying an Image

.

Displaying an Image

.

The Icon Interface Type

public interface Icon
{
   int getIconWidth();
   int getIconHeight();
   void paintIcon(Component c, Graphics g, int x, int y)
}

Interface Types

The Icon Interface Type and Implementing Classes

.

Polymorphism

Polymorphism

A Variable of Interface Type

.

Polymorphism

Benefits of Polymorphism

Drawing Shapes

Drawing Rectangles and Ellipses

Drawing Ellipses

.

Drawing Line Segments

Relationship Between Shape Classes

.

Drawing Text

.

Filling Shapes

.

The Comparable Interface Type

The Comparable Interface Type

The Comparator interface type

The Comparator interface type

Anonymous Classes

Anonymous Classes

Anonymous Classes

Frames

Adding Components

.

User Interface Actions

User Interface Actions

helloButton.addActionListener( 
  new ActionListener()
  { 
    public void actionPerformed(ActionEvent event)
      { 
        textField.setText("Hello, World");
      }
  } );

Accessing Variables from Enclosing Scope

User Interface Actions

Constructing Related Actions

public static ActionListener createGreetingButtonListener(
   final String message)
{
   return new
      ActionListener()
      {
         public void actionPerformed(ActionEvent event)
         {
            textField.setText(message);
         }
      };
}

Timers

.

Defining a New Interface Type

.

CRC Card for the MoveableShape Interface Type

.

Defining a New Interface Type

Implementing the Animation

Implementing the Animation

Implementing the Animation

.