001    /*
002     * cs101 DefaultGameFrame
003     * $Id: DefaultGameFrame.java,v 1.2 2002/11/25 15:21:08 gus Exp $
004     *
005     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
006     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
007     * CS101 homepage</a> or email <las@ai.mit.edu>.
008     *
009     * Copyright (C) 1999 Massachusetts Institute of Technology.
010     * Please do not redistribute without obtaining permission.
011     */
012    
013    package cs101.awt;
014    
015    import java.awt.*;
016    import java.awt.event.*;
017    import cs101.util.gamecontrol.GameControllable;
018    
019    
020    /** 
021     * Simple top level Frame that deals with one component and sizing.
022     *
023     * <P>Copyright (c) 1999 Massachusetts Institute of Technology
024     *
025     * @author Paul K. Njoroge, pnjoroge@mit.edu
026     * @author Lynn Andrea Stein, las@ai.mit.edu
027     * @version $Id: DefaultGameFrame.java
028     */
029    public class DefaultGameFrame extends Frame  {
030    
031      /** The Component (view) to display. */
032      protected Component c;
033    
034      /** The controls for this component. */
035      protected GameControllable gc ;
036    
037      /** Default Frame dimensions.  Initialized in constructor **/
038      private Dimension defaultDimension = new Dimension(500,400);
039    
040      /** 
041       * Creates a new frame relying on the supplied GameControllable to
042       * provide its behavior.  If GameControllable is also a 
043       * java.awt.Component, displays that component in this Frame.
044       * 
045       * Note:  This method does not show the frame.  To show the frame,
046       * you must also call init().
047       *
048       * @see #init
049       * @see #DefaultGameFrame( GameControllable, Component, Dimension )
050       *
051       * @param gc GameControllable to use; may also be Component to show.
052       */
053      public DefaultGameFrame( GameControllable gc )
054      {
055        this( gc, (( gc instanceof Component ) ? ((Component) gc) : null) );
056      }
057    
058      /** 
059       * Creates a new frame surrounding the supplied component and
060       * relying on the GameControllable to provide its behavior.  
061       * Does not show the frame.  To show the frame, use init().
062       *
063       * If no size is supplied, this is where the default dimensions of
064       * this Frame are provided. 
065       *
066       * @see #init
067       * @see #DefaultGameFrame( GameControllable, Component, Dimension )
068       *
069       * @param gc GameControllable that supplies its behavior.
070       * @param c Component to display.  
071       */
072      public DefaultGameFrame(GameControllable gc,Component c)
073      {
074        this( gc, c, new Dimension(500,400) );
075      }
076    
077      /** 
078       * Creates a new frame of the specified size surrounding the
079       * supplied component and relying on the GameControllable to provide
080       * its behavior.  Does not show the frame.  To show the frame, use
081       * init().
082       *
083       * @see #init
084       * @see #DefaultGameFrame( GameControllable, Component, Dimension )
085       *
086       * @param gc GameControllable that supplies its behavior.
087       * @param c Component to display.  
088       * @param x preferred horizontal dimension of the Frame (in pixels) 
089       * @param y preferred vertical dimension of the Frame (in pixels) 
090       */
091      public DefaultGameFrame(GameControllable gc, Component c, int x , int y)
092      {
093        this( gc, c, new Dimension(x,y) );
094      }
095    
096      /** 
097       * Creates a new frame of the specified size surrounding the
098       * supplied component and relying on the GameControllable to provide
099       * its behavior.  Does not show the frame.  To show the frame, use
100       * init().
101       *
102       * @see #init
103       *
104       * @param gc GameControllable that supplies its behavior.
105       * @param c Component to display.  
106       * @param d preferred Dimension of the Frame (in pixels) 
107       */
108      public DefaultGameFrame( GameControllable gc, Component c, Dimension d ) 
109      {
110        super( "GameFrame" );
111    
112        this.c=c;
113        this.gc = gc;
114        this.defaultDimension=d;
115    
116        this.addWindowListener(new WindowAdapter() 
117                               {
118                                 public void windowClosing(WindowEvent e) 
119                                   {
120                                     DefaultGameFrame.this.shutdown();
121                                   }
122                               });
123        
124        MenuBar mbar = new MenuBar();
125        Menu gameMenu = new Menu( "gameControl", true);
126    
127        gameMenu.add( createMenuItem ( "Start...", 
128                                       new ActionListener(){
129          public void actionPerformed( ActionEvent event ) 
130          {
131            DefaultGameFrame.this.gc.start();
132          }}) );
133        
134        gameMenu.add( createMenuItem ( "Stop....", 
135                                       new ActionListener(){
136          public void actionPerformed( ActionEvent event ) 
137          {
138            DefaultGameFrame.this.gc.stop();
139          }}) );
140        
141        gameMenu.add( createMenuItem ( "Reset...", 
142                                       new ActionListener(){
143          public void actionPerformed( ActionEvent event ) 
144          {
145            DefaultGameFrame.this.gc.reset();
146          }}) );
147        
148        /* gameMenu.add( createMenuItem ( "Pause...", 
149                                       new ActionListener(){
150          public void actionPerformed( ActionEvent event ) 
151          {
152            DefaultGameFrame.this.gc.pause();
153          }}) );
154          */
155    
156        gameMenu.add( createCheckboxMenuItem ( "Pause...", 
157                                       new ItemListener(){
158          public void itemStateChanged(ItemEvent event ) 
159            {
160              CheckboxMenuItem item = (CheckboxMenuItem)event.getSource();
161              if (item.getState())
162                {
163                  DefaultGameFrame.this.gc.pause();
164                }
165              else
166                {
167                  DefaultGameFrame.this.gc.unpause();
168                }
169            }}) );
170        
171    
172        gameMenu.addSeparator(); 
173    
174        gameMenu.add( createMenuItem ( "Quit....", 
175                                       new ActionListener(){
176          public void actionPerformed( ActionEvent event ) 
177          {
178            DefaultGameFrame.this.shutdown();
179          }}) );
180        
181        mbar.add( gameMenu );
182        this.setMenuBar( mbar );
183    
184        this.pack();
185      }
186    
187    
188      /**
189       * private (macro-like) helper function to create and initialize the
190       * menu items.
191       *
192       * @see #DefaultGameFrame( 
193       * @param itemLabel  What this menu item is called
194       * @param itemListener  Who this menu item should notify when selected
195       */
196      private final MenuItem createMenuItem( String itemLabel, 
197                                         ActionListener itemListener )
198      {
199        MenuItem theItem = new MenuItem( itemLabel );
200        theItem.addActionListener( itemListener );
201        return theItem ;
202      }
203    
204       private final CheckboxMenuItem createCheckboxMenuItem( String itemLabel, 
205                                         ItemListener itemListener )
206      {
207        CheckboxMenuItem theItem = new CheckboxMenuItem( itemLabel );
208        theItem.addItemListener( itemListener );
209        return theItem ;
210      }
211      
212      
213    
214      /**
215       * Helper method for what to do when this window goes away....
216       */
217      protected void shutdown()
218      {
219        this.dispose();
220        System.exit(0);
221      }
222    
223      /**
224       * Make the frame appear 
225       */
226      public void init() {
227        this.add("Center",c);
228        this.show();
229      }
230    
231        
232      /** 
233       * This method overrides Frame's default getPreferredSize() in case
234       * component doesn't specify its own preferences.  Without this
235       * method, the DefaultFrame has a disconcerting tendency to paint
236       * itself into a tiny little corner....
237       *
238       * @see java.awt.Frame#getPreferredSize()
239       *
240       * @return the default dimension supplied to (or by) the constructor 
241       */
242      public Dimension getPreferredSize()
243      {
244        return this.defaultDimension;
245      }
246    
247    }
248    
249    /*
250     * $Log: DefaultGameFrame.java,v $
251     * Revision 1.2  2002/11/25 15:21:08  gus
252     * fix javadoc error.
253     *
254     * Revision 1.1.1.1  2002/06/05 21:56:32  root
255     * CS101 comes to Olin finally.
256     *
257     * Revision 1.5  2000/04/30 02:33:50  mharder
258     * Changed import statements to confirm to new cs101 packages.
259     *
260     * Revision 1.4  1999/08/16 16:56:48  jsmthng
261     * Updated to make JavaDoc happy.
262     *
263     * Revision 1.3  1999/08/04 21:35:15  pnjoroge
264     * I added a pause checkbox function onto the gamecontrol menu items list.
265     * To pause one clicks on the checkbox and to unPause one clicks it again.
266     *
267     * Revision 1.2  1999/07/27 18:55:54  las
268     * Patched up DefaultFrame (mostly docs) and DefaultGameFrame (aesthetics
269     * and docs).
270     *
271     * Capitalized the name of the GameControllable interface.
272     *
273     * Moved the other four interfaces (Pausable, Resetable, Startable,
274     * Stoppable) to cs101.util as they really have nothing to do w/awt or
275     * windowing in particular.
276     *
277     * Also, added unpause() to Pausable as it doesn't make much sense to be
278     * able to pause but not unpause something.
279     *
280     * Revision 1.1  1999/07/13 20:42:49  pnjoroge
281     * This is a new implementation of DefaultGameFrame. The frame has
282     * four functionalities that have to be implemented by the user . This
283     * include start, stop, reset and pause.
284     * 
285     */
286