001         package com.croftsoft.apps.sprite;
002         
003         import java.awt.*;
004         import java.awt.event.*;
005         import java.awt.image.*;
006         import java.security.*;
007         import javax.swing.*;
008         import javax.swing.event.*;
009    
010         import com.croftsoft.core.gui.BufferCapabilitiesLib;
011         import com.croftsoft.core.gui.LifecycleWindowListener;
012         import com.croftsoft.core.lang.ClassLib;
013         import com.croftsoft.core.lang.lifecycle.Lifecycle;
014         import com.croftsoft.core.animation.AnimatedComponent;
015    
016         /*********************************************************************
017         * Main SpriteDemo class.
018         *
019         * @version
020         *   $Date: 2008/04/19 21:31:00 $
021         * @since
022         *   2001-03-04
023         * @author
024         *   <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
025         *********************************************************************/
026    
027    // rename this to AnimationDemo
028    
029         public final class  SpriteDemo
030           extends JApplet
031           implements ChangeListener, ItemListener, Lifecycle, SpriteConstants
032         //////////////////////////////////////////////////////////////////////
033         //////////////////////////////////////////////////////////////////////
034         {
035    
036         private final BufferStrategy  bufferStrategy;
037    
038         //
039           
040         private AnimatedComponent   animatedComponent;
041    
042         private SpriteAnimator      spriteAnimator;
043    
044         private SpinnerNumberModel  spriteVelocitySpinnerNumberModel;
045    
046         private SpinnerNumberModel  frameRateSpinnerNumberModel;
047    
048         private JCheckBoxMenuItem   paintBackgroundCheckBox;
049    
050         private JCheckBoxMenuItem   paintBricksCheckBox;
051    
052         private JCheckBoxMenuItem   paintSpritesCheckBox;
053    
054         private JCheckBoxMenuItem   paintCloudsCheckBox;
055    
056         private JCheckBoxMenuItem   paintFogNightCheckBox;
057    
058         private JCheckBoxMenuItem   onlySpriteUpdatesCheckBox;
059    
060         private JCheckBoxMenuItem   onlySpriteRegionsCheckBox;
061    
062         private JCheckBoxMenuItem   useSystemClockCheckBox;
063    
064         private JCheckBoxMenuItem   useFixedDelayCheckBox;
065    
066         private JCheckBoxMenuItem   useSwingRepainterCheckBox;
067    
068         private JCheckBoxMenuItem   useDoubleBufferingCheckBox;
069    
070         //////////////////////////////////////////////////////////////////////
071         //////////////////////////////////////////////////////////////////////
072    
073         public static void  main ( String [ ]  args )
074         //////////////////////////////////////////////////////////////////////
075         {
076           JFrame  jFrame = new JFrame ( FRAME_TITLE );
077    
078           try
079           {
080             jFrame.setIconImage ( ClassLib.getResourceAsImage (
081               SpriteDemo.class, FRAME_ICON_FILENAME ) );
082           }
083           catch ( Exception  ex )
084           {
085             ex.printStackTrace ( );
086           }
087    
088           BufferStrategy  bufferStrategy = null;
089    
090           GraphicsConfiguration  graphicsConfiguration
091             = jFrame.getGraphicsConfiguration ( );
092    
093           GraphicsDevice  graphicsDevice
094             = graphicsConfiguration.getDevice ( );
095    
096           if ( graphicsDevice.isFullScreenSupported ( ) )
097           {
098             try
099             {
100               // jFrame.setUndecorated ( );
101    
102               // jFrame.setIgnoreRepaint ( true );
103    
104               graphicsDevice.setFullScreenWindow ( jFrame );
105    
106               System.out.println ( "full screen" );
107    
108               jFrame.createBufferStrategy ( 2 );
109    
110               bufferStrategy = jFrame.getBufferStrategy ( );
111    
112               BufferCapabilitiesLib.print (
113                 bufferStrategy.getCapabilities ( ) );
114             }
115             catch ( AccessControlException  ex )
116             {
117             }
118           }
119    
120    System.out.println ( bufferStrategy );
121    
122           SpriteDemo  spriteDemo = new SpriteDemo ( bufferStrategy );
123    
124           jFrame.setContentPane ( spriteDemo );
125    
126           if ( bufferStrategy == null )
127           {
128             LifecycleWindowListener.launchFrameAsDesktopApp (
129               jFrame,
130               new Lifecycle [ ] { spriteDemo },
131               FRAME_SIZE,
132               SHUTDOWN_CONFIRMATION_PROMPT );
133           }
134           else
135           {
136             jFrame.setDefaultCloseOperation (
137               WindowConstants.DO_NOTHING_ON_CLOSE );
138    
139             jFrame.addWindowListener (
140               new LifecycleWindowListener (
141                 new Lifecycle [ ] { spriteDemo },
142                 SHUTDOWN_CONFIRMATION_PROMPT ) );
143    
144    //       jFrame.show ( );
145           }
146         }
147    
148         //////////////////////////////////////////////////////////////////////
149         //////////////////////////////////////////////////////////////////////
150    
151         public  SpriteDemo ( BufferStrategy  bufferStrategy )
152         //////////////////////////////////////////////////////////////////////
153         {
154           this.bufferStrategy = bufferStrategy;
155         }
156    
157         public  SpriteDemo ( )
158         //////////////////////////////////////////////////////////////////////
159         {
160           this ( null );
161         }
162    
163         //////////////////////////////////////////////////////////////////////
164         //////////////////////////////////////////////////////////////////////
165    
166         public String  getAppletInfo ( )
167         //////////////////////////////////////////////////////////////////////
168         {
169           return INFO;
170         }
171    
172         //////////////////////////////////////////////////////////////////////
173         //////////////////////////////////////////////////////////////////////
174    
175         public void  init ( )
176         //////////////////////////////////////////////////////////////////////
177         {
178           System.out.println ( INFO );
179    
180           spriteAnimator = new SpriteAnimator ( bufferStrategy );
181    
182           animatedComponent = spriteAnimator.getAnimatedComponent ( );
183    
184           animatedComponent.init ( );
185    
186           //
187    
188           spriteVelocitySpinnerNumberModel = new SpinnerNumberModel (
189             DEFAULT_SPRITE_VELOCITY,
190             0.0,
191             999.9,
192             1.0 );
193    
194           spriteVelocitySpinnerNumberModel.addChangeListener ( this );
195    
196           //
197    
198           frameRateSpinnerNumberModel = new SpinnerNumberModel (
199             DEFAULT_FRAME_RATE,
200             0.1,
201             999.9,
202             1.0 );
203    
204           frameRateSpinnerNumberModel.addChangeListener ( this );
205    
206           //
207    
208           JPanel  southPanel = new JPanel ( new GridBagLayout ( ), true );
209    
210           GridBagConstraints  gridBagConstraints = new GridBagConstraints ( );
211    
212           //
213    
214           gridBagConstraints.gridx   = 0;
215    
216           gridBagConstraints.gridy   = 0;
217    
218           gridBagConstraints.weightx = 1.0;
219    
220           gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
221    
222           gridBagConstraints.insets = new Insets ( 1, 2, 2, 1 );
223    
224           southPanel.add (
225             new JLabel ( "Sprite Velocity (pixels/sec)", SwingConstants.RIGHT ),
226             gridBagConstraints );
227    
228           gridBagConstraints.gridx   = 1;
229    
230           gridBagConstraints.gridy   = 0;
231    
232           gridBagConstraints.weightx = 0.0;
233    
234           southPanel.add (
235             new JSpinner ( spriteVelocitySpinnerNumberModel ),
236             gridBagConstraints );
237    
238           //
239    
240           gridBagConstraints.gridx   = 0;
241    
242           gridBagConstraints.gridy   = 1;
243    
244           gridBagConstraints.weightx = 1.0;
245    
246           southPanel.add (
247             new JLabel ( "Frame Rate (frames/sec)", SwingConstants.RIGHT ),
248             gridBagConstraints );
249    
250           gridBagConstraints.gridx   = 1;
251    
252           gridBagConstraints.gridy   = 1;
253    
254           gridBagConstraints.weightx = 0.0;
255    
256           southPanel.add (
257             new JSpinner ( frameRateSpinnerNumberModel ),
258             gridBagConstraints );
259    
260           //
261    
262           JMenuBar  menuBar = new JMenuBar ( );
263    
264           setJMenuBar ( menuBar );
265    
266           //
267    
268           JMenu  optionsMenu = new JMenu ( "Options" );
269    
270           optionsMenu.setMnemonic ( KeyEvent.VK_O );
271    
272           menuBar.add ( optionsMenu );
273    
274           //
275    
276           paintBackgroundCheckBox
277             = new JCheckBoxMenuItem ( "Paint background?", true );
278    
279           optionsMenu.add ( paintBackgroundCheckBox );
280    
281           paintBackgroundCheckBox.addItemListener ( this );
282    
283           //
284    
285           paintBricksCheckBox
286             = new JCheckBoxMenuItem ( "Paint bricks?", true );
287    
288           optionsMenu.add ( paintBricksCheckBox );
289    
290           paintBricksCheckBox.addItemListener ( this );
291    
292           //
293    
294           paintSpritesCheckBox
295             = new JCheckBoxMenuItem ( "Paint sprites?", true );
296    
297           optionsMenu.add ( paintSpritesCheckBox );
298    
299           paintSpritesCheckBox.addItemListener ( this );
300    
301           //
302    
303           paintCloudsCheckBox
304             = new JCheckBoxMenuItem ( "Paint clouds?", true );
305    
306           optionsMenu.add ( paintCloudsCheckBox );
307    
308           paintCloudsCheckBox.addItemListener ( this );
309    
310           //
311    
312           paintFogNightCheckBox
313             = new JCheckBoxMenuItem ( "Paint fog and night?", false );
314    
315           optionsMenu.add ( paintFogNightCheckBox );
316    
317           paintFogNightCheckBox.addItemListener ( this );
318    
319           //
320    
321           onlySpriteUpdatesCheckBox
322             = new JCheckBoxMenuItem ( "Only sprite updates?", false );
323    
324           optionsMenu.add ( onlySpriteUpdatesCheckBox );
325    
326           onlySpriteUpdatesCheckBox.addItemListener ( this );
327    
328           //
329    
330           onlySpriteRegionsCheckBox
331             = new JCheckBoxMenuItem ( "Only sprite regions?", false );
332    
333           optionsMenu.add ( onlySpriteRegionsCheckBox );
334    
335           onlySpriteRegionsCheckBox.addItemListener ( this );
336    
337           //
338    
339           useSystemClockCheckBox
340             = new JCheckBoxMenuItem ( "Use system clock?", false );
341    
342           optionsMenu.add ( useSystemClockCheckBox );
343    
344           useSystemClockCheckBox.addItemListener ( this );
345    
346           //
347    
348           useFixedDelayCheckBox
349             = new JCheckBoxMenuItem ( "Use fixed delay?", false );
350    
351           optionsMenu.add ( useFixedDelayCheckBox );
352    
353           useFixedDelayCheckBox.addItemListener ( this );
354    
355           //
356    
357           useSwingRepainterCheckBox
358             = new JCheckBoxMenuItem ( "Use Swing RepaintManager?", false );
359    
360           optionsMenu.add ( useSwingRepainterCheckBox );
361    
362           useSwingRepainterCheckBox.addItemListener ( this );
363    
364           //
365    
366           useDoubleBufferingCheckBox
367             = new JCheckBoxMenuItem ( "Use double-buffering?", true );
368    
369           optionsMenu.add ( useDoubleBufferingCheckBox );
370    
371           useDoubleBufferingCheckBox.addItemListener ( this );
372    
373           //
374    
375           Container  contentPane = getContentPane ( );
376    
377           contentPane.setLayout ( new BorderLayout ( ) );
378    
379           contentPane.add ( animatedComponent, BorderLayout.CENTER );
380    
381           contentPane.add ( southPanel, BorderLayout.SOUTH );
382    
383           validate ( );
384         }
385    
386         public void  start   ( ) { animatedComponent.start   ( ); }
387    
388         public void  stop    ( ) { animatedComponent.stop    ( ); }
389    
390         public void  destroy ( )
391         //////////////////////////////////////////////////////////////////////
392         {
393           animatedComponent.destroy ( );
394    
395           RepaintManager.currentManager ( animatedComponent )
396             .setDoubleBufferingEnabled ( true );
397         }
398    
399         //////////////////////////////////////////////////////////////////////
400         //////////////////////////////////////////////////////////////////////
401    
402         public void  itemStateChanged ( ItemEvent  itemEvent )
403         //////////////////////////////////////////////////////////////////////
404         {
405           Object  item = itemEvent.getItem ( );
406    
407           if ( item == paintBackgroundCheckBox )
408           {
409             spriteAnimator.setPaintBackground (
410               paintBackgroundCheckBox.isSelected ( ) );
411           }
412           else if ( item == paintBricksCheckBox )
413           {
414             spriteAnimator.setPaintBricks (
415               paintBricksCheckBox.isSelected ( ) );
416           }
417           else if ( item == paintSpritesCheckBox )
418           {
419             spriteAnimator.setPaintSprites (
420               paintSpritesCheckBox.isSelected ( ) );
421           }
422           else if ( item == paintCloudsCheckBox )
423           {
424             spriteAnimator.setPaintClouds (
425               paintCloudsCheckBox.isSelected ( ) );
426           }
427           else if ( item == paintFogNightCheckBox )
428           {
429             spriteAnimator.setPaintFogNight (
430               paintFogNightCheckBox.isSelected ( ) );
431           }
432           else if ( item == onlySpriteUpdatesCheckBox )
433           {
434             spriteAnimator.setOnlySpriteUpdates (
435               onlySpriteUpdatesCheckBox.isSelected ( ) );
436           }       
437           else if ( item == onlySpriteRegionsCheckBox )
438           {
439             spriteAnimator.setOnlySpriteRegions (
440               onlySpriteRegionsCheckBox.isSelected ( ) );
441           }       
442           else if ( item == useSystemClockCheckBox )
443           {
444             spriteAnimator.setUseSystemClock (
445               useSystemClockCheckBox.isSelected ( ) );
446           }       
447           else if ( item == useFixedDelayCheckBox )
448           {
449             setLoopGovernor ( );
450           }
451           else if ( item == useSwingRepainterCheckBox )
452           {
453             spriteAnimator.setUseSwingRepaintCollector (
454               useSwingRepainterCheckBox.isSelected ( ) );
455           }
456           else if ( item == useDoubleBufferingCheckBox )
457           {
458             spriteAnimator.setUseDoubleBuffering (
459               useDoubleBufferingCheckBox.isSelected ( ) );
460           }
461         }
462    
463         public void  stateChanged ( ChangeEvent  changeEvent )
464         //////////////////////////////////////////////////////////////////////
465         {
466           Object  source = changeEvent.getSource ( );
467    
468           if ( source == spriteVelocitySpinnerNumberModel )
469           {
470             Double  value
471               = ( Double ) spriteVelocitySpinnerNumberModel.getValue ( );
472    
473             spriteAnimator.setSpriteVelocity ( value.doubleValue ( ) );         
474           }
475           else if ( source == frameRateSpinnerNumberModel )
476           {
477             setLoopGovernor ( );
478           }
479         }
480    
481         //////////////////////////////////////////////////////////////////////
482         // private methods
483         //////////////////////////////////////////////////////////////////////
484    
485         private void  setLoopGovernor ( )
486         //////////////////////////////////////////////////////////////////////
487         {
488    // what if the frequency is zero?
489    
490           spriteAnimator.setLoopGovernor (
491             useFixedDelayCheckBox.isSelected ( ),
492             ( ( Double ) frameRateSpinnerNumberModel.getValue ( ) )
493               .doubleValue ( ) );
494         }
495    
496         //////////////////////////////////////////////////////////////////////
497         //////////////////////////////////////////////////////////////////////
498         }