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