001    /*
002     Copyright (C) 2003 Adam Olsen
003    
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     any later version.
008    
009     This program 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
012     GNU General Public License for more details.
013    
014     You should have received a copy of the GNU General Public License
015     along with this program; if not, write to the Free Software
016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    
019    package com.valhalla.jbother;
020    
021    import java.awt.AWTException;
022    import java.awt.Color;
023    import java.awt.Dimension;
024    import java.awt.Graphics;
025    import java.awt.Graphics2D;
026    import java.awt.Rectangle;
027    import java.awt.Robot;
028    import java.awt.event.ActionEvent;
029    import java.awt.event.ActionListener;
030    import java.awt.image.BufferedImage;
031    import java.awt.image.ConvolveOp;
032    import java.awt.image.Kernel;
033    import java.io.IOException;
034    
035    import javax.imageio.ImageIO;
036    import javax.swing.JWindow;
037    import javax.swing.SwingUtilities;
038    
039    /**
040     * Displays a splash screen for a second
041     *
042     * @author Adam Olsen
043     * @version 1.0
044     */
045    public class SplashScreen extends JWindow {
046        private javax.swing.Timer splashTimer = new javax.swing.Timer(1000,
047                new SplashHandler());
048    
049        private JBotherLoader loader;
050    
051        private BufferedImage splash = null;
052    
053        public SplashScreen(JBotherLoader loader) {
054            this.loader = loader;
055            BufferedImage image = null;
056            try {
057                image = ImageIO.read(getClass().getClassLoader()
058                        .getResourceAsStream("images/splashimage.png"));
059            } catch (IOException ex) {
060                loader.afterSplash();
061                return;
062            }
063    
064            createShadowPicture(image);
065    
066            setLocationRelativeTo(null);
067            setVisible(true);
068            toFront();
069            splashTimer.start();
070        }
071    
072        public void paint(Graphics g) {
073            if (splash != null) {
074                g.drawImage(splash, 0, 0, null);
075            }
076        }
077    
078        private void createShadowPicture(BufferedImage image) {
079            int width = image.getWidth();
080            int height = image.getHeight();
081            int extra = 14;
082    
083            setSize(new Dimension(width + extra, height + extra));
084            setLocationRelativeTo(null);
085            Rectangle windowRect = getBounds();
086    
087            splash = new BufferedImage(width + extra, height + extra,
088                    BufferedImage.TYPE_INT_ARGB);
089            Graphics2D g2 = (Graphics2D) splash.getGraphics();
090    
091            try {
092                Robot robot = new Robot(getGraphicsConfiguration().getDevice());
093                BufferedImage capture = robot.createScreenCapture(new Rectangle(
094                        windowRect.x, windowRect.y, windowRect.width + extra,
095                        windowRect.height + extra));
096                g2.drawImage(capture, null, 0, 0);
097            } catch (AWTException e) {
098            }
099    
100            BufferedImage shadow = new BufferedImage(width + extra, height + extra,
101                    BufferedImage.TYPE_INT_ARGB);
102            Graphics g = shadow.getGraphics();
103            g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f));
104            g.fillRoundRect(6, 6, width, height, 12, 12);
105    
106            g2.drawImage(shadow, getBlurOp(7), 0, 0);
107            g2.drawImage(image, 0, 0, this);
108        }
109    
110        private ConvolveOp getBlurOp(int size) {
111            float[] data = new float[size * size];
112            float value = 1 / (float) (size * size);
113            for (int i = 0; i < data.length; i++) {
114                data[i] = value;
115            }
116            return new ConvolveOp(new Kernel(size, size, data));
117        }
118    
119        /**
120         * Closes the SplashScreen Closes the SplashScreen after the time has
121         * expired
122         *
123         * @author Adam Olsen
124         * @version 1.0
125         */
126        class SplashHandler implements ActionListener {
127            /**
128             * Called by the <code>javax.swing.Timer</code>
129             */
130            public void actionPerformed(ActionEvent e) {
131                SwingUtilities.invokeLater(new Runnable() {
132                    public void run() {
133                        if( !loader.done() )
134                        {
135                            com.valhalla.Logger.debug( "Loader is not done" );
136                            splashTimer.restart();
137                            return;
138                        }
139                        splashTimer.stop();
140                        setVisible(false);
141                        loader.afterSplash();
142                        dispose();
143                    }
144                });
145            }
146        }
147    }