LAB 12  (Comp 170) - Introduction to AWT

The Abstract Window Toolkit (or AWT) is a collection of Java classes designed to be the tools of a windowing system.  AWT is a standard library that accompanies all versions of Java.  In this lab we will explore the use of the AWT through several simple examples (drawn from W. Savitch's Java: An Introduction to Computer Science & Programming).

I   The first example demonstrates the construction of a window using AWT.  We need two classes:  FirstWindow, and WindowDestroyer.  Create a new project using the two classes below, and run the program.

import java.awt.*;
import java.awt.event.*;

public class FirstWindow extends Frame
{
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    public static void main(String[] args)
    {
        FirstWindow myWindow = new FirstWindow();
        myWindow.setSize(WIDTH, HEIGHT);

        WindowDestroyer listener = new WindowDestroyer();
        myWindow.addWindowListener(listener);

        myWindow.setVisible(true);
    }

    public void paint(Graphics g)
    {
        g.drawString("This is my first GUI.", 75, 100);
    }
}

import java.awt.*;
import java.awt.event.*;

public class WindowDestroyer extends WindowAdapter
{
    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
}


(A)  All that this program does is to create a window containing a line of text.  If you click the close-window button on the window, the window disappears.  The units used in the drawString method and in the assignment statements for WIDTH and HEIGHT are in pixels (the smallest unit of space on which your screen can write).  These numbers are implementation dependent.  Try changing the values for WIDTH and HEIGHT to see what happens.  Next, change the numerical values in

g.drawString("This is my first GUI.", 75, 100);
to see what occurs.
Note the the coordinate system has the origin in the upper-left corner of the rectangle; horizontal distance is measured from the right of the origin; vertical distance is measured from below the origin.

(B)  Add several more lines to produce the text:

This is my first GUI.
Click on the box in the upper right-hand
corner to make this window disappear.
(C)   Which statement (upon execution) in the program above results in immediate termination of the program.

II  In this second example we make a few modifications to example I, including the addition of background color to the window and a title for the window, "Second Window".

import java.awt.*;
import java.awt.event.*;

public class SecondWindow extends Frame
{
    public static final int WIDTH = 550;
    public static final int HEIGHT = 400;

    public static void main(String[] args)
    {
        SecondWindow myWindow = new SecondWindow();
        myWindow.setVisible(true);
    }

    public SecondWindow()
    {
        super();
        addWindowListener(new WindowDestroyer());
        setTitle("Second Window Example");
        setSize(WIDTH, HEIGHT);
        setBackground(Color.blue);
    }

    public void paint(Graphics g)
    {
        g.drawString("Color, at last!", 10, 100);
    }
}

(A)   Color.blue is a predefined constant that stands for the color blue.  Try several of the following:
Color.white, Color.gray, Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.magenta, Color.cyan, Color.black

(B)  Change the title of the window, and change the message in the box.

III   Next we will add buttons to the GUI.  Run the following program (along with the WindowDestroyer class).

import java.awt.*;
import java.awt.event.*;

public class ButtonDemo extends Frame implements ActionListener
{
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    public static void main(String[] args)
    {
        ButtonDemo buttonGui = new ButtonDemo();
        buttonGui.setVisible(true);
    }

    public ButtonDemo()
    {
        setSize(WIDTH, HEIGHT);

        addWindowListener(new WindowDestroyer());
        setTitle("Button Demonstration");
        setBackground(Color.blue);
 
        setLayout(new FlowLayout());

        Button stopButton = new Button("Red");
        stopButton.addActionListener(this);
        add(stopButton);

        Button goButton = new Button("Green");
        goButton.addActionListener(this);
        add(goButton);
    }

    public void paint(Graphics g)
    {
        g.drawString(theText, 75, 100);
    }
 
    public void actionPerformed(ActionEvent e)
    {
       if (e.getActionCommand().equals("Red"))
        {
            setBackground(Color.red);
            theText = "STOP";
        }
        else if (e.getActionCommand().equals("Green"))
        {
           setBackground(Color.green);
           theText = "GO";
        }
        else
            theText = "Error in button interface.";

       repaint(); //force color and text change
    }
 
    private String theText = "Explore my buttons!";
}

(A)   Study the above program.  Add a third button, cautionButton, which when clicked will result in the background color being changed to Yellow, and the message "Caution" appearing.

(B)  Using the method

void setForeground (Color c)
change the color of the text.
 
 

Course Home Page          Department Home Page        Loyola Home Page