Software Design (CSC-223 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Studies] [Examples] [Readings] [Projects] [API]


Outline of Class 9: AWT

Miscellaneous


The Abstract Windowing Toolkit

Overview

A Simple Sample

 import java.awt.*;
 import java.awt.event.*;
 
 /**
  * A simple program to draw a few concentric circles on the screen.
  * Warning!  It is not possible to quit this program.
  *
  * @author Samuel A. Rebelsky
  * @version 1.0 of 15 September 97
  */
 class Circles1 extends Frame {
   // ----------------------------------------------------------------------
   // Constructors
   // ------------
 
   /** Create a new frame with simple title. */
   public Circles1() {
     setTitle("A Sample Program");
   } // Circles1
 
  
   // ----------------------------------------------------------------------
   // Methods
   // -------
 
   /** Override paint to display a few concentric circles. */
   public void paint(Graphics g) {
     g.setColor(Color.red);
     g.drawOval(10,10,190,190);
     g.setColor(Color.blue);
     g.drawOval(20,20,170,170);
     g.setColor(Color.green);
     g.drawOval(30,30,150,150);
   } // paint
 
 
   // ----------------------------------------------------------------------
   // Main
   // ----
 
   public static void main(String args[]) {
     Circles1 c = new Circles1();
     c.setSize(210,240);
     c.show();
   } // main
 } // Circles1

Java's Event Model

The Sample, Revised

 import java.awt.*;
 import java.awt.event.*;
 
 /**
  * A simple program to draw a few concentric circles on the screen.
  * Actually knows how to quit, and allows the user to make slight
  * modfications to the circles.
  *
  * @author Samuel A. Rebelsky
  * @version 1.0 of 17 September 97
  * @see <code>Circle1</code>
  */
 class CirclesInteract
       extends Frame
       implements WindowListener,KeyListener {
   // ----------------------------------------------------------------------
   // Fields
   // ------
 
   /** How big is the biggest circle? */
   private int diameter;
 
   /** What is the base point of the biggest circle? */
   private int x_pos;
   private int y_pos;
 
 
   // ----------------------------------------------------------------------
   // Constructors
   // ------------
 
   /** Create a new frame with simple title. */
   public CirclesInteract() {
     // Set some stupid title
     setTitle("Circles, Circles Everywhere");
     // Inidicate which events we'll handle
     addWindowListener(this);
     addKeyListener(this);
     // Set up appropriate initial values
     diameter = 100;
     x_pos = 10;
     y_pos = 10;
   } // CirclesInteract
 
  
   // ----------------------------------------------------------------------
   // Methods
   // -------
 
   /** Override <code>paint</code> to display a few concentric circles. */
   public void paint(Graphics g) {
     g.setColor(Color.red);
     g.drawOval(x_pos,y_pos,diameter,diameter);
     g.setColor(Color.blue);
     g.drawOval(x_pos+(diameter/5),y_pos+(diameter/5),
                3*diameter/5, 3*diameter/5);
     g.setColor(Color.green);
     g.drawOval(x_pos+(2*diameter/5),y_pos+(2*diameter/5),
                diameter/5, diameter/5);
   } // paint
 
 
   // ----------------------------------------------------------------------
   // Event handlers
   // --------------
 
   public void keyPressed(KeyEvent e) {
   }
 
   public void keyReleased(KeyEvent e) {
   }
 
   /**
    * Update the circles depending on which key the user pressed.
    * <ul>
    * <li><code>&lt;</code> shrink
    * <li><code>&gt;</code> enlarge
    * <li><code>h</code> left
    * <li><code>l</code> right
    * <li><code>k</code> up
    * <li><code>j</code> down
    * </ul>
    */
   public void keyTyped(KeyEvent e) {
     switch (e.getKeyChar()) {
       case '<':		// Reduce
         diameter -= 10;
         x_pos += 5;	// Keep it centered
         y_pos += 5;
         repaint();	// To see the changes
         break;
       case '>':		// Enlarge
         diameter += 10;
         x_pos -= 5;	// Keep it centered
         y_pos -= 5;
         repaint();	// See the changes
         break;
       case 'k':		// Move up
         y_pos -= 5;	// y values increase top to bottom
         repaint();	// See the changes
         break;
       case 'j':		// Move down
         y_pos += 5;	// y values increase top to bottom
         repaint();	// See the changes
         break;
       case 'h':		// Move left
         x_pos -= 5;	// x values increase left to right
         repaint();	// See the changes
         break;
       case 'l':		// Move right
         x_pos += 5;	// x values increase left to right
         repaint();	// See the changes
         break;
       case 'q':		// Quit
         // Try sending our own event.  I'm not sure if this is the
         // appropriate strategy.
         windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
         break;
     } // switch
   } // keyTyped()
 
   public void windowActivated(WindowEvent e) {
   }
 
   public void windowClosed(WindowEvent e) {
     // Quit the damn program.
     System.exit(0);
   }
 
   public void windowClosing(WindowEvent e) {
     // Get rid of the window!
     this.dispose();
   }
 
   public void windowDeactivated(WindowEvent e) {
   }
 
   public void windowDeiconified(WindowEvent e) {
   }
 
   public void windowIconified(WindowEvent e) {
   }
 
   public void windowOpened(WindowEvent e) {
   }
 
 
   // ----------------------------------------------------------------------
   // Main
   // ----
 
   public static void main(String args[]) {
     CirclesInteract c = new CirclesInteract();
     c.setSize(210,210);
     c.show();
   } // main
 } // CirclesInteract


Outlines: prev next 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42


[News] [Basics] [Syllabus] [Outlines] [Assignments] [Studies] [Examples] [Readings] [Projects] [API]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Mon Nov 10 08:57:09 1997.

This page generated on Mon Nov 10 10:13:08 1997 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu