All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.widget.SplashScreen Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/* *##% Graphical Widget
 * Copyright (C) 2004 - 2008 CodeLutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * . ##%*/

/* *
 * SplashScreen.java
 *
 * Created: Jul 29, 2004
 *
 * @author Cédric Pineau 
 * @version $Revision: 254 $
 *
 * Last update : $Date: 2010-04-10 01:13:11 +0200 (sam., 10 avril 2010) $
 * by : $Author: tchemit $
 */

package org.nuiton.widget;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

/**
 *  
 */
public class SplashScreen extends JFrame {

    private static final long serialVersionUID=1L;
    
    public final static long DEFAULT_DURATION = 3000;

    public final static long DEFAULT_REMANENCE = 5000;

    public final static int DEFAULT_STEPPING_LEFT_INSET = 100;

    public final static int DEFAULT_STEPPING_RIGHT_INSET = 100;

    public final static int DEFAULT_STEPPING_BOTTOM_INSET = 100;

    public final static Composite defaultComposite = AlphaComposite
            .getInstance(AlphaComposite.SRC_OVER, 0.7f);

    protected long remanence = DEFAULT_REMANENCE;

    protected Image splashImage;

    protected String applicationTitle;

    protected long estimatedDuration;

    protected String[] stepDescriptions;

    protected Image[] stepImages;

    protected int currentStep = -1;

    protected int steppingLeftInset = DEFAULT_STEPPING_LEFT_INSET;

    protected int steppingRightInset = DEFAULT_STEPPING_RIGHT_INSET;

    protected int steppingBottomInset = DEFAULT_STEPPING_BOTTOM_INSET;

    protected Image currentImage;

    protected Composite currentComposite;

    protected ImageAnimator imageAnimator;

    public SplashScreen(String applicationTitle, String splashImagePath) {
        this(applicationTitle, splashImagePath, DEFAULT_DURATION);
    }

    public SplashScreen(String applicationTitle, String splashImagePath,
            long estimatedDuration) {
        this(applicationTitle, splashImagePath, estimatedDuration, null);
    }

    public SplashScreen(String applicationTitle, String splashImagePath,
            long estimatedDuration, String[] stepDescriptions) {
        this(applicationTitle, splashImagePath, estimatedDuration,
                stepDescriptions, null);
    }

    public SplashScreen(String applicationTitle, String splashImagePath,
            String[] stepDescriptions) {
        this(applicationTitle, splashImagePath, DEFAULT_DURATION,
                stepDescriptions, null);
    }

    public SplashScreen(String applicationTitle, String splashImagePath,
            String[] stepDescriptions, String[] stepImagesPaths) {
        this(applicationTitle, splashImagePath, DEFAULT_DURATION,
                stepDescriptions, stepImagesPaths);
    }

    public SplashScreen(String applicationTitle, String splashImagePath,
            long estimatedDuration, String[] stepDescriptions,
            String[] stepIconsPaths) {
        super();
        this.applicationTitle = applicationTitle;
        this.estimatedDuration = estimatedDuration;
        this.stepDescriptions = stepDescriptions;
        initialize(splashImagePath, stepIconsPaths);
    }

    /**
     * @return Returns the stepDescriptions.
     */
    public String[] getstepDescriptions() {
        return stepDescriptions;
    }

    /**
     * @param stepDescriptions The stepDescriptions to set.
     */
    public void setstepDescriptions(String[] stepDescriptions) {
        this.stepDescriptions = stepDescriptions;
    }

    /**
     * @return Returns the steppingInset.
     */
    public int getSteppingLeftInset() {
        return steppingLeftInset;
    }

    /**
     * @param steppingLeftInset The steppingInset to set.
     */
    public void setSteppingLeftInset(int steppingLeftInset) {
        this.steppingLeftInset = steppingLeftInset;
    }

    /**
     * @return Returns the steppingRightInset.
     */
    public int getSteppingRightInset() {
        return steppingRightInset;
    }

    /**
     * @param steppingRightInset The steppingRightInset to set.
     */
    public void setSteppingRightInset(int steppingRightInset) {
        this.steppingRightInset = steppingRightInset;
    }

    /**
     * @return Returns the applicationTitle.
     */
    public String getApplicationTitle() {
        return applicationTitle;
    }

    /**
     * @return Returns the currentStep.
     */
    public int getCurrentStep() {
        return currentStep;
    }

    /**
     * @param currentStep The currentStep to set.
     */
    public void setCurrentStep(int currentStep) {
        this.currentStep = currentStep;
    }

    /**
     * @return Returns the estimatedDuration.
     */
    public long getEstimatedDuration() {
        return estimatedDuration;
    }

    /**
     * @return Returns the splashImage.
     */
    public Image getSplashImage() {
        return splashImage;
    }

    /**
     * @return Returns the stepImages.
     */
    public Image[] getStepImages() {
        return stepImages;
    }

    /**
     * @return Returns the steppingBottomInset.
     */
    public int getSteppingBottomInset() {
        return steppingBottomInset;
    }

    /**
     * @param steppingBottomInset The steppingBottomInset to set.
     */
    public void setSteppingBottomInset(int steppingBottomInset) {
        this.steppingBottomInset = steppingBottomInset;
    }

    /**
     * @return Returns the currentComposite.
     */
    public Composite getCurrentComposite() {
        return currentComposite;
    }

    /**
     * @param currentComposite The currentComposite to set.
     */
    public void setCurrentComposite(Composite currentComposite) {
        this.currentComposite = currentComposite;
    }

    public void nextStep() {
        if (imageAnimator != null) {
            imageAnimator.cancel();
        }
        setCurrentStep(getCurrentStep() + 1);
        currentAlpha = 0.1f;
        currentImage = stepImages[getCurrentStep()];
        imageAnimator = new ImageAnimator();
        new Timer().schedule(imageAnimator, 0, 70);
        repaint();
    }

    public void complete() {
        if (imageAnimator != null) {
            imageAnimator.cancel();
        }
        new Timer().schedule(new FrameHider(), remanence);
        repaint();
    }

    protected float currentAlpha = 0.1f;

    protected boolean alphaUp = true;

    protected void animateImage() {
        if (currentAlpha <= 0.3) {
            alphaUp = true;
        } else if (currentAlpha >= 0.7) {
            alphaUp = false;
        }
        if (alphaUp) {
            currentAlpha += 0.1;
        } else {
            currentAlpha -= 0.1;
        }
        setCurrentComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                currentAlpha));
        repaint();
    }

    protected Image getStepImage(int i) {
        Image image = null;
        if (i == currentStep) {
            image = currentImage;
        } else {
            image = stepImages[i];
        }
        return image;
    }

    /**
     * @param step the required step
     * @return Returns the currentComposite.
     */
    public Composite getCompositeFor(int step) {
        if (step == getCurrentStep()) {
            return getCurrentComposite();
        }
        return defaultComposite;
    }

    protected void paintSplash(Graphics g) {
        g.drawImage(splashImage, 0, 0, null);
        // TODO draw application Title

        // TODO
        if (stepImages == null) {
            // TODO DRAW PROGRESS BAR
            // g.fillRect(50,50,100,3);
        } else {
            // draw step images
            int steppingInset = (getSplashImage().getWidth(null)
                    - getSteppingLeftInset() - getSteppingRightInset())
                    / (stepImages.length + 1);
            int steppingY = getSplashImage().getHeight(null)
                    - getSteppingBottomInset();
            for (int i = 0; i <= currentStep; i++) {
                Image image = getStepImage(i);
                Graphics2D g2 = (Graphics2D) g;
                Composite oldComposite = g2.getComposite();
                g2.setComposite(getCompositeFor(i));
                g2.drawImage(image, getSteppingLeftInset()
                        + (steppingInset * (i + 1)) - image.getWidth(null) / 2,
                        steppingY, null);
                g2.setComposite(oldComposite);
            }
        }
    }

    protected void initialize(String splashImagePath, String[] stepIconsPaths) {
        initializeUI();

        splashImage = getImage(splashImagePath);
        this.setUndecorated(true);
        this.positionAtCenter(splashImage.getWidth(null), splashImage
                .getHeight(null));
        // this.setAlwaysOnTop(true); // TODO JDK5.0 specific
        this.setVisible(true);

        if (stepIconsPaths != null) {
            stepImages = new Image[stepIconsPaths.length];
            for (int i = 0; i < stepIconsPaths.length; i++) {
                stepImages[i] = getImage(stepIconsPaths[i]);
            }
        }
    }

    protected Image getImage(String imagePath) {
        Image result = null;
        java.net.URL imageURL = getClass().getResource(imagePath);
        if (imageURL != null) {
            result = new ImageIcon(imageURL).getImage();
        }
        return result;
    }

    /**
     * Positions the window at the centre of the screen, taking into account the
     * specified width and height
     */
    protected void positionAtCenter(int width, int height) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - width) / 2,
                (screenSize.height - height) / 2, width, height);
    }

    protected void initializeUI() {
        this.setContentPane(getJContentPane());
    }

    protected javax.swing.JPanel jContentPane = null;

    protected javax.swing.JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new javax.swing.JPanel();
            jContentPane
                    .setBorder(javax.swing.BorderFactory
                            .createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
            jContentPane.setLayout(new java.awt.GridBagLayout());
            java.awt.GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.weightx = 1.0D;
            gridBagConstraints.weighty = 1.0D;
            jContentPane.add(getSplashPane(), gridBagConstraints);
        }
        return jContentPane;
    }

    protected javax.swing.JPanel splashPane = null;

    protected javax.swing.JPanel getSplashPane() {
        if (splashPane == null) {
            splashPane = new javax.swing.JPanel() {
                private static final long serialVersionUID=1L;
                
                @Override
                public void paint(Graphics g) {
                    paintSplash(g);
                }
            };
            splashPane.setLayout(new java.awt.BorderLayout());
        }
        return splashPane;
    }

    protected class ImageAnimator extends TimerTask {

        @Override
        public void run() {
            animateImage();
        }

    }

    protected class FrameHider extends TimerTask {

        @Override
        public void run() {
            setVisible(false);
        }

    }

    //

    public static void main(String[] args) {
        SplashScreen f = new SplashScreen(
                "SplashScreen v0.1\n Released under GNU General Public License v2.0",
                "/redGecko.jpg", new String[] {}, new String[] {
                        "/64x64/camera.png", "/64x64/joystick.png",
                        "/64x64/modem.png", "/64x64/mouse.png",
                        "/64x64/pda.png", "/64x64/printer.png",
                        "/64x64/scanner.png", "/64x64/tablet.png" });
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 8; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            f.nextStep();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        f.complete();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy