001         package com.croftsoft.apps.ajgp.grap;
002         
003         import java.awt.*;
004         import java.awt.image.BufferStrategy;
005         import java.awt.event.*;
006         import java.io.*;
007         import java.security.*;
008         import javax.swing.*;
009    
010         import com.croftsoft.core.CroftSoftConstants;
011         import com.croftsoft.core.animation.AnimatedComponent;
012         import com.croftsoft.core.animation.ComponentAnimator;
013         import com.croftsoft.core.animation.ComponentPainter;
014         import com.croftsoft.core.animation.animator.FrameRateAnimator;
015         import com.croftsoft.core.animation.animator.NullComponentAnimator;
016         import com.croftsoft.core.animation.animator.TileAnimator;
017         import com.croftsoft.core.animation.component
018           .BufferStrategyAnimatedComponent;
019         import com.croftsoft.core.animation.painter.ColorPainter;
020         import com.croftsoft.core.animation.painter.NullComponentPainter;
021         import com.croftsoft.core.awt.image.ImageLib;
022         import com.croftsoft.core.gui.BufferCapabilitiesLib;
023         import com.croftsoft.core.gui.DisplayModeLib;
024         import com.croftsoft.core.gui.FullScreenToggler;
025         import com.croftsoft.core.gui.GraphicsDeviceLib;
026         import com.croftsoft.core.gui.WindowLib;
027         import com.croftsoft.core.lang.lifecycle.Lifecycle;
028    
029         /*********************************************************************
030         * Demonstration of full screen exclusive mode.
031         *
032         * @version
033         *   2003-08-01
034         * @since
035         *   2003-02-24
036         * @author
037         *   <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
038         *********************************************************************/
039    
040         public final class  FullScreenDemo
041           extends JApplet
042           implements ComponentAnimator, Lifecycle
043         //////////////////////////////////////////////////////////////////////
044         //////////////////////////////////////////////////////////////////////
045         {
046           
047         //////////////////////////////////////////////////////////////////////
048         // Applet constants
049         //////////////////////////////////////////////////////////////////////
050    
051         private static final String  VERSION
052           = "2003-08-01";
053    
054         private static final String  TITLE
055           = "CroftSoft Full Screen Demo";
056    
057         private static final String  INFO
058           = "\n" + TITLE
059           + "\n" + CroftSoftConstants.COPYRIGHT
060           + "\n" + CroftSoftConstants.HOME_PAGE
061           + "\n" + "Version " + VERSION
062           + "\n";
063    
064         //////////////////////////////////////////////////////////////////////
065         // animation constants
066         //////////////////////////////////////////////////////////////////////
067    
068         private static final DisplayMode [ ]  DESIRED_DISPLAY_MODES
069           = new DisplayMode [ ] {
070             new DisplayMode ( 640, 480, 8, 85 ),
071             new DisplayMode ( 640, 480, 8, 75 ),
072             new DisplayMode ( 640, 480, 8, 70 ),
073             new DisplayMode ( 640, 480, 8,  0 ),
074             new DisplayMode ( 640, 480, 0,  0 ),
075             new DisplayMode ( 800, 600, 8, 85 ),
076             new DisplayMode ( 800, 600, 8, 75 ),
077             new DisplayMode ( 800, 600, 8, 70 ),
078             new DisplayMode ( 800, 600, 8,  0 ),
079             new DisplayMode ( 800, 600, 0,  0 ) };
080    
081         private static final int     BUFFER_COUNT = 2;
082    
083         private static final String  MEDIA_DIR    = "media/fullscreen/";
084    
085         private static final String  TILE_IMAGE_FILENAME
086           = "clear_brick_32x32.png";
087    
088         //////////////////////////////////////////////////////////////////////
089         // instance variables
090         //////////////////////////////////////////////////////////////////////
091    
092         private final BufferStrategy  bufferStrategy;
093    
094         //
095    
096         private AnimatedComponent  animatedComponent;
097    
098         private ComponentPainter   brickColorPainter;
099    
100         private ComponentAnimator  brickTileAnimator;
101    
102         private ComponentAnimator  frameRateAnimator;
103    
104         //////////////////////////////////////////////////////////////////////
105         //////////////////////////////////////////////////////////////////////
106    
107         public static void  main ( String [ ]  args )
108         //////////////////////////////////////////////////////////////////////
109         {
110           System.out.println ( INFO );
111    
112           JFrame  jFrame = new JFrame ( );
113    
114           jFrame.setUndecorated ( true );
115    
116           GraphicsConfiguration  graphicsConfiguration
117             = jFrame.getGraphicsConfiguration ( );
118    
119           GraphicsDevice  graphicsDevice
120             = graphicsConfiguration.getDevice ( );
121    
122           System.out.println ( "Initial display mode:" );
123    
124           DisplayModeLib.print ( graphicsDevice.getDisplayMode ( ) );
125    
126           BufferStrategy  bufferStrategy = null;
127    
128           try
129           {
130             graphicsDevice.setFullScreenWindow ( jFrame );
131    
132             jFrame.validate ( );
133    
134             jFrame.repaint  ( );
135    
136             System.out.println ( "\nisDisplayChangeSupported:  "
137               + graphicsDevice.isDisplayChangeSupported ( ) );
138    
139             DisplayMode [ ]  desiredDisplayModes = DESIRED_DISPLAY_MODES;
140    
141             if ( args.length == 4 )
142             {
143               desiredDisplayModes = new DisplayMode [ ] {
144                 new DisplayMode (
145                   Integer.parseInt ( args [ 0 ] ),
146                   Integer.parseInt ( args [ 1 ] ),
147                   Integer.parseInt ( args [ 2 ] ),
148                   Integer.parseInt ( args [ 3 ] ) ) };
149             }
150    
151             boolean  displayModeChanged
152               = GraphicsDeviceLib.changeDisplayMode (
153               graphicsDevice, desiredDisplayModes );
154    
155             if ( displayModeChanged )
156             {
157               System.out.println ( "\nNew display mode:" );
158    
159               DisplayModeLib.print ( graphicsDevice.getDisplayMode ( ) );
160             }
161    
162             if ( graphicsDevice.isFullScreenSupported ( ) )
163             {
164               System.out.println ( "\nFull-screen exclusive mode supported" );
165    
166               jFrame.setIgnoreRepaint ( true );
167    
168               jFrame.createBufferStrategy ( BUFFER_COUNT );
169    
170               bufferStrategy = jFrame.getBufferStrategy ( );
171    
172               BufferCapabilitiesLib.print (
173                 bufferStrategy.getCapabilities ( ) );
174             }
175             else
176             {
177               System.out.println (
178                 "\nFull-screen exclusive mode unsupported" );
179             }
180           }
181           catch ( AccessControlException  ex )
182           {
183             System.out.println ( "\nFull-screen exclusive mode not allowed" );
184           }
185    
186           FullScreenDemo  fullScreenDemo
187             = new FullScreenDemo ( bufferStrategy );
188    
189           jFrame.setContentPane ( fullScreenDemo );
190    
191           jFrame.show ( );
192    
193           fullScreenDemo.init  ( );
194    
195           fullScreenDemo.start ( );
196         }
197    
198         //////////////////////////////////////////////////////////////////////
199         //////////////////////////////////////////////////////////////////////
200    
201         public  FullScreenDemo ( BufferStrategy  bufferStrategy )
202         //////////////////////////////////////////////////////////////////////
203         {
204           this.bufferStrategy = bufferStrategy;
205    
206           brickColorPainter = NullComponentPainter .INSTANCE;
207    
208           brickTileAnimator = NullComponentAnimator.INSTANCE;
209    
210           frameRateAnimator = NullComponentAnimator.INSTANCE;
211         }
212    
213         //////////////////////////////////////////////////////////////////////
214         //////////////////////////////////////////////////////////////////////
215    
216         public String  getAppletInfo ( )
217         //////////////////////////////////////////////////////////////////////
218         {
219           return INFO;
220         }
221    
222         //////////////////////////////////////////////////////////////////////
223         // interface Lifecycle methods
224         //////////////////////////////////////////////////////////////////////
225    
226         public void  init ( )
227         //////////////////////////////////////////////////////////////////////
228         {
229           if ( bufferStrategy == null )
230           {
231             animatedComponent = new AnimatedComponent ( this );
232           }
233           else
234           {
235             animatedComponent
236               = new BufferStrategyAnimatedComponent ( this, bufferStrategy );
237           }
238    
239           animatedComponent.addMouseListener (
240             new MouseAdapter ( )
241             {
242               public void  mousePressed ( MouseEvent  mouseEvent )
243               {
244                 FullScreenToggler.toggle (
245                   WindowLib.getParentWindow ( animatedComponent ), false );
246    
247                 System.exit ( 0 );
248               }
249             } );
250    
251           Container  contentPane = getContentPane ( );
252    
253           contentPane.setLayout ( new BorderLayout ( ) );
254    
255           contentPane.add ( animatedComponent, BorderLayout.CENTER );
256    
257           validate ( );
258    
259           animatedComponent.init ( );
260    
261           brickColorPainter = new ColorPainter ( Color.RED );
262    
263           try
264           {
265             Image  brickTileImage = ImageLib.loadAutomaticImage (
266               MEDIA_DIR + TILE_IMAGE_FILENAME,
267               Transparency.BITMASK,
268               animatedComponent,
269               getClass ( ).getClassLoader ( ),
270               null ); // dimension
271    
272             Icon  brickTileIcon = new ImageIcon ( brickTileImage );
273    
274             brickTileAnimator = new TileAnimator (
275               0, 0, brickTileIcon, ( Shape ) null,  1, 1 );
276           }
277           catch ( IOException  ex )
278           {
279             ex.printStackTrace ( );
280           }
281    
282           frameRateAnimator = new FrameRateAnimator ( this, Color.ORANGE );
283         }
284    
285         public void  start   ( ) { animatedComponent.start   ( ); }
286    
287         public void  stop    ( ) { animatedComponent.stop    ( ); }
288    
289         public void  destroy ( ) { animatedComponent.destroy ( ); }
290    
291         //////////////////////////////////////////////////////////////////////
292         // interface ComponentAnimator methods
293         //////////////////////////////////////////////////////////////////////
294    
295         public void  update ( JComponent  component )
296         //////////////////////////////////////////////////////////////////////
297         {
298           brickTileAnimator.update ( component );
299    
300           frameRateAnimator.update ( component );
301         }
302    
303         public void  paint (
304           JComponent  component,
305           Graphics2D  graphics2D )
306         //////////////////////////////////////////////////////////////////////
307         {
308           brickColorPainter.paint ( component, graphics2D );
309    
310           brickTileAnimator.paint ( component, graphics2D );
311    
312           frameRateAnimator.paint ( component, graphics2D );
313         }
314    
315         //////////////////////////////////////////////////////////////////////
316         //////////////////////////////////////////////////////////////////////
317         }