1 package org.astrogrid.ui;
2
3 import javax.swing.JWindow;
4 import javax.swing.JLabel;
5 import java.awt.Toolkit;
6 import java.awt.Dimension;
7 import java.awt.Image;
8 import javax.swing.ImageIcon;
9 import java.io.File;
10 import java.net.URL;
11 import java.awt.*;
12 import javax.swing.*;
13
14 /***
15 * A splash screen suitable for displaying as a program loads. Two
16 * uses are provided:
17 * <p>
18 * 1) Pass in the filename to the constructor of an image file. The
19 * splash screen will be created & displayed from that.
20 * <p>
21 * 2) Override the null constructor to assemble your own components
22 * <p>
23 * The splash is displayed as part of the constructor; to remove it
24 * when the application has started, call the dispose() method.
25 * <p>
26 * The GIF load may be quicker than the assembler, as less classes
27 * need to be loaded initially, but there does not appear to be a
28 * significant difference between them.
29 * <p>
30 * NB - This class is growing as more features are added. Given that
31 * splash screens ought to be small and quick, you might want to copy
32 * it to your directory and delete all the stuff you don't need.
33 * <p>
34 * Timestamp provided for a convenient way of timing start-up
35 * @author M Hill
36 */
37
38 public class Splash extends JWindow
39 {
40 /***
41 * Construct and display splash screen from image given by name, at
42 * ./images/ subdirectory of the package this class is in
43 */
44 public Splash(String imageName)
45 {
46 this(new File("./images/"+imageName+".gif"));
47 }
48
49 /***
50 * Construct & display splash screen from image at given file location
51 */
52 public Splash(File file)
53 {
54 this(Toolkit.getDefaultToolkit().createImage(file.getAbsolutePath()));
55 }
56
57 /***
58 * Construct & display splash screen from image at given url
59 */
60 public Splash(URL url)
61 {
62 this(Toolkit.getDefaultToolkit().createImage(url));
63 }
64
65
66 /*** Construct & show splash screen from given image
67 */
68 public Splash(Image splashImage)
69 {
70 getContentPane().add(new JLabel(new ImageIcon(splashImage)));
71 showCenter();
72 }
73
74 /*** Null constructor for descendents that want to construct their own
75 */
76 protected Splash()
77 {}
78
79 /*** General purpose constructor for instant splash-screens */
80 public Splash(String title, String subtitle, String version,
81 Color bg, Color fg,
82 Image coyLogo, String coyName)
83 {
84 makeSplash(title, subtitle, version, bg, fg, coyLogo, coyName);
85 showCenter();
86 }
87
88 public void makeSplash(String title, String subtitle, String version,
89 Color bg, Color fg,
90 Image coyLogo, String coyName)
91 {
92 Container contentPane = getContentPane();
93 contentPane.setBackground(bg);
94 JPanel newContent = new JPanel(new BorderLayout());
95 newContent.setBackground(fg);
96 newContent.setBorder(BorderFactory.createCompoundBorder(
97 BorderFactory.createRaisedBevelBorder(),
98 BorderFactory.createLineBorder(bg, 10)
99 ));
100 contentPane.add(newContent);
101 contentPane = newContent;
102
103
104 JPanel topPanel = new JPanel(new BorderLayout());
105 topPanel.setBackground(bg);
106 JLabel titleLabel = new JLabel(title);
107 titleLabel.setFont(Font.decode("dialog").deriveFont(24F).deriveFont(Font.BOLD));
108 titleLabel.setForeground(fg);
109 topPanel.add(titleLabel, BorderLayout.WEST);
110 if (subtitle != null)
111 {
112 JLabel subTitleLabel = new JLabel(subtitle);
113 subTitleLabel.setFont(titleLabel.getFont().deriveFont(16F));
114 subTitleLabel.setForeground(fg);
115 topPanel.add(subTitleLabel, BorderLayout.SOUTH);
116 }
117 contentPane.add(topPanel, BorderLayout.NORTH);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 JLabel coyLabel = new JLabel();
133 coyLabel.setFont(titleLabel.getFont().deriveFont(12F));
134 coyLabel.setForeground(fg);
135 if (coyLogo != null)
136 coyLabel.setIcon(new ImageIcon(coyLogo));
137 if (coyName != null)
138 coyLabel.setText(coyName);
139
140 JLabel verLabel = new JLabel(version);
141 verLabel.setFont(titleLabel.getFont().deriveFont(12F));
142 verLabel.setForeground(fg);
143
144 JPanel bottomPanel = new JPanel(new BorderLayout());
145 bottomPanel.setBackground(bg);
146 bottomPanel.add(coyLabel, BorderLayout.EAST);
147 bottomPanel.add(verLabel, BorderLayout.WEST);
148
149 contentPane.add(bottomPanel, BorderLayout.SOUTH);
150 }
151
152 /*** Centers splash on screen and sets visible */
153 public void showCenter()
154 {
155
156 pack();
157 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
158 Dimension splashSize = getPreferredSize();
159 setLocation(screenSize.width/2 - (splashSize.width/2),
160 screenSize.height/2 - (splashSize.height/2));
161 setVisible(true);
162 }
163
164
165 /***
166 * "Test harness" to run splash screen
167 */
168 public static void main(String[] args)
169 {
170 new Splash("Test Splash","A demonstration splash screen", "v1.0",
171 Color.green, Color.white,
172 null, "MCH");
173 }
174
175 }
176
177