001         package com.croftsoft.apps.savor;
002         
003         import java.awt.*;
004         import java.awt.event.*;
005         import java.util.*;
006         
007         import net.java.games.jogl.GL;
008         import net.java.games.jogl.GLCapabilities;
009         import net.java.games.jogl.GLDrawable;
010         import net.java.games.jogl.GLU;
011         
012         import org.jdesktop.jdic.screensaver.JOGLScreensaver;
013         import org.jdesktop.jdic.screensaver.ScreensaverContext;
014         import org.jdesktop.jdic.screensaver.ScreensaverSettings;
015         
016         /*********************************************************************
017         * CroftSoft Savor.
018         *
019         * @version
020         *   $Id: SavorJogl.java,v 1.1 2006/12/09 06:31:36 croft Exp $
021         * @since
022         *   2006-09-26
023         * @author
024         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
025         *********************************************************************/
026    
027         public final class  SavorJogl
028           extends JOGLScreensaver
029         //////////////////////////////////////////////////////////////////////
030         //////////////////////////////////////////////////////////////////////
031         {
032           
033         private static final String  PROPERTY_OVAL_SIZE = "ovalSize";
034           
035         private static final String [ ]  PROPERTY_VALUES = {
036           "tiny",
037           "small",
038           "medium",
039           "large" };
040         
041         private static final int [ ]  OVAL_SIZE_DIVISORS = {
042           Integer.MAX_VALUE,
043           8,
044           4,
045           2 };
046         
047         private static final long  FRAME_PERIOD = 200;
048         
049         //
050         
051         private final Map<String, Integer>  ovalSizeMap;
052         
053         private final Random  random;
054         
055         //
056           
057         private int
058           ovalSize,
059           ovalSizeDivisor,
060           width,
061           height;
062         
063         private boolean
064           toggle,
065           triangle;
066         
067         
068         //////////////////////////////////////////////////////////////////////
069         //////////////////////////////////////////////////////////////////////
070         
071         public  SavorJogl ( )
072         //////////////////////////////////////////////////////////////////////
073         {
074           random = new Random ( );
075           
076           ovalSizeMap = new HashMap<String, Integer> ( );
077           
078           for ( int  i = 0; i < PROPERTY_VALUES.length; i++ )
079           {
080             ovalSizeMap.put (
081               PROPERTY_VALUES [ i ],
082               new Integer ( OVAL_SIZE_DIVISORS [ i ] ) );
083           }
084         }
085         
086         //////////////////////////////////////////////////////////////////////
087         //////////////////////////////////////////////////////////////////////
088         
089         @Override
090         public GLCapabilities  getGLCapabilities ( )
091         //////////////////////////////////////////////////////////////////////
092         {
093           final GLCapabilities  glCapabilities = new GLCapabilities ( );
094           
095           glCapabilities.setDoubleBuffered ( false );
096           
097           return glCapabilities;
098         }
099         
100         public void  init ( final GLDrawable  glDrawable )
101         //////////////////////////////////////////////////////////////////////
102         {
103           final GL  gl = glDrawable.getGL ( );
104           
105           gl.glClearColor ( 0, 0, 0, 0 );
106    
107           ovalSizeDivisor
108             = OVAL_SIZE_DIVISORS [ OVAL_SIZE_DIVISORS.length - 1 ];
109           
110           final ScreensaverContext  screensaverContext = getContext ( );
111           
112           final ScreensaverSettings  screensaverSettings
113             = screensaverContext.getSettings ( );
114           
115           String  propertyValue
116             = screensaverSettings.getProperty ( PROPERTY_OVAL_SIZE );
117           
118           if ( propertyValue != null )
119           {
120             propertyValue = propertyValue.trim ( ).toLowerCase ( );
121             
122             final Integer  ovalSizeDivisorInteger
123               = ovalSizeMap.get ( propertyValue );
124             
125             if ( ovalSizeDivisorInteger != null )
126             {
127               ovalSizeDivisor = ovalSizeDivisorInteger.intValue ( );
128             }
129           }
130           
131           final Component  component = screensaverContext.getComponent ( );
132           
133           component.addKeyListener (
134             new KeyAdapter ( )
135             {
136               @Override
137               public void  keyTyped ( final KeyEvent  keyEvent )
138               {
139                 final char  keyChar = keyEvent.getKeyChar ( );
140                 
141                 if ( keyChar == ' ' )
142                 {
143                   toggle = true;
144                 }
145                 else
146                 {
147                   destroy ( );
148                 }
149               }
150             } );
151           
152           component.requestFocus ( );
153         }
154           
155         public void  reshape (
156           final GLDrawable  glDrawable,
157           final int         x,
158           final int         y,
159           final int         width, 
160                 int         height ) 
161         //////////////////////////////////////////////////////////////////////
162         {
163           final GL  gl = glDrawable.getGL ( );
164           
165           final GLU  glu = glDrawable.getGLU();
166            
167           gl.glViewport ( x, y, width, height);
168           
169           gl.glMatrixMode ( GL.GL_PROJECTION );
170           
171           gl.glLoadIdentity ( );
172           
173           glu.gluOrtho2D ( 0, width, 0, height );
174           
175           ovalSize = Math.max (
176             1,
177             Math.min ( width, height ) / ovalSizeDivisor );
178           
179           this.width  = width  + 2 * ovalSize;
180           
181           this.height = height + 2 * ovalSize;
182           
183           gl.glClear ( GL.GL_COLOR_BUFFER_BIT );       
184         }     
185         
186         public void  displayChanged (
187           final GLDrawable  arg0,
188           final boolean     arg1,
189           final boolean     arg2 )
190         //////////////////////////////////////////////////////////////////////
191         {
192           // ignore
193         }
194         
195         public void  display ( final GLDrawable  glDrawable )
196         //////////////////////////////////////////////////////////////////////
197         {
198           final int  x
199             = ( int ) ( Math.random ( ) * width  ) - ovalSize;
200           
201           final int  y
202             = ( int ) ( Math.random ( ) * height ) - ovalSize;
203           
204           final int  ovalWidth  = Math.max (
205             1, ( int ) ( ovalSize * ( 1 + 0.2 * random.nextGaussian ( ) ) ) );
206           
207           final int  ovalHeight = Math.max (
208             1, ( int ) ( ovalSize * ( 1 + 0.2 * random.nextGaussian ( ) ) ) );
209           
210           final GL  gl = glDrawable.getGL ( );
211           
212           if ( toggle )
213           {
214             toggle = false;
215             
216             triangle = !triangle;
217             
218             gl.glClear ( GL.GL_COLOR_BUFFER_BIT );
219           }
220           
221           gl.glColor3f (
222             ( float ) Math.random ( ),
223             ( float ) Math.random ( ),
224             ( float ) Math.random ( ) );
225           
226           gl.glBegin ( GL.GL_POLYGON );
227           
228           gl.glVertex2i ( x, y );
229           
230           gl.glVertex2i ( x + ovalWidth, y );
231           
232           if ( triangle )
233           {
234             gl.glVertex2i ( x + ovalWidth / 2, y + ovalHeight );
235           }
236           else
237           {
238             gl.glVertex2i ( x + ovalWidth, y + ovalHeight );
239           
240             gl.glVertex2i ( x, y + ovalHeight );
241           }
242           
243           gl.glEnd ( );
244           
245           try
246           {
247             Thread.sleep ( FRAME_PERIOD );
248           }
249           catch ( final InterruptedException  ex )
250           {
251             // ignore
252           }
253         }
254    
255         //////////////////////////////////////////////////////////////////////
256         //////////////////////////////////////////////////////////////////////
257         }