View Javadoc

1   package helloagain.kanday;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.Font;
6   import java.awt.Toolkit;
7   import java.awt.image.BufferedImage;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import javax.imageio.ImageIO;
11  import javax.swing.BorderFactory;
12  import javax.swing.Icon;
13  import javax.swing.ImageIcon;
14  import javax.swing.JLabel;
15  import javax.swing.JPanel;
16  import javax.swing.JWindow;
17  import java.util.logging.Logger;
18  
19  /***
20   * プログラムのスプラッシュ画像を表示するためのウィンドウ。
21   * 画像ファイルは、クラスローダによってリソースとして読み込まれる。
22   * 画像ファイル名:splash.png (固定)
23   * 画像ファイルを置くパス:本クラスと同じパッケージの場所。
24   *
25   * Created: Tue Jun 01 06:04:09 2004
26   *
27   * @author <a href="mailto:torutk@02.246.ne.jp">TAKAHASHI,Toru</a>
28   * @version 1.0
29   */
30  public class KandaySplash extends JWindow {
31  
32      /***
33       * <code>KandaySplash</code>インスタンスを生成する。
34       * コンストラクタ中でウィンドウのサイズ確定(pack)と表示位置の確定を
35       * 行う。
36       */
37      public KandaySplash() {
38          JPanel contentPane = new JPanel();
39          contentPane.setLayout(new BorderLayout());
40          contentPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
41          setContentPane(contentPane);
42  
43          InputStream inStream = getClass().getResourceAsStream("splash.png");
44          JLabel imageLabel = createImageLabel(inStream);
45          try {
46              if (inStream != null) {
47                  inStream.close();
48              }
49          } catch (IOException e) {
50              // do nothing
51          }
52          contentPane.add(imageLabel, BorderLayout.CENTER);
53  
54          JLabel kandayLabel = new JLabel("Desktop Edition", JLabel.CENTER);
55          getContentPane().add(kandayLabel, BorderLayout.SOUTH);
56          pack();
57  
58          Toolkit toolkit = Toolkit.getDefaultToolkit();
59          int screenWidth = toolkit.getScreenSize().width;
60          int screenHeight = toolkit.getScreenSize().height;
61          int x = screenWidth/2 - getWidth()/2;
62          int y = screenHeight/2 - getHeight()/2;
63          setLocation(x, y);
64  
65      }
66  
67      /***
68       * 引数で指定したストリームから画像ファイルを読み込み、その画像を
69       * 載せた<code>JLabel</code>を返却する。
70       * 引数がnullの場合は、テキストを載せた<code>JLabel</code>を返却する。
71       * 
72       * @param inStream 画像データのストリーム。
73       * @retrun JLabelのインスタンス
74       */
75      private final JLabel createImageLabel(final InputStream inStream) {
76          JLabel imageLabel = null;
77          if (inStream != null) {
78              try {
79                  BufferedImage image = ImageIO.read(inStream);
80                  Icon icon = new ImageIcon(image);
81                  imageLabel = new JLabel(icon);
82                  return imageLabel;
83              } catch (IOException e) {
84              }
85          }
86          // 画像が生成できない場合、文字で代替
87          Font font = new Font("Monospaced", Font.BOLD, 36);
88          imageLabel = new JLabel("Java Hello Again");
89          imageLabel.setFont(font);
90          return imageLabel;
91      }
92  
93      private static final String MYCLASS = KandaySplash.class.getName();
94      private static Logger logger = Logger.getLogger(MYCLASS);
95  }