001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.main;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023: import java.awt.Toolkit;
024: import java.awt.event.MouseAdapter;
025: import java.awt.event.MouseEvent;
026: import java.io.BufferedInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.InputStream;
029:
030: import javax.swing.BorderFactory;
031: import javax.swing.ImageIcon;
032: import javax.swing.JLabel;
033: import javax.swing.JWindow;
034:
035: /**
036: * Splash screen for the application
037: *
038: * @author Jeff Tassin
039: */
040: public class Splash extends JWindow {
041: private static long WAIT_TIME = 2000;
042: private static long m_start_time = 0;
043:
044: public Splash() {
045: try {
046: m_start_time = System.currentTimeMillis();
047:
048: ImageIcon icon = loadImage("jeta.resources/images/forms/logo.png");
049: if (icon == null) {
050: return;
051: }
052:
053: JLabel label = new JLabel(icon);
054: label.setBorder(BorderFactory
055: .createLineBorder(java.awt.Color.black));
056: getContentPane().add(label, BorderLayout.CENTER);
057: pack();
058:
059: addMouseListener(new MouseAdapter() {
060: public void mousePressed(MouseEvent e) {
061: m_start_time = 0;
062: setVisible(false);
063: dispose();
064: }
065: });
066:
067: Dimension screenSize = Toolkit.getDefaultToolkit()
068: .getScreenSize();
069: Dimension labelSize = label.getPreferredSize();
070: setLocation(screenSize.width / 2 - (labelSize.width / 2),
071: screenSize.height / 2 - (labelSize.height / 2));
072: setVisible(true);
073: } catch (Exception e) {
074: e.printStackTrace();
075: }
076: }
077:
078: public void dispose() {
079: if (m_start_time > 0) {
080: try {
081: long current = System.currentTimeMillis();
082: if ((current - m_start_time) < WAIT_TIME) {
083: Thread.currentThread().sleep(
084: WAIT_TIME - (current - m_start_time));
085: }
086: } catch (Exception e) {
087:
088: }
089: }
090: super .dispose();
091: }
092:
093: /**
094: * Loads the main splash screen image
095: */
096: private ImageIcon loadImage(String imageResource) {
097: try {
098: ClassLoader loader = Splash.class.getClassLoader();
099: InputStream istream = loader
100: .getResourceAsStream(imageResource);
101: BufferedInputStream bis = new BufferedInputStream(istream);
102:
103: ByteArrayOutputStream bos = new ByteArrayOutputStream();
104: byte[] buff = new byte[1024];
105: int numread = bis.read(buff);
106: while (numread > 0) {
107: bos.write(buff, 0, numread);
108: numread = bis.read(buff);
109: }
110: bis.close();
111:
112: return new ImageIcon(bos.toByteArray());
113: } catch (Exception e) {
114:
115: }
116: return null;
117: }
118:
119: }
|