Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / Swing

Sliding Panel in Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
21 Mar 2013CPOL3 min read 46.8K   2K   8  
Animated sliding panel in Swing.
package slidepanel;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.Timer;

public class SlideAnimator extends Object {

    protected static final int ANIMATION_TIME = 500;
    protected static final int DISPOSE_TIME = 10000;
    protected static final float ANIMATION_TIME_F =
            (float) ANIMATION_TIME;
    protected static final int ANIMATION_DELAY = 50;

    JPanel container;
    JComponent contents;
    AnimatingSheet animatingSheet;
    Rectangle desktopBounds;
    Dimension tempWindowSize;
    Timer animationTimer;
    long animationStart;
    boolean visible = true;

    public SlideAnimator() {
        initDesktopBounds();
    }

    public SlideAnimator(JPanel panel, JComponent contents) {
        this();
        this.container = panel;
        this.contents = contents;
        animatingSheet = new AnimatingSheet();
    }

    public void Dispose() {
        visible = false;
    }

    protected void initDesktopBounds() {
        GraphicsEnvironment env =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        desktopBounds = env.getMaximumWindowBounds();
    }

    public void setContents() {
        visible = true;
        JWindow tempWindow = new JWindow();
        tempWindow.getContentPane().add(contents);
        tempWindow.pack();
        tempWindowSize = tempWindow.getSize();
        tempWindow.getContentPane().removeAll();
        animatingSheet.setSource(contents);
        container.removeAll();
        container.add(animatingSheet);
    }

    public void showAt() {
        setContents();
        ActionListener animationLogic = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                long elapsed =
                        System.currentTimeMillis() - animationStart;
                if (elapsed > ANIMATION_TIME) {
                    container.removeAll();
                    container.add(contents);
                    container.revalidate();
                    animationTimer.stop();
                } else {
                    float progress =
                            (float) elapsed / ANIMATION_TIME_F;
                    int animatingHeight =
                            (int) (progress * tempWindowSize.getHeight());
                    animatingHeight = Math.max(animatingHeight, 1);
                    animatingSheet.setAnimatingHeight(animatingHeight);
                    container.setVisible(true);
                    container.repaint();
                    container.revalidate();
                }
            }
        };
        animationTimer =
                new Timer(ANIMATION_DELAY, animationLogic);
        animationStart = System.currentTimeMillis();
        animationTimer.start();
    }

    @SuppressWarnings("serial")
    class AnimatingSheet extends JPanel {

        Dimension animatingSize = new Dimension(0, 1);
        JComponent source;
        BufferedImage offscreenImage;

        public AnimatingSheet() {
            super();
            setOpaque(true);
        }

        public void setSource(JComponent source) {
            this.source = source;
            animatingSize.width = source.getWidth();
            makeOffscreenImage(source);
        }

        public void setAnimatingHeight(int height) {
            animatingSize.height = height;
            setSize(animatingSize);
        }

        private void makeOffscreenImage(JComponent source) {
            GraphicsEnvironment ge =
                    GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsConfiguration gfxConfig =
                    ge.getDefaultScreenDevice().getDefaultConfiguration();
            offscreenImage =
                    gfxConfig.createCompatibleImage(source.getWidth(),
                    source.getHeight());
            Graphics2D offscreenGraphics =
                    (Graphics2D) offscreenImage.getGraphics();
            // windows workaround
            offscreenGraphics.setColor(source.getBackground());
            offscreenGraphics.fillRect(0, 0,
                    source.getWidth(), source.getHeight());
            // paint from source to offscreen buffer
            source.paint(offscreenGraphics);
        }

        public Dimension getPreferredSize() {
            return animatingSize;
        }

        public Dimension getMinimumSize() {
            return animatingSize;
        }

        public Dimension getMaximumSize() {
            return animatingSize;
        }

        public void update(Graphics g) {
            // override to eliminate flicker from
            // unneccessary clear
            paint(g);
        }

        public void paint(Graphics g) {
            // get the top-most n pixels of source and
            // paint them into g, where n is height
            // (different from sheet example, which used bottom-most)
            BufferedImage fragment =
                    offscreenImage.getSubimage(0,
                    0,
                    source.getWidth(),
                    animatingSize.height);
            g.drawImage(fragment, 0, 0, this);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Make the world open source

Comments and Discussions