KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JSplashScreen


1 /*
2  * Created on Dec 31, 2003
3  */

4 package swingwtx.swing;
5
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.graphics.GC;
8 import org.eclipse.swt.graphics.ImageData;
9 import org.eclipse.swt.graphics.Rectangle;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.widgets.Shell;
12
13 import swingwt.awt.Image;
14
15 /**
16  * In swing, it's a relatively easy process to create a
17  * splash screen. Simply override the paintComponent
18  * method of the JFrame and don't call the superclass's
19  * implementation of paintComponent. Then draw your
20  * splash screen on the JFrame. Simple. Unfortunately,
21  * SwingWT cannot use the same aproach to splash
22  * screens because SwingWT doesn't actually paint the
23  * components, it's the native widget set that does that.
24  * Because of this, the code that used to work to create
25  * a splash screen no longer works (and cannot work).
26  * To make up for this, I have created a JSplashScreen
27  * component which creates a standard SWT splash
28  * screen.
29  *
30  * Rob: This class isn't really necessary as I've now added
31  * support for Java2D to all components/frames so you can
32  * now intercept the paint event and draw your own stuff
33  * on a frame! I've left this in as it is quite a useful
34  * convenience class.
35  *
36  * @author Daniel Spiewak
37  */

38 public class JSplashScreen {
39     public static final int TEXT_LOWER_RIGHT = 0;
40     public static final int TEXT_LOWER_LEFT = 1;
41     public static final int TEXT_UPPER_RIGHT = 2;
42     public static final int TEXT_UPPER_LEFT = 3;
43     public static final int NO_TEXT = 4;
44     
45     private String JavaDoc text = "";
46     private int style = 4;
47     private Shell shell;
48     private Label label;
49     private ImageData data;
50     private org.eclipse.swt.graphics.Image image;
51     
52     public JSplashScreen(Image img, int style) {
53         this.style = style;
54         image = img.image;
55         data = image.getImageData();
56         
57         shell = new Shell(SwingWTUtils.getDisplay(), SWT.NO_TRIM);
58         shell.setSize(data.width, data.height);
59         
60         Rectangle rect = SwingWTUtils.getDisplay().getBounds();
61         int shellX = (rect.width - data.width) / 2;
62         int shellY = (rect.height - data.height) / 2;
63         shell.setLocation(shellX, shellY);
64         
65         shell.open();
66         new GC(shell).drawImage(image, 0, 0);
67         
68         if (style == TEXT_LOWER_RIGHT) {
69             label = new Label(shell, SWT.RIGHT);
70             label.setText(text);
71             label.setLocation(data.width - label.getBounds().width, data.height - label.getBounds().height);
72         } else if (style == TEXT_LOWER_LEFT) {
73             label = new Label(shell, SWT.LEFT);
74             label.setText(text);
75             label.setLocation(0, data.height - label.getBounds().height);
76         } else if (style == TEXT_UPPER_RIGHT) {
77             label = new Label(shell, SWT.RIGHT);
78             label.setText(text);
79             label.setLocation(data.width - label.getBounds().width, 0);
80         } else if (style == TEXT_UPPER_LEFT) {
81             label = new Label(shell, SWT.LEFT);
82             label.setText(text);
83             label.setLocation(0, 0);
84         }
85     }
86     
87     public JSplashScreen(Image img) {
88         this(img, NO_TEXT);
89     }
90     
91     public void setText(String JavaDoc text) {
92         this.text = text;
93         
94         if (style == TEXT_LOWER_RIGHT) {
95             label.setText(text);
96             label.setLocation(data.width - label.getBounds().width, data.height - label.getBounds().height);
97         } else if (style == TEXT_LOWER_LEFT) {
98             label.setText(text);
99             label.setLocation(0, data.height - label.getBounds().height);
100         } else if (style == TEXT_UPPER_RIGHT) {
101             label.setText(text);
102             label.setLocation(data.width - label.getBounds().width, 0);
103         } else if (style == TEXT_UPPER_LEFT) {
104             label.setText(text);
105             label.setLocation(0, 0);
106         }
107     }
108     
109     public void takeDown() {
110         image.dispose();
111         shell.dispose();
112     }
113 }
114
115 /*
116  *****************************************************
117  * Copyright 2003 Completely Random Solutions *
118  * *
119  * DISCLAMER: *
120  * We are not responsible for any damage *
121  * directly or indirectly caused by the usage *
122  * of this or any other class in assosiation *
123  * with this class. Use at your own risk. *
124  * This or any other class by CRS is not *
125  * certified for use in life support systems, *
126  * the Space Shuttle, in use or developement *
127  * of nuclear reactors, weapons of mass *
128  * destruction, or in interplanitary conflict. *
129  * (Unless otherwise specified) *
130  *****************************************************
131  */

132
Popular Tags