/* * cs101 DefaultGameFrame * $Id: DefaultGameFrame.java,v 1.2 2002/11/25 15:21:08 gus Exp $ * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see the * CS101 homepage or email . * * Copyright (C) 1999 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */ package cs101.awt; import java.awt.*; import java.awt.event.*; import cs101.util.gamecontrol.GameControllable; /** * Simple top level Frame that deals with one component and sizing. * *

Copyright (c) 1999 Massachusetts Institute of Technology * * @author Paul K. Njoroge, pnjoroge@mit.edu * @author Lynn Andrea Stein, las@ai.mit.edu * @version $Id: DefaultGameFrame.java */ public class DefaultGameFrame extends Frame { /** The Component (view) to display. */ protected Component c; /** The controls for this component. */ protected GameControllable gc ; /** Default Frame dimensions. Initialized in constructor **/ private Dimension defaultDimension = new Dimension(500,400); /** * Creates a new frame relying on the supplied GameControllable to * provide its behavior. If GameControllable is also a * java.awt.Component, displays that component in this Frame. * * Note: This method does not show the frame. To show the frame, * you must also call init(). * * @see #init * @see #DefaultGameFrame( GameControllable, Component, Dimension ) * * @param gc GameControllable to use; may also be Component to show. */ public DefaultGameFrame( GameControllable gc ) { this( gc, (( gc instanceof Component ) ? ((Component) gc) : null) ); } /** * Creates a new frame surrounding the supplied component and * relying on the GameControllable to provide its behavior. * Does not show the frame. To show the frame, use init(). * * If no size is supplied, this is where the default dimensions of * this Frame are provided. * * @see #init * @see #DefaultGameFrame( GameControllable, Component, Dimension ) * * @param gc GameControllable that supplies its behavior. * @param c Component to display. */ public DefaultGameFrame(GameControllable gc,Component c) { this( gc, c, new Dimension(500,400) ); } /** * Creates a new frame of the specified size surrounding the * supplied component and relying on the GameControllable to provide * its behavior. Does not show the frame. To show the frame, use * init(). * * @see #init * @see #DefaultGameFrame( GameControllable, Component, Dimension ) * * @param gc GameControllable that supplies its behavior. * @param c Component to display. * @param x preferred horizontal dimension of the Frame (in pixels) * @param y preferred vertical dimension of the Frame (in pixels) */ public DefaultGameFrame(GameControllable gc, Component c, int x , int y) { this( gc, c, new Dimension(x,y) ); } /** * Creates a new frame of the specified size surrounding the * supplied component and relying on the GameControllable to provide * its behavior. Does not show the frame. To show the frame, use * init(). * * @see #init * * @param gc GameControllable that supplies its behavior. * @param c Component to display. * @param d preferred Dimension of the Frame (in pixels) */ public DefaultGameFrame( GameControllable gc, Component c, Dimension d ) { super( "GameFrame" ); this.c=c; this.gc = gc; this.defaultDimension=d; this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { DefaultGameFrame.this.shutdown(); } }); MenuBar mbar = new MenuBar(); Menu gameMenu = new Menu( "gameControl", true); gameMenu.add( createMenuItem ( "Start...", new ActionListener(){ public void actionPerformed( ActionEvent event ) { DefaultGameFrame.this.gc.start(); }}) ); gameMenu.add( createMenuItem ( "Stop....", new ActionListener(){ public void actionPerformed( ActionEvent event ) { DefaultGameFrame.this.gc.stop(); }}) ); gameMenu.add( createMenuItem ( "Reset...", new ActionListener(){ public void actionPerformed( ActionEvent event ) { DefaultGameFrame.this.gc.reset(); }}) ); /* gameMenu.add( createMenuItem ( "Pause...", new ActionListener(){ public void actionPerformed( ActionEvent event ) { DefaultGameFrame.this.gc.pause(); }}) ); */ gameMenu.add( createCheckboxMenuItem ( "Pause...", new ItemListener(){ public void itemStateChanged(ItemEvent event ) { CheckboxMenuItem item = (CheckboxMenuItem)event.getSource(); if (item.getState()) { DefaultGameFrame.this.gc.pause(); } else { DefaultGameFrame.this.gc.unpause(); } }}) ); gameMenu.addSeparator(); gameMenu.add( createMenuItem ( "Quit....", new ActionListener(){ public void actionPerformed( ActionEvent event ) { DefaultGameFrame.this.shutdown(); }}) ); mbar.add( gameMenu ); this.setMenuBar( mbar ); this.pack(); } /** * private (macro-like) helper function to create and initialize the * menu items. * * @see #DefaultGameFrame( * @param itemLabel What this menu item is called * @param itemListener Who this menu item should notify when selected */ private final MenuItem createMenuItem( String itemLabel, ActionListener itemListener ) { MenuItem theItem = new MenuItem( itemLabel ); theItem.addActionListener( itemListener ); return theItem ; } private final CheckboxMenuItem createCheckboxMenuItem( String itemLabel, ItemListener itemListener ) { CheckboxMenuItem theItem = new CheckboxMenuItem( itemLabel ); theItem.addItemListener( itemListener ); return theItem ; } /** * Helper method for what to do when this window goes away.... */ protected void shutdown() { this.dispose(); System.exit(0); } /** * Make the frame appear */ public void init() { this.add("Center",c); this.show(); } /** * This method overrides Frame's default getPreferredSize() in case * component doesn't specify its own preferences. Without this * method, the DefaultFrame has a disconcerting tendency to paint * itself into a tiny little corner.... * * @see java.awt.Frame#getPreferredSize() * * @return the default dimension supplied to (or by) the constructor */ public Dimension getPreferredSize() { return this.defaultDimension; } } /* * $Log: DefaultGameFrame.java,v $ * Revision 1.2 2002/11/25 15:21:08 gus * fix javadoc error. * * Revision 1.1.1.1 2002/06/05 21:56:32 root * CS101 comes to Olin finally. * * Revision 1.5 2000/04/30 02:33:50 mharder * Changed import statements to confirm to new cs101 packages. * * Revision 1.4 1999/08/16 16:56:48 jsmthng * Updated to make JavaDoc happy. * * Revision 1.3 1999/08/04 21:35:15 pnjoroge * I added a pause checkbox function onto the gamecontrol menu items list. * To pause one clicks on the checkbox and to unPause one clicks it again. * * Revision 1.2 1999/07/27 18:55:54 las * Patched up DefaultFrame (mostly docs) and DefaultGameFrame (aesthetics * and docs). * * Capitalized the name of the GameControllable interface. * * Moved the other four interfaces (Pausable, Resetable, Startable, * Stoppable) to cs101.util as they really have nothing to do w/awt or * windowing in particular. * * Also, added unpause() to Pausable as it doesn't make much sense to be * able to pause but not unpause something. * * Revision 1.1 1999/07/13 20:42:49 pnjoroge * This is a new implementation of DefaultGameFrame. The frame has * four functionalities that have to be implemented by the user . This * include start, stop, reset and pause. * */