1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.common; |
17 | |
18 | import java.awt.Color; |
19 | import java.awt.Dimension; |
20 | import java.awt.Font; |
21 | import java.awt.FontMetrics; |
22 | import java.awt.Graphics; |
23 | import java.awt.Graphics2D; |
24 | import java.awt.RenderingHints; |
25 | import java.awt.Toolkit; |
26 | import java.awt.geom.AffineTransform; |
27 | import java.awt.image.RenderedImage; |
28 | import java.io.IOException; |
29 | import java.net.URL; |
30 | |
31 | import javax.imageio.ImageIO; |
32 | import javax.swing.BorderFactory; |
33 | import javax.swing.JFrame; |
34 | import javax.swing.JPanel; |
35 | import javax.swing.border.Border; |
36 | |
37 | /** |
38 | * Splash screen to show during loading. |
39 | * |
40 | * @author Thomas Aglassinger |
41 | */ |
42 | public class SplashScreen extends JFrame |
43 | { |
44 | private static final float SPLASH_WINDOW_SCALE = 0.3333F; |
45 | |
46 | public SplashScreen(String imageResourcePath) { |
47 | assert imageResourcePath != null; |
48 | |
49 | RenderedImage splashImage = readSplashImage(imageResourcePath); |
50 | float scale = splashScale(splashImage); |
51 | |
52 | JPanel imagePane = new SplashImagePane(splashImage, scale); |
53 | Border blackBorder = BorderFactory.createLineBorder(Color.black); |
54 | |
55 | imagePane.setBorder(blackBorder); |
56 | setResizable(false); |
57 | setUndecorated(true); |
58 | getContentPane().add(imagePane); |
59 | } |
60 | |
61 | public void dispose() { |
62 | super.dispose(); |
63 | } |
64 | |
65 | /** |
66 | * Show the splash screen in the center of the screen. |
67 | */ |
68 | public void splash() { |
69 | pack(); |
70 | |
71 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
72 | int x = (int) (screenSize.getWidth() - getWidth()) / 2; |
73 | int y = (int) (screenSize.getHeight() - getHeight()) / 2; |
74 | |
75 | setLocation(x, y); |
76 | setVisible(true); |
77 | } |
78 | |
79 | private RenderedImage readSplashImage(String imageResourcePath) { |
80 | URL imageUrl = SplashScreen.class.getResource(imageResourcePath); |
81 | |
82 | if (imageUrl == null) { |
83 | // Note: This must work without logging and localization being initialized. |
84 | throw new IllegalArgumentException("cannot find image resource: \"" + imageResourcePath + "\""); |
85 | } |
86 | RenderedImage splashImage; |
87 | |
88 | try { |
89 | splashImage = ImageIO.read(imageUrl); |
90 | } catch (IOException error) { |
91 | // If this failes, it most likely means that the default ImageIO |
92 | // cache directory got trashed. By default, it is located in the |
93 | // system temporary directory, for example "/tmp". You |
94 | // could try to completely remove and recreate this directory |
95 | // as described in the Jomic FAQ. |
96 | |
97 | // Note: This must work without logging and localization being initialized. |
98 | IllegalArgumentException error2 = new IllegalArgumentException( |
99 | "cannot read image resource: \"" + imageResourcePath + "\""); |
100 | |
101 | error2.initCause(error); |
102 | throw error2; |
103 | } |
104 | return splashImage; |
105 | } |
106 | |
107 | private float splashScale(RenderedImage splashImage) { |
108 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
109 | int screenWidth = (int) screenSize.getWidth(); |
110 | int screenHeight = (int) screenSize.getHeight(); |
111 | int actualImageWidth = splashImage.getWidth(); |
112 | int actualImageHeight = splashImage.getHeight(); |
113 | int targetImageWidth = (int) (SPLASH_WINDOW_SCALE * screenWidth); |
114 | int targetImageHeight = (int) (SPLASH_WINDOW_SCALE * screenHeight); |
115 | float widthScale = ((float) targetImageWidth) / actualImageWidth; |
116 | float heightScale = ((float) targetImageHeight) / actualImageHeight; |
117 | float scale = Math.min(widthScale, heightScale); |
118 | |
119 | return scale; |
120 | } |
121 | |
122 | /** |
123 | * JPanel to display the splash image, overlaid by application name and version information. |
124 | */ |
125 | private static class SplashImagePane extends JPanel |
126 | { |
127 | private static final float TEXT_INDENT_PERCENT = 0.97f; |
128 | private Font font; |
129 | private RenderedImage image; |
130 | private float scale; |
131 | |
132 | SplashImagePane(RenderedImage newImage, float newScale) { |
133 | assert newImage != null; |
134 | assert newScale > 0; |
135 | image = newImage; |
136 | scale = newScale; |
137 | |
138 | Dimension panelSize = new Dimension((int) (scale * image.getWidth()), (int) (scale * image.getHeight())); |
139 | |
140 | setPreferredSize(panelSize); |
141 | font = new Font(null); |
142 | } |
143 | |
144 | protected void paintComponent(Graphics g) { |
145 | assert g instanceof Graphics2D; |
146 | |
147 | Graphics2D g2 = (Graphics2D) g; |
148 | |
149 | g2.setFont(font); |
150 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
151 | g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
152 | |
153 | String text = "Jomic " + Version.VERSION_TAG; |
154 | FontMetrics metrics = g2.getFontMetrics(); |
155 | int textWidth = metrics.stringWidth(text); |
156 | int textHeight = metrics.getHeight(); |
157 | |
158 | g2.drawRenderedImage(image, AffineTransform.getScaleInstance(scale, scale)); |
159 | g2.drawString(text, |
160 | TEXT_INDENT_PERCENT * getWidth() - textWidth, |
161 | TEXT_INDENT_PERCENT * getHeight() - textHeight); |
162 | } |
163 | } |
164 | } |