Groovy Documentation

griffon.plugins.splash
[Java] Class SplashScreen

java.lang.Object
  griffon.plugins.splash.SplashScreen
All Implemented Interfaces:
ImageObserver

public class SplashScreen
extends Object

SplashScreen is a general purpose splash screen for application startup. Usage is straight forward: simply construct a SplashScreen at the start of main ( ) and call its splash () method. Proceed with startup as normal.Use showStatus ( String ) f or reporting progress during startup. Finally , at the end of main() call SplashScreen s dispose() method.By default , the splash loads image splash.g i f but you can change this if necessary.

Example 1

 class splasher1 {
 public static void main ( String[ ] args ) {
 SplashScreen splashScreen = new SplashScreen ( ) ;
 splashScreen.splash ( ) ;
 for ( int i = 10 ; i > 0 ; i++) {
 splashScreen.showStatus ( Integer.toString ( i ) + "..." ) ;
 try {
 Thead.sleep (1000) ;
 g
 catch ( Inter ruptedEx cept ion i e ) f g
 g
 ; // frame.show() <   here
 splashScreen.dispose ( ) ;
 ; // frame.show() <   or here
 g
 g
 
 
 

Example 2

 class splasher2 {
 public static void main ( String[ ] args ) {
 SplashScreen splashScreen = new SplashScreen ( ) ;
 
 splashScreen.splash ( ) ;
 t r y {
 Thread.sleep ( 5 0 0 ) ;
 g
 catch ( Inter ruptedEx cept ion i e ) f g
 splashScreen.splashFor ( 1 0 0 0 ) ; // discretion
 splashScreen.dispose ( ) ;
 g
 g
 
Note f o l l owi n g comments quoted from design documentation by R.R.
This example adds splashFor ( 1 0 0 0 ).I f the splash screen i s al ready displayed , i t wai ts f o r at most 1000 mi l l i seconds before r e t u r n i n g.Note t h i s means 1000 mi l l i seconds of t o t a l splash ! So , i { al ready displayed f o r 1000 mi l l i seconds or more , the delay i s 0. In other words , i t avoids f l i c k e r i n g the splash on then o f f i f the splash t iming coincides wi th a p p l i c a t i o n s t a r t up t iming.This i s a compromise between user feedback and c r u f t.I f the splash takes longer than s t a r t up , i t does not appear at a l l.I f al ready di splay f o r a f r a c t i o n of the given t ime , the delay i s only the remainder.

Example 3

 class splasher3 {
 p u b l i c s t a t i c void main ( St r i n g [ ] args ) {
 SplashScreen splashScreen = new SplashScreen ( ) ;
 splashScreen.splash ( ) ;
 t r y {
 Thread.sleep ( 1 0 0 ) ;
 g
 catch ( Inter ruptedEx cept ion i e ) f g
 splashScreen.wai tForSplash ( ) ; // c r u f t zone?
 splashScreen.splashFor (1000) ;
 splashScreen.dispose ( ) ;
 g
 g
 
This example adds wai tForSplash.I t wai ts f o r the splash screen even though the a p p l i c a t i o n loads f a s t e r.Not recommended as some users consider t h i s bad p r a c t i c e.

Example 4

 class splasher4 {
 p u b l i c s t a t i c void main ( St r i n g [ ] args ) {
 SplashScreen.instance ( ).splash ( ) ;
 SplashScreen.instance ( ).delayForSplash ( ) ;
 t r y {
 Thread.sleep ( 1 0 0 ) ;
 
 g
 catch ( Inter ruptedEx cept ion i e ) f g
 SplashScreen.instance ( ).splashFor (1000) ;
 SplashScreen.instance ( ).dispose ( ) ;
 g
 g
 
This example demonstrates two new features of version 1.5 SplashScreen.F i r s t l y , the Singleton pattern.Class scoped method instance ( ) accesses the s i n g l e SplashScreen instance.You can t h e r e f or e access t h i s instance from anywhere.

Secondly , method delayForSplash ( ) appears j u s t a f t e r splash ( ). This possibly delays the main thread , allowing the splash screen to load and display.Tests on some uniprocessor platforms show poor multithreading performance.See Appendix F of design documentation by R.R. The new method bases the extent of delay i { any on number of avail a b l e computing resources.

Modelling

In U.M. L. modelling terms , SplashScreen fulfils the following requirement depicted as a Use Case.

The sketch below outlines the user interface design.

To meet this requirement , the implementation uses the following class design.

Or in full detail as follows.

todo:
Add method or methods for adjusting background colours.
Authors:
Roy Ratcliffe
Version:
1.5


Field Summary
private Frame frame

private Image image

private static SplashScreen instance

private JLabel label

private static SplashScreen singleton

private long splashTime

 
Constructor Summary
SplashScreen(String filename)

Constructs SplashScreen using a given filename for the splash image.

SplashScreen(URL url)

Constructs SplashScreen using a given URL f o r the splash image.

SplashScreen()

Constructs SplashScreen using filename " splash.png " for the image unless you change the default using setImage or call splash with an argument specifying a different image.

 
Method Summary
void delayForSplash()

Optimise splash latenc y by delaying the c a l l i n g thread according to number of processors a v a i l a b l e.Mul t iproces sor plat forms s u c c e s s f u l l y load the splash image i n p a r a l l e l wi th low overhead.Uniprocessors s t ruggle however !

void dispose()

Closes the splash screen i f open , or abandons splash screen i { not al ready open.Re l a t i v e l y long image loading delays the opening.Cal l t h i s method at the end of program s t a r t up , i.e.t y p i c a l l y at the end of main ( ).

static SplashScreen getInstance()

boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)

Runs dur ing image loading.Informs t h i s ImageObserver about progress.

static SplashScreen instance()

Ensures the class has only one instance and gives a global p oi n t of access to i t.The Singleton pat tern avoids using global v a r i a b l e s.Instead , the class i t s e l f references the s i n g l e instance using a class scoped v a r i a b l e , s t a t i c i n Java terms.

void setImage(String filename)

Uses the given filename for the splash image.This method calls Toolkit.getImage which resolves mu l t i p l e requests f o r the same filename to the same Image , u n l i k e createImage which creates a non shared instance of Image.In other words , getImage caches Images , createImage does not.Use splash ( createImage (... i f you want Image privacy.

void setImage(URL url)

Uses the given URL f o r the splash image.

void showStatus(String s)

Changes the s tatus message j u s t below the image.Use i t to di splay s t a r t up progress.

void splash(String filename)

St a r t s the asynchronous splash screen using the given filename f o r the image.

void splash(URL url)

St a r t s the asynchronous splash screen using the given URL f o r the image.

void splash()

St a r t s the asynchronous splash screen using the prev ious l y s p e c i f i e d image , or using filename " splash.g i f " by d e f a u l t i { no image yet s p e c i f i e d.

void splash(Image img)

Splash the screen !

void splashFor(int ms)

Splashes the screen f o r at l e a s t the given number o{ mi l l i seconds i f , and only i f , the splash screen has al ready loaded.I f not al ready splashed , the method returns immediately.Invoke t h i s method before disposing i f you want to for ce a minimum splash per iod.

private void splashScreen()

Runs when the splash image i s f u l l y loaded , a l l i t s b i t s and pieces.

void waitForSplash()

Waits f o r the splash screen to load , returns when the splash s t a r t s.The wai t i s i n d e f i n i t e i f necessary.The operat ion returns immediately i f the splash image has al ready loaded.

void waitForSplash(long ms)

Waits f o r the splash screen to load f o r a l imi t e d amount o{ t ime.Method returns when the splash has loaded , or when the given t ime l imi t expi res.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

frame

private Frame frame


image

private Image image


instance

private static SplashScreen instance


label

private JLabel label


singleton

private static SplashScreen singleton


splashTime

private long splashTime


 
Constructor Detail

SplashScreen

public SplashScreen(String filename)
Constructs SplashScreen using a given filename for the splash image.
Parameters:
filename - name of an image file


SplashScreen

public SplashScreen(URL url)
Constructs SplashScreen using a given URL f o r the splash image.
Parameters:
url - the URL of an image


SplashScreen

public SplashScreen()
Constructs SplashScreen using filename " splash.png " for the image unless you change the default using setImage or call splash with an argument specifying a different image.


 
Method Detail

delayForSplash

public void delayForSplash()
Optimise splash latenc y by delaying the c a l l i n g thread according to number of processors a v a i l a b l e.Mul t iproces sor plat forms s u c c e s s f u l l y load the splash image i n p a r a l l e l wi th low overhead.Uniprocessors s t ruggle however ! This method o f f e r s compromise.I t delays i n d e f i n i t e l y wi th one processor , same as wai tForSplash ( ) ; however , i t returns immediately wi th four our more processors thereby maximising p a r a l l e l execut ion ; or wai ts f o r 500 mi l l i seconds at most wi th dual processors.Cal l delayForSplash ( ) i n place o{ wai tForSplash ( ).


dispose

public void dispose()
Closes the splash screen i f open , or abandons splash screen i { not al ready open.Re l a t i v e l y long image loading delays the opening.Cal l t h i s method at the end of program s t a r t up , i.e.t y p i c a l l y at the end of main ( ).

Implementat ion note. I f you dispose too f a s t , t h i s method could coinc ide wi th splashScreen ( ).We cannot now preempt the other thread.I t needs s ynchroni sat ion.This s i t u a t i o n and i t s requi rement proves i n e v i t a b l e when two threads access the same t h in g.For t h i s reason , methods dispose ( ) and splashScreen ( ) share the synchronized a t t r i b u t e.

pre:
Assumes previous invocat ion of splash ( ).Do not dispose before the splash !


getInstance

public static SplashScreen getInstance()


imageUpdate

public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
Runs dur ing image loading.Informs t h i s ImageObserver about progress. This method does not run i n the main thread , i.e.the thread which c a l l s splash ( ) and dispose ( ).That i s why methods splashScreen ( ) and dispose ( ) are synchronized.


instance

public static SplashScreen instance()
Ensures the class has only one instance and gives a global p oi n t of access to i t.The Singleton pat tern avoids using global v a r i a b l e s.Instead , the class i t s e l f references the s i n g l e instance using a class scoped v a r i a b l e , s t a t i c i n Java terms.

The implementat ion a c t u a l l y mixes s i n g l e t o n and non s i n g l e t o n pat terns.( Tempting to c a l l i t Mu l t i t o n but t h a t r e f e r s to a v a r i a t i o n of Singleton where the instance has many mu l t i p l i c i t y instead of u n i t y.) Co r r e c t l y applying the Singleton pat tern requi res c los ing access to cons t ruc tor methods.However , SplashScreen r e t a i n s p u b l i c cons t ruc tor s , so compromises the pat tern.You can f o l l ow Singleton usage or not at your own d i s c r e t i o n.

Returns:
s i n g l e t o n SplashScreen instance


setImage

public void setImage(String filename)
Uses the given filename for the splash image.This method calls Toolkit.getImage which resolves mu l t i p l e requests f o r the same filename to the same Image , u n l i k e createImage which creates a non shared instance of Image.In other words , getImage caches Images , createImage does not.Use splash ( createImage (... i f you want Image privacy.
Parameters:
filename - name of an image file


setImage

public void setImage(URL url)
Uses the given URL f o r the splash image.
Parameters:
u - r l the URL of an image


showStatus

public void showStatus(String s)
Changes the s tatus message j u s t below the image.Use i t to di splay s t a r t up progress.


splash

public void splash(String filename)
St a r t s the asynchronous splash screen using the given filename f o r the image.
Parameters:
filename - name of an image f i l e


splash

public void splash(URL url)
St a r t s the asynchronous splash screen using the given URL f o r the image.
Parameters:
u - r l the URL of an image


splash

public void splash()
St a r t s the asynchronous splash screen using the prev ious l y s p e c i f i e d image , or using filename " splash.g i f " by d e f a u l t i { no image yet s p e c i f i e d.


splash

public void splash(Image img)
Splash the screen ! Or , i n other words , make a splash ! Ac t u a l l y , t h i s method merely s t a r t s the process o{ splashing.The splash screen w i l l appear sometime l a t e r when the splash image i s ready f o r di splay. Note t h a t t h i s splash screen implementat ion uses f u l l y asynchronous image loading.The splash ( ) method i t s e l f returns to the c a l l e r as soon as pos s ible.The screen appears l a t e r as soon as image loading completes.
pre:
Do not double splash ! I t creates waves ! That i s , do not invoke splash ( ) twice , not wi thout c a l l i n g dispose ( ) i n between.
Parameters:
img - the image used f o r splashing


splashFor

public void splashFor(int ms)
Splashes the screen f o r at l e a s t the given number o{ mi l l i seconds i f , and only i f , the splash screen has al ready loaded.I f not al ready splashed , the method returns immediately.Invoke t h i s method before disposing i f you want to for ce a minimum splash per iod.

Why i s t h i s method synchronized? In order to avoid a race c o n d i t i o n.I t accesses the splashTime a t t r i b u t e which updates i n another thread.

pre:
The argument i s greater than zero. You al ready c a l l e d splash.
Parameters:
ms - mi l l i seconds of minimum splash


splashScreen

private void splashScreen()
Runs when the splash image i s f u l l y loaded , a l l i t s b i t s and pieces.
todo:
Animated splash screens ! I s there a requi rement ? I { so , implement animat ion suppor t.


waitForSplash

public void waitForSplash()
Waits f o r the splash screen to load , returns when the splash s t a r t s.The wai t i s i n d e f i n i t e i f necessary.The operat ion returns immediately i f the splash image has al ready loaded.

Please note f o l l owi n g discussion taken from design documentation by R.R.

As a guide , invoke t h i s method at the end of s t a r t up , not the beginning.Wai t ing f o r the image to load does not make the image load f a s t e r , neces sar i l y.Image loading i s an i np u t bound process , reading from f i l e s y s t em or network.Remaining s t a r t up steps are t y p i c a l l y compute bound and l i k e l y compute resource i s a v a i l a b l e f o r consumption.Most l i k e l y , s t a r t up mixes i np u t and compute resource demands , and pos s ibl y even output.

This g u i d e l i n e appl ies to uniprocessor as wel l as mul t iproces sor plat forms.Wai t ing only wastes any a v a i l a b l e compute cycles.I f you need to delay unnecessar i ly , do t h i s at the end when there i s nothing l e f t to do.Even so , t h i s p r a c t i c e can be viewed as user i n t e r f a c e c r u f t ! Please use wi th care.


waitForSplash

public void waitForSplash(long ms)
Waits f o r the splash screen to load f o r a l imi t e d amount o{ t ime.Method returns when the splash has loaded , or when the given t ime l imi t expi res.
Parameters:
ms - mi l l i seconds to wai t f o r


 

Groovy Documentation