CSCI A202 - Introduction to Programming (II)

Lecture 25: The Abstract Windowing Toolkit (II)

Please read pages 443-455 in your textbook. They should provide you with a complete understanding of the following program which I encourage you to try out during the lab.

import java.awt.*;
import java.awt.event.*;
 
public class AWTExample extends    Frame 
                        implements ActionListener, 
                                   WindowListener {
 
  private Button up, down, left, right; 
 
  AWTExample() {
    up    = new Button("Up"   ); 
    down  = new Button("Down" );
    right = new Button("Right"); 
    left  = new Button("Left" );
 
    FlowLayout layout = new FlowLayout(); 
    setLayout(layout); 
 
    add(up);       up.addActionListener(this); 
    add(down);   down.addActionListener(this); 
    add(right); right.addActionListener(this); 
    add(left);   left.addActionListener(this); 
 
    addWindowListener(this); 
  } 
 
  public void actionPerformed(ActionEvent e) {
    Button button = (Button)e.getSource(); 
    if        (button == up)    {
      System.out.println("Up button pressed."); 
    } else if (button == down)  {
      System.out.println("Down button pressed."); 
    } else if (button == right) {
      System.out.println("Right button pressed."); 
    } else                      {
      System.out.println("Left button pressed."); 
    } 
  } 
 
  public void windowClosing     (WindowEvent e) { 
    System.exit(0); 
  } 
  public void windowActivated   (WindowEvent e) { } 
  public void windowClosed      (WindowEvent e) { } 
  public void windowDeactivated (WindowEvent e) { } 
  public void windowDeiconified (WindowEvent e) { } 
  public void windowIconified   (WindowEvent e) { } 
  public void windowOpened      (WindowEvent e) { } 
 
  public static void main(String[] args) {
    Frame frm = new AWTExample(); 
    frm.setSize(250, 100); 
    frm.setVisible(true); 
  }  
}
This program is only a framework. You should strive to understand it completely. Let's look at a few variations of this program. We are building towards your assignment #12.

For the first variation, the added lines (or characters) are in blue.

The code comes with a question attached: what better names (or labels) can you find for each of the buttons in this variation?

import java.awt.*;
import java.awt.event.*;
 
public class AWTExample2 extends    Frame 
                         implements ActionListener, 
                                    WindowListener {
 
  private Button up, down, left, right; 
  OneWayList myList = new OneWayList(); 
  int counter = 0; 
 
  AWTExample2() {
    up    = new Button("Up"   ); 
    down  = new Button("Down" );
    right = new Button("Right"); 
    left  = new Button("Left" );
 
    FlowLayout layout = new FlowLayout(); 
    setLayout(layout); 
 
    add(up);       up.addActionListener(this); 
    add(down);   down.addActionListener(this); 
    add(right); right.addActionListener(this); 
    add(left);   left.addActionListener(this); 
 
    addWindowListener(this); 
  } 
 
  public void actionPerformed(ActionEvent e) {
    Button button = (Button)e.getSource(); 
    if        (button == up)    {
      System.out.println("Up button pressed."); 
      myList.insert(counter++ + ""); 
      System.out.println(myList); 
    } else if (button == down)  {
      System.out.println("Down button pressed."); 
      if (!myList.atEnd())
        myList.next(); 
      System.out.println(myList); 
    } else if (button == right) {
      System.out.println("Right button pressed."); 
      myList.first(); 
      System.out.println(myList);  
    } else                      {
      System.out.println("Left button pressed."); 
      if (!myList.isEmpty())
        myList.remove(); 
      System.out.println(myList);  
    } 
  } 
 
  public void windowClosing     (WindowEvent e) { 
    System.exit(0); 
  } 
  public void windowActivated   (WindowEvent e) { } 
  public void windowClosed      (WindowEvent e) { } 
  public void windowDeactivated (WindowEvent e) { } 
  public void windowDeiconified (WindowEvent e) { } 
  public void windowIconified   (WindowEvent e) { } 
  public void windowOpened      (WindowEvent e) { } 
 
  public static void main(String[] args) {
    Frame frm = new AWTExample2(); 
    frm.setSize(250, 100); 
    frm.setVisible(true); 
  }  
}
Given the increased level of functionality aren't you surprised by the small number of changes? Of course, something is not shown here, although it is supposed to be very well understood, and already in your toolkit. What is it?

For the second variation we again start from the first program, AWTExample.java.

import java.awt.*;
import java.awt.event.*;
 
public class AWTExample3 extends    Frame 
                         implements ActionListener, 
                                    WindowListener {
 
  private Button up, down, left, right; 
  Circle circle = new Circle(100, 100, 30); 
 
  AWTExample3() {
    up    = new Button("Up"   ); 
    down  = new Button("Down" );
    right = new Button("Right"); 
    left  = new Button("Left" );
 
    FlowLayout layout = new FlowLayout(); 
    setLayout(layout); 
 
    add(up);       up.addActionListener(this); 
    add(down);   down.addActionListener(this); 
    add(right); right.addActionListener(this); 
    add(left);   left.addActionListener(this); 
 
    addWindowListener(this); 
  } 
 
  public void actionPerformed(ActionEvent e) {
    Button button = (Button)e.getSource(); 
    if        (button == up)    {
      System.out.println("Up button pressed."); 
      circle.up(); 
    } else if (button == down)  {
      System.out.println("Down button pressed."); 
      circle.down(); 
    } else if (button == right) {
      System.out.println("Right button pressed."); 
      circle.right(); 
    } else                      {
      System.out.println("Left button pressed."); 
      circle.left(); 
    } 
    repaint(); 
  } 
 
  public void paint(Graphics g) {
    circle.draw(g); 
  }  
 
  public void windowClosing     (WindowEvent e) { 
    System.exit(0); 
  } 
  public void windowActivated   (WindowEvent e) { } 
  public void windowClosed      (WindowEvent e) { } 
  public void windowDeactivated (WindowEvent e) { } 
  public void windowDeiconified (WindowEvent e) { } 
  public void windowIconified   (WindowEvent e) { } 
  public void windowOpened      (WindowEvent e) { } 
 
  public static void main(String[] args) {
    Frame frm = new AWTExample3(); 
    frm.setSize(250, 100); 
    frm.setVisible(true); 
  }  
}
                                     
class Circle {
  int x, y, radius; 
  Circle (int x, int y, int radius) {
    this.x = x; this.y = y; this.radius = radius; 
  } 
  public void draw(Graphics g) { 
    g.drawOval(x, y, 2*radius, 2*radius); 
  } 
  public void up   () { y -= 3; } 
  public void down () { y += 3; } 
  public void right() { x += 3; } 
  public void left () { x -= 3; } 
}
Again, the new characters, compared to the first example, are in blue.

Can you see now how straightforward your assignment #12 is?