001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: SplashScreen.java,v $
023   Revision 1.6  2004/05/12 18:55:02  markl
024   comment block updates
025
026   Revision 1.5  2003/01/19 09:50:54  markl
027   Javadoc & comment header updates.
028
029   Revision 1.4  2001/03/12 09:27:01  markl
030   Forced foreground to black.
031
032   Revision 1.3  1999/11/15 03:36:38  markl
033   Fix for phantomFrame reference.
034
035   Revision 1.2  1999/01/10 03:00:07  markl
036   added GPL header & RCS tag
037   ----------------------------------------------------------------------------
038*/
039
040package kiwi.ui;
041
042import java.awt.*;
043
044import kiwi.util.KiwiUtils;
045
046/** This class represents a <i>splash screen</i>: an untitled, frameless window
047  * that briefly appears on the desktop, typically while an application or
048  * installer program is launching. A <code>SplashScreen</code> contains an
049  * image and, optionally, a one-line textual caption. It is drawn with a
050  * 1-pixel wide black border and appears at the center of the screen when
051  * shown. The <code>SplashScreen</code> appears above all other windows on the
052  * desktop.
053  * <p>
054  * As with all <code>Component</code>s, the <code>setForeground()</code> and
055  * <code>setBackground()</code> methods may be called to change the appearance
056  * of the splash screen.
057  *
058  * <p><center>
059  * <img src="snapshot/SplashScreen.gif"><br>
060  * <i>An example SplashScreen.</i>
061  * </center>
062  *
063  * @author Mark Lindner
064  */
065
066public class SplashScreen extends Window
067  {
068  private int delay = 5;
069  private Image image;
070  private String caption;
071
072  /** Construct a new <code>SplashScreen</code>.
073    *
074    * @param image The image to display in the splash screen.
075    * @param caption A short text caption to display below the image (may be
076    * <code>null</code>).
077    */
078
079  public SplashScreen(Image image, String caption)
080    {
081    super(KiwiUtils.getPhantomFrame());
082
083    this.image = image;
084    this.caption = caption;
085
086    setForeground(Color.black);
087    }
088
089  /** Set the display duration.
090    *
091    * @param seconds The number of seconds that the splash screen should remain
092    * onscreen before it is automatically hidden. If 0, it will remain onscreen
093    * until explicitly hidden via a call to <code>setVisible()</code> or
094    * <code>dispose()</code>.
095    *
096    * @exception java.lang.IllegalArgumentException If <code>seconds</code> is
097    * less than 0.
098    */
099
100  public void setDelay(int seconds) throws IllegalArgumentException
101    {
102    if(seconds < 0)
103      throw(new IllegalArgumentException("Delay must be >= 0 seconds."));
104
105    delay = seconds;
106    }
107
108  /** Paint the splash screen. */
109
110  public void paint(Graphics gc)
111    {
112    Dimension size = getSize();
113
114    FontMetrics fm = gc.getFontMetrics();
115
116    gc.setColor(Color.black);
117    gc.drawRect(0, 0, size.width - 1, size.height - 1);
118    gc.drawImage(image, 1, 1, null);
119
120    if(caption != null)
121      {
122      int y = image.getHeight(null) + 2 + fm.getAscent();
123      int x = (size.width - fm.stringWidth(caption)) / 2;
124
125      gc.setColor(getForeground());
126      gc.drawString(caption, x, y);
127      }
128    }
129
130  /** Display or hide the splash screen. The splash screen is displayed on the
131    * desktop, centered on the screen. Although this method returns
132    * immediately, the splash screen remains on the desktop for the duration
133    * of the time delay, or indefinitely if the delay was set to 0.
134    */
135
136  public void setVisible(boolean flag)
137    {
138    if(flag)
139      {
140      pack();
141      KiwiUtils.centerWindow(this);
142      }
143    super.setVisible(flag);
144
145    if(flag && (delay > 0))
146      {
147      Thread thread = new Thread(new Runnable()
148        {
149        public void run()
150          {
151          try
152            {
153            Thread.currentThread().sleep(delay * 1000);
154            }
155          catch(InterruptedException ex) {}
156          dispose();
157          }
158        });
159
160      thread.start();
161      }
162    }
163
164  /** Get the splash screen's preferred size.
165    *
166    * @return The preferred size of the component.
167    */
168
169  public Dimension getPreferredSize()
170    {
171    FontMetrics fm = getGraphics().getFontMetrics();
172
173    Dimension d = new Dimension(image.getWidth(null) + 2,
174                                image.getHeight(null) + 2);
175    if(caption != null) d.height += fm.getHeight() + 2;
176
177    return(d);
178    }
179
180  }
181
182/* end of source file */