001 package com.croftsoft.core.animation;
002
003 import java.awt.*;
004 import javax.swing.*;
005
006 import com.croftsoft.core.animation.painter.ArrayComponentPainter;
007 import com.croftsoft.core.animation.updater.ArrayComponentUpdater;
008 import com.croftsoft.core.gui.LifecycleWindowListener;
009 import com.croftsoft.core.lang.NullArgumentException;
010 import com.croftsoft.core.lang.lifecycle.Lifecycle;
011
012 /*********************************************************************
013 * Animated applet.
014 *
015 * @version
016 * $Date: 2008/04/19 21:27:13 $
017 * @since
018 * 2003-03-07
019 * @author
020 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
021 *********************************************************************/
022
023 public class AnimatedApplet
024 extends JApplet
025 implements ComponentAnimator, Lifecycle
026 //////////////////////////////////////////////////////////////////////
027 //////////////////////////////////////////////////////////////////////
028 {
029
030 protected final AnimationInit animationInit;
031
032 protected final AnimatedComponent animatedComponent;
033
034 //
035
036 protected ArrayComponentUpdater arrayComponentUpdater;
037
038 protected ArrayComponentPainter arrayComponentPainter;
039
040 //////////////////////////////////////////////////////////////////////
041 //////////////////////////////////////////////////////////////////////
042
043 public static void main ( String [ ] args )
044 throws Exception
045 //////////////////////////////////////////////////////////////////////
046 {
047 AnimationInit animationInit = null;
048
049 if ( ( args == null )
050 || ( args.length < 1 ) )
051 {
052 animationInit = new AnimationInit ( );
053 }
054 else
055 {
056 animationInit = AnimationInit.load ( args [ 0 ] );
057 }
058
059 launch ( new AnimatedApplet ( animationInit ) );
060 }
061
062 public static void launch ( AnimatedApplet animatedApplet )
063 //////////////////////////////////////////////////////////////////////
064 {
065 AnimationInit animationInit = animatedApplet.animationInit;
066
067 LifecycleWindowListener.launchAppletAsDesktopApp (
068 animatedApplet,
069 animationInit.getFrameTitle ( ),
070 animationInit.getFrameIconFilename ( ),
071 animatedApplet.getClass ( ).getClassLoader ( ),
072 true, // useFullScreenToggler,
073 animationInit.getFrameSize ( ),
074 animationInit.getShutdownConfirmationPrompt ( ) );
075 }
076
077 //////////////////////////////////////////////////////////////////////
078 //////////////////////////////////////////////////////////////////////
079
080 public AnimatedApplet ( AnimationInit animationInit )
081 //////////////////////////////////////////////////////////////////////
082 {
083 NullArgumentException.check ( this.animationInit = animationInit );
084
085 Double frameRate = animationInit.getFrameRate ( );
086
087 if ( frameRate == null )
088 {
089 animatedComponent = new AnimatedComponent ( this );
090 }
091 else
092 {
093 animatedComponent
094 = new AnimatedComponent ( this, frameRate.doubleValue ( ) );
095 }
096 }
097
098 public AnimatedApplet ( )
099 //////////////////////////////////////////////////////////////////////
100 {
101 this ( new AnimationInit ( ) );
102 }
103
104 //////////////////////////////////////////////////////////////////////
105 // Overridden Applet methods
106 //////////////////////////////////////////////////////////////////////
107
108 public String getAppletInfo ( )
109 //////////////////////////////////////////////////////////////////////
110 {
111 return animationInit.getAppletInfo ( );
112 }
113
114 //////////////////////////////////////////////////////////////////////
115 // interface Lifecycle methods
116 //////////////////////////////////////////////////////////////////////
117
118 public void init ( )
119 //////////////////////////////////////////////////////////////////////
120 {
121 String appletInfo = getAppletInfo ( );
122
123 if ( appletInfo != null )
124 {
125 System.out.println ( appletInfo );
126 }
127
128 Color backgroundColor = animationInit.getBackgroundColor ( );
129
130 if ( backgroundColor != null )
131 {
132 animatedComponent.setBackground ( backgroundColor );
133 }
134
135 Color foregroundColor = animationInit.getForegroundColor ( );
136
137 if ( foregroundColor != null )
138 {
139 animatedComponent.setForeground ( foregroundColor );
140 }
141
142 Font font = animationInit.getFont ( );
143
144 if ( font != null )
145 {
146 animatedComponent.setFont ( font );
147 }
148
149 Cursor cursor = animationInit.getCursor ( );
150
151 if ( cursor != null )
152 {
153 animatedComponent.setCursor ( cursor );
154 }
155
156 arrayComponentUpdater = animationInit.getArrayComponentUpdater ( );
157
158 arrayComponentPainter = animationInit.getArrayComponentPainter ( );
159
160 if ( arrayComponentUpdater == null )
161 {
162 arrayComponentUpdater = new ArrayComponentUpdater ( );
163 }
164
165 if ( arrayComponentPainter == null )
166 {
167 arrayComponentPainter = new ArrayComponentPainter ( );
168 }
169
170 Container contentPane = getContentPane ( );
171
172 contentPane.setLayout ( new BorderLayout ( ) );
173
174 contentPane.add ( animatedComponent, BorderLayout.CENTER );
175
176 animatedComponent.init ( );
177
178 validate ( );
179 }
180
181 public void start ( ) { animatedComponent.start ( ); }
182
183 public void stop ( ) { animatedComponent.stop ( ); }
184
185 public void destroy ( ) { animatedComponent.destroy ( ); }
186
187 //////////////////////////////////////////////////////////////////////
188 //////////////////////////////////////////////////////////////////////
189
190 public void update ( JComponent component )
191 //////////////////////////////////////////////////////////////////////
192 {
193 arrayComponentUpdater.update ( component );
194 }
195
196 public void paint (
197 JComponent component,
198 Graphics2D graphics )
199 //////////////////////////////////////////////////////////////////////
200 {
201 arrayComponentPainter.paint ( component, graphics );
202 }
203
204 //////////////////////////////////////////////////////////////////////
205 //////////////////////////////////////////////////////////////////////
206
207 public void addComponentAnimator (
208 ComponentAnimator componentAnimator )
209 //////////////////////////////////////////////////////////////////////
210 {
211 addComponentUpdater ( componentAnimator );
212
213 addComponentPainter ( componentAnimator );
214 }
215
216 public void addComponentPainter (
217 ComponentPainter componentPainter )
218 //////////////////////////////////////////////////////////////////////
219 {
220 arrayComponentPainter.add ( componentPainter );
221 }
222
223 public void addComponentUpdater (
224 ComponentUpdater componentUpdater )
225 //////////////////////////////////////////////////////////////////////
226 {
227 arrayComponentUpdater.add ( componentUpdater );
228 }
229
230 //////////////////////////////////////////////////////////////////////
231 //////////////////////////////////////////////////////////////////////
232 }