/* ApplicationFrame 
    Defines a window which can contain graphics. 
    From the book ``Java2D'' by Jonathan Knudson, 
    comments by Nina Amenta, Aug 2001. 
*/

import java.awt.*;
import java.awt.event.*;   // For handling WindowClosing

public class ApplicationFrame
    extends Frame {  
    // Frame is defined as part of AWT, it's a window with a border                    

  /* Constructor with no argument calls constructor with string 
    argument, using a default title string for the window. */
  public ApplicationFrame() { this("ApplicationFrame v1.0"); }
 
  /* Constructor which takes title of window as argument */ 
  public ApplicationFrame(String title) {
    super(title); // call constructor of Frame
    createUI();
  }

/* Creates user interface. Only function is to close window
    when it gets a windowClosing event from the AWT */  
  protected void createUI() {
    setSize(500, 400); // method of Component, an anscestor of Frame. 
    // sets size of window
    center();
    
    /* The lines below ask AWT to tell us about  events which occur
       when the user interacts with the window. We override 
       the windowClosing method, 
       which is called by AWT when the user clicks on the X. Notice
       the tricky syntax: the WindowAdaptor object is not assigned to 
       a varaible name. Instead, it's costruction and the implementation 
       of windowClosing is all included as the definition of the 
       argument for addWindowListener. Yikes! Lucky for us we 
       just had to copy this from the book ... */
    addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) {
        dispose();  // Cleans up memory; method of Window, an anscestor of Frame
        System.exit(0);
      }
    });
  }
  
 /* Centers the window in the middle of the monitor. The Toolkit 
    object deals with things related to a particular machine, so 
    it's where we look to find out how big the monitor is (in 
    pixels). */
 public void center() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    // static method of Toolkit class, returns a Toolkit whose method we
    // call.
    Dimension frameSize = getSize();
    int x = (screenSize.width - frameSize.width) / 2;
    int y = (screenSize.height - frameSize.height) / 2;
    // You tell me: what did those last two lines do? 
    setLocation(x, y); // method of Component; look it up! Maybe it'll help!
  }
}
