KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > PookaStartup


1 package net.suberic.pooka.gui;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 import net.suberic.pooka.Pooka;
7
8 /**
9  * Shows a Pooka startup screen. Very simple.
10  */

11 public class PookaStartup {
12
13   JLabel mStatusField;
14   JProgressBar mProgressBar;
15
16   JFrame mFrame;
17
18   /**
19    * Shows the startup screen.
20    */

21   public void show() {
22     mFrame = new JFrame();
23     JPanel fullPanel = new JPanel();
24     fullPanel.setLayout(new BorderLayout());
25     
26     ImagePanel imagePanel = null;
27     try {
28       java.net.URL JavaDoc sourceUrl = this.getClass().getResource(Pooka.getProperty("Pooka.startupImage", "images/PookaSplashscreen.jpg"));
29       Image image = Toolkit.getDefaultToolkit().getImage(sourceUrl);
30       
31       imagePanel = new ImagePanel(image);
32       imagePanel.setSize(300,300);
33       imagePanel.setPreferredSize(new java.awt.Dimension JavaDoc(300,300));
34       
35     } catch (Exception JavaDoc e) {
36       e.printStackTrace();
37     }
38     
39     mStatusField = new JLabel("Loading Pooka...", SwingConstants.CENTER);
40     
41     mProgressBar = new JProgressBar(0, 100);
42     
43     Box progressBox = Box.createVerticalBox();
44     progressBox.add(mStatusField);
45     progressBox.add(mProgressBar);
46     
47     fullPanel.add(imagePanel, BorderLayout.CENTER);
48     fullPanel.add(progressBox, BorderLayout.SOUTH);
49     
50     fullPanel.setBorder(BorderFactory.createEtchedBorder());
51     
52     mFrame.getContentPane().add(fullPanel);
53     
54     SwingUtilities.invokeLater(new Runnable JavaDoc() {
55         public void run() {
56           mFrame.setUndecorated(true);
57           mFrame.pack();
58           
59           Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
60           Dimension frameSize = mFrame.getSize();
61           int x = Math.max(0, (screenSize.width - frameSize.width) / 2);
62           int y = Math.max(0, (screenSize.height - frameSize.height) / 2);
63           
64           mFrame.setLocation(x, y);
65           //mFrame.setVisible(true);
66
mFrame.setLocation(x, y);
67         }
68       });
69   }
70   
71   /**
72    * Sets the current status. Note that this actually takes a resource string
73    * from
74    */

75   public void setStatus(String JavaDoc pStatus) {
76     final String JavaDoc fStatus = pStatus;
77     Runnable JavaDoc runMe = new Runnable JavaDoc() {
78         public void run() {
79           String JavaDoc text = Pooka.getProperty(fStatus + ".label", fStatus);
80           int weight = 10;
81           try {
82             weight = Integer.parseInt(Pooka.getProperty(fStatus + ".weight", "10"));
83           } catch (NumberFormatException JavaDoc nfe) {
84           }
85           mStatusField.setText(text);
86           mStatusField.repaint();
87           
88           try {
89             mProgressBar.setValue(mProgressBar.getValue() + weight);
90           } catch (Exception JavaDoc e) {
91             // if we get an error setting the value, just set to
92
// indeterminate.
93
mProgressBar.setIndeterminate(true);
94           }
95         }
96       };
97     
98     if (SwingUtilities.isEventDispatchThread()) {
99       runMe.run();
100     } else {
101       SwingUtilities.invokeLater(runMe);
102     }
103   }
104   
105   /**
106    * Removes/destroys the startup screen.
107    */

108   public void hide() {
109     /*
110     Runnable runMe = new Runnable() {
111         public void run() {
112           mFrame.setVisible(false);
113           mFrame.dispose();
114         }
115       };
116     
117     if (SwingUtilities.isEventDispatchThread()) {
118       runMe.run();
119     } else {
120       SwingUtilities.invokeLater(runMe);
121     }
122     */

123   }
124   
125   class ImagePanel extends JPanel {
126     Image mImage;
127     
128     ImagePanel(Image pImage) {
129       mImage = pImage;
130     }
131     
132     public void paintComponent(Graphics g) {
133       super.paintComponent(g);
134       
135       int x = (getSize().width - mImage.getWidth(this)) / 2;
136       int y = (getSize().height - mImage.getHeight(this)) / 2;
137       
138       g.drawImage(mImage, x, y, this);
139       
140     }
141   }
142   
143 }
144
Popular Tags