001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.frame;
020
021import icy.file.FileUtil;
022import icy.image.ImageUtil;
023import icy.resource.ResourceUtil;
024import icy.util.ClassUtil;
025import icy.util.Random;
026
027import java.awt.Dimension;
028import java.awt.Graphics;
029import java.awt.image.BufferedImage;
030import java.net.URL;
031
032import javax.swing.JFrame;
033import javax.swing.JPanel;
034
035/**
036 * Animated ICY Logo.
037 * 
038 * @author Fab & Stephane
039 */
040public class SplashScreenFrame extends JFrame
041{
042    /**
043     * 
044     */
045    private static final long serialVersionUID = -519109094312389176L;
046
047    public class SplashPanel extends JPanel
048    {
049        private static final long serialVersionUID = -6955085853269659076L;
050
051        private static final String SPLASH_FOLDER = ResourceUtil.IMAGE_PATH + "splash";
052        private static final int DEFAULT_WIDTH = 960;
053        private static final int DEFAULT_HEIGTH = 300;
054
055        private BufferedImage image;
056
057        public SplashPanel()
058        {
059            image = null;
060
061            String[] files = FileUtil.getFiles(SPLASH_FOLDER, null, false, false, false);
062
063            if (files.length > 0)
064                image = ImageUtil.load(files[Random.nextInt(files.length)]);
065            else
066            {
067                try
068                {
069                    files = ClassUtil.getResourcesInPackage(SPLASH_FOLDER, null, true, false, false, false).toArray(
070                            new String[0]);
071                    if (files.length > 0)
072                    {
073                        final URL url = getClass().getResource("/" + files[Random.nextInt(files.length)]);
074                        if (url != null)
075                            image = ImageUtil.load(url, true);
076                    }
077                }
078                catch (Exception e)
079                {
080                    System.err.println("Warning: cannot load splashscreen image");
081                }
082            }
083
084            if (image != null)
085                setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
086            else
087                setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGTH));
088        }
089
090        @Override
091        public void paint(Graphics g)
092        {
093            super.paint(g);
094
095            if (image != null)
096                g.drawImage(image, 0, 0, this);
097        }
098    }
099
100    private final SplashPanel splash;
101
102    /**
103         * 
104         */
105    public SplashScreenFrame()
106    {
107        super("Icy");
108
109        splash = new SplashPanel();
110
111        setUndecorated(true);
112
113        add(splash);
114        pack();
115
116        setLocationRelativeTo(null);
117    }
118}