release 0.9.6
[debian/openrocket] / src / net / sf / openrocket / gui / main / Splash.java
1 package net.sf.openrocket.gui.main;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.RenderingHints;
7 import java.awt.SplashScreen;
8 import java.awt.font.GlyphVector;
9 import java.awt.geom.Rectangle2D;
10 import java.io.IOException;
11
12 import net.sf.openrocket.util.Prefs;
13
14 /**
15  * Helper methods for manipulating the Java runtime splash screen.
16  * <p>
17  * Notes:
18  * SplashScreen.update() takes randomly between 4 and 500 ms to complete,
19  * even after it has been called ~100 times (and therefore pre-compiled).
20  * Therefore it cannot be relied upon to perform for example color fades.
21  * Care should be taken to call update() only once or twice per second.
22  * 
23  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
24  */
25 public class Splash {
26         
27         // The right edge of the text base line for the version string
28         private static final int VERSION_POSITION_X = 617;
29         private static final int VERSION_POSITION_Y = 135;
30         private static final Font VERSION_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 9);
31         private static final Color VERSION_COLOR = Color.WHITE;
32         
33         
34         /**
35          * Initialize the splash screen with additional information.  This method should
36          * be called as soon as reasonably possible during program startup.
37          * 
38          * @return      <code>true</code> if the splash screen could be successfully initialized
39          */
40         public static boolean init() {
41                 // Get the splash screen
42                 SplashScreen s = getSplash();
43                 if (s == null)
44                         return false;
45                 
46                 // Create graphics context and set antialiasing on
47                 Graphics2D g2 = s.createGraphics();
48                 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
49                                 RenderingHints.VALUE_STROKE_NORMALIZE);
50                 g2.setRenderingHint(RenderingHints.KEY_RENDERING, 
51                                 RenderingHints.VALUE_RENDER_QUALITY);
52                 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
53                                 RenderingHints.VALUE_ANTIALIAS_ON);
54                 
55                 // Draw the version number
56                 drawVersionNumber(g2);
57                 
58                 // Update the splash screen
59                 s.update();
60                 return true;
61         }
62         
63         
64         
65         private static void drawVersionNumber(Graphics2D g2) {
66                 String text = "Version " + Prefs.getVersion();
67                 GlyphVector gv = VERSION_FONT.createGlyphVector(g2.getFontRenderContext(), text);
68                 
69                 Rectangle2D rect = gv.getVisualBounds();
70                 double width = rect.getWidth();
71                 
72                 g2.setColor(VERSION_COLOR);
73                 g2.drawGlyphVector(gv, (float)(VERSION_POSITION_X - width), (float)VERSION_POSITION_Y);
74                 
75         }
76         
77         
78         /**
79          * Return the current splash screen or <code>null</code> if not available.
80          * This method catches the possible exceptions and returns null if they occur.
81          * 
82          * @return      the current splash screen, or <code>null</code>.
83          */
84         private static SplashScreen getSplash() {
85                 try {
86                         return SplashScreen.getSplashScreen();
87                 } catch (RuntimeException e) {
88                         return null;
89                 }
90         }
91         
92
93         
94         
95         public static void main(String[] args) throws InterruptedException, IOException {
96                 System.out.println("Calling init...");
97                 System.out.println("Init returned " + Splash.init());
98                 System.out.println("Press enter...");
99                 System.in.read();
100                 System.out.println("Exiting...");
101         }
102
103 }