001         package com.croftsoft.apps.rainbow;
002         
003         import java.awt.*;
004         import java.awt.event.*;
005         import java.util.*;
006         import javax.swing.*;
007         import javax.swing.event.*;
008    
009         import com.croftsoft.core.CroftSoftConstants;
010         import com.croftsoft.core.animation.AnimatedComponent;
011         import com.croftsoft.core.animation.ComponentAnimator;
012         import com.croftsoft.core.animation.collector.SimpleRepaintCollector;
013         import com.croftsoft.core.animation.factory.DefaultAnimationFactory;
014         import com.croftsoft.core.gui.FrameLib;
015         import com.croftsoft.core.lang.ClassLib;
016         import com.croftsoft.core.lang.lifecycle.Lifecycle;
017    
018         /*********************************************************************
019         * Main Rainbow class.
020         *
021         * @version
022         *   2003-07-23
023         * @since
024         *   2002-03-18
025         * @author
026         *   <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
027         *********************************************************************/
028    
029         public final class  Rainbow
030           extends JApplet
031           implements ComponentAnimator, Lifecycle
032         //////////////////////////////////////////////////////////////////////
033         //////////////////////////////////////////////////////////////////////
034         {
035           
036         //////////////////////////////////////////////////////////////////////
037         // Applet constants
038         //////////////////////////////////////////////////////////////////////
039    
040         private static final String  VERSION
041           = "2003-07-23";
042    
043         private static final String  TITLE
044           = "CroftSoft Rainbow";
045    
046         private static final String  INFO
047           = "\n" + TITLE + "\n"
048           + CroftSoftConstants.COPYRIGHT + "\n"
049           + CroftSoftConstants.HOME_PAGE + "\n"
050           + "Version " + VERSION + "\n";
051    
052         //////////////////////////////////////////////////////////////////////
053         // Frame constants
054         //////////////////////////////////////////////////////////////////////
055    
056         private static final String  FRAME_TITLE
057           = TITLE;
058    
059         private static final String  FRAME_ICON_FILENAME
060           = "/images/croftsoft.png";
061           
062         private static final Dimension  FRAME_SIZE
063           = null;
064    
065         private static final String  SHUTDOWN_CONFIRMATION_PROMPT
066           = "Close " + TITLE + "?";
067    
068         //////////////////////////////////////////////////////////////////////
069         // animation constants
070         //////////////////////////////////////////////////////////////////////
071    
072         private static final double  FRAME_RATE       = 100.0;
073    
074         private static final int     COLOR_WIDTH      = 64;
075    
076         private static final int     COLOR_HEIGHT     = 64;
077    
078         private static final Color   BACKGROUND_COLOR = Color.WHITE;
079    
080         //////////////////////////////////////////////////////////////////////
081         // instance variables
082         //////////////////////////////////////////////////////////////////////
083    
084         private AnimatedComponent  animatedComponent;
085    
086         private Random             random;
087    
088         private Color              color;
089    
090         private int                x;
091    
092         private int                y;
093    
094         private Point              mousePoint;
095    
096         //////////////////////////////////////////////////////////////////////
097         //////////////////////////////////////////////////////////////////////
098    
099         public static void  main ( String [ ]  args )
100         //////////////////////////////////////////////////////////////////////
101         {
102           JFrame  jFrame = new JFrame ( FRAME_TITLE );
103    
104           try
105           {
106             jFrame.setIconImage ( ClassLib.getResourceAsImage (
107               Rainbow.class, FRAME_ICON_FILENAME ) );
108           }
109           catch ( Exception  ex )
110           {
111           }
112    
113           Rainbow  rainbow = new Rainbow ( );
114    
115           jFrame.setContentPane ( rainbow );
116    
117           FrameLib.launchJFrameAsDesktopApp (
118             jFrame,
119             new Lifecycle [ ] { rainbow },
120             FRAME_SIZE,
121             SHUTDOWN_CONFIRMATION_PROMPT );
122         }
123    
124         //////////////////////////////////////////////////////////////////////
125         //////////////////////////////////////////////////////////////////////
126    
127         public String  getAppletInfo ( )
128         //////////////////////////////////////////////////////////////////////
129         {
130           return INFO;
131         }
132    
133         //////////////////////////////////////////////////////////////////////
134         // interface Lifecycle methods
135         //////////////////////////////////////////////////////////////////////
136    
137         public void  init ( )
138         //////////////////////////////////////////////////////////////////////
139         {
140           System.out.println ( INFO );
141    
142           animatedComponent = new AnimatedComponent (
143             this,
144             new SimpleRepaintCollector ( ),
145             DefaultAnimationFactory.INSTANCE.createLoopGovernor (
146               FRAME_RATE ) );
147    
148           Container  contentPane = getContentPane ( );
149    
150           contentPane.setLayout ( new BorderLayout ( ) );
151    
152           contentPane.add ( animatedComponent, BorderLayout.CENTER );
153    
154           animatedComponent.init ( );
155    
156           color = Color.WHITE;
157    
158           animatedComponent.addMouseMotionListener (
159             new MouseMotionAdapter ( )
160             {
161               public void  mouseMoved ( MouseEvent  mouseEvent )
162               {
163                 mousePoint = mouseEvent.getPoint ( );
164               }
165             } );
166    
167           animatedComponent.addMouseListener (
168             new MouseAdapter ( )
169             {
170               public void  mouseExited ( MouseEvent  mouseEvent )
171               {
172                 mousePoint = null;
173               }
174    
175               public void  mousePressed ( MouseEvent  mouseEvent )
176               {
177                 repaint ( );
178               }
179             } );
180    
181           animatedComponent.setCursor (
182             new Cursor ( Cursor.CROSSHAIR_CURSOR ) );
183    
184           random = new Random ( );
185         }
186    
187         public void  start   ( ) { animatedComponent.start   ( ); }
188    
189         public void  stop    ( ) { animatedComponent.stop    ( ); }
190    
191         public void  destroy ( ) { animatedComponent.destroy ( ); }
192    
193         //////////////////////////////////////////////////////////////////////
194         // interface ComponentAnimator methods
195         //////////////////////////////////////////////////////////////////////
196    
197         public void  update ( JComponent  component )
198         //////////////////////////////////////////////////////////////////////
199         {
200           Point  mousePoint = this.mousePoint;
201    
202           if ( mousePoint != null )
203           {
204             color = new Color (
205               random.nextInt ( 256 ),
206               random.nextInt ( 256 ),
207               random.nextInt ( 256 ) );
208    
209             x = mousePoint.x - COLOR_WIDTH  / 2;
210    
211             y = mousePoint.y - COLOR_HEIGHT / 2;
212    
213             component.repaint ( x, y, COLOR_WIDTH, COLOR_HEIGHT );
214           }
215           else
216           {
217             x = Integer.MIN_VALUE;
218           }
219         }
220    
221         public void  paint (
222           JComponent  component,
223           Graphics2D  graphics )
224         //////////////////////////////////////////////////////////////////////
225         {
226           graphics.setColor ( BACKGROUND_COLOR );
227    
228           graphics.fillRect (
229             0, 0, component.getWidth ( ), component.getHeight ( ) );
230    
231           graphics.setColor ( color );
232    
233           graphics.fillRect ( x, y, COLOR_WIDTH, COLOR_HEIGHT );
234         }
235    
236         //////////////////////////////////////////////////////////////////////
237         //////////////////////////////////////////////////////////////////////
238         }