Sunday, May 19, 2013

koding Java untuk Splash Screen

*    Author: R. Kevin Cole  kcole@users.sourceforge.net
 */

package rnb.cell;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;


/**
 *     Stringer's splash screen.
 *    $Id$
 *     @author R. Kevin Cole    kcole@users.sourceforge.net
 */
public class JSplash extends JWindow implements ActionListener

{ static MenuUtama m=new MenuUtama();
    /**
     *     Create a splash screen centered within the boundaries of
     *     the screen.
     *
     *     @param    filename    the file name of an image
     *     @param    timeout        the maximum time in milliseconds that the splash
     *         window will be displayed.
     */
    public JSplash(String filename, int timeout)
    {
        super();

        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

        init( filename, timeout, new Point(0,0), size );
    }


    /**
     *     Create a splash screen centered within the boundaries of and
     *     parented by a frame.
     *
     *     @param    parent    becomes the parent of the splash window.
     *     @param    filename    the file name of an image
     *     @param    timeout        the maximum time in milliseconds that the splash
     *         window will be displayed.
     */
    public JSplash(JFrame parent, String filename, int timeout)
    {
        super(parent);

        Dimension size = parent.getSize();
        Point location = parent.getLocation();

        //    a click in the parent's window will dismiss the splash screen
        parent.addMouseListener( new MouseAdapter()
            {
                            @Override
                public void mouseClicked(MouseEvent event)
                {
                    close();
                }
            });

        init( filename, timeout, location, size );
    }


    /**
     *     initialize the splash window
     *
     *     @param    filename    the name of a file containing a graphic that will
     *         be displayed while the splash screen is visible.
     *     @param    location  screen position
     *     @param    size    screen size
     *     @param    timeout    the maximum length of time that the splash screen
     *         will be displayed.
     */
    protected final void init( String filename, int timeout, Point location, Dimension size )
    {
        ImageIcon image = new ImageIcon(getClass().getResource(filename));
        int w = image.getIconWidth() + 5;
        int h = image.getIconHeight() + 5;
               
        int x = (size.width - w) / 2;
        int y = (size.height - h) / 2;
        setBounds(location.x + x, location.y + y, w, h);

        JLabel graphic = new JLabel(image);
        graphic.setBorder(new BevelBorder(BevelBorder.RAISED));

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add("Center", graphic );

        // Close the splash window when any keystroke is detected.
        addKeyListener( new KeyAdapter()
            {
                            @Override
                public void keyPressed(KeyEvent event)
                {
                    close();
                }
            });

        // Close the splash window when any mouse-click is detected.
        addMouseListener( new MouseAdapter()
            {
                            @Override
                public void mouseClicked(MouseEvent event)
                {
                    close();
                }
            });

        //    close after our elapsed time
        Timer timer = new Timer(0, this);
        timer.setInitialDelay(timeout);
        timer.setRepeats(false);
        timer.start();
    }


    /**    close and dispose of the splash screen
     */
    private void close()
    {
        setVisible(false);
        dispose();
               m.setVisible(true);
    }


// -- implementation of ActionListener
//
    /** Dismiss the splash window when a timeout occurs.
     */
    @Override
    public void actionPerformed(ActionEvent event)
    {
        close();
    }
//
// -- end implementation of ActionListener

}

No comments:

Post a Comment