Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple GUI animation using JFrame

I overrode the setVisible() function in my JFrame. All it does is continually grows the frame or shrinks the frame until it is the size I want. This makes for a simple animation. My problem with this though, is that it is choppy, not smooth, and doesn't work well if the frame is moved. Is there a way I can do this cleaner so it is more fluid and smooth?

/**
 * Sets this window visible and also displays an effect when the window becomes
 * visible
 * @param visible True if the window should be visible, false otherwise
 */
@Override
public void setVisible(final boolean visible)
{
    final int effectSpeed = 5;

    if(effect == false)
    {
        super.setVisible(visible);
    }

    if(effect && visible)
    {
        super.setVisible(visible);
        //run the window opening effect
        new Thread(new Runnable()
        {
            public void run()
            {
                for (int i = 0; i <= parent.getHeight(); i = i + effectSpeed)
                {
                    setSize(getPreferredSize().width, i);
                    try
                    {
                        Thread.sleep(1);
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
        }).start();
    }
    else if(effect && visible == false)
    {
        //run the window closing effect
        new Thread(new Runnable()
        {
            public void run()
            {
                for (int i = parent.getHeight(); i >= 0; i = i - effectSpeed)
                {
                    setSize(getPreferredSize().width, i);
                    try
                    {
                        Thread.sleep(1);
                    }
                    catch (Exception e)
                    {
                    }
                }
                DataWindow.super.setVisible(visible);
            }
        }).start();
    }
}
like image 907
user489041 Avatar asked Feb 24 '23 20:02

user489041


2 Answers

You should use Swing Timers for this, instead of creating your own threads.

like image 105
Fortega Avatar answered Feb 26 '23 10:02

Fortega


maybe wrong asked question, nobody know, nice example from Old.Java.Sun.Forum, how to use javax.swing.Timer

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.AlphaComposite;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class ButtonTest {

    private JFrame frame;
    private JButton opaqueButton1;
    private JButton opaqueButton2;
    private SoftJButton softButton1;
    private SoftJButton softButton2;
    private Timer alphaChanger;

    public void createAndShowGUI() {
        opaqueButton1 = new JButton("Opaque Button");
        opaqueButton2 = new JButton("Opaque Button");
        softButton1 = new SoftJButton("Transparent Button");
        softButton2 = new SoftJButton("Transparent Button");
        opaqueButton1.setBackground(Color.GREEN);
        softButton1.setBackground(Color.GREEN);
        frame = new JFrame();
        frame.getContentPane().setLayout(new java.awt.GridLayout(2, 2, 10, 10));
        frame.add(opaqueButton1);
        frame.add(softButton1);
        frame.add(opaqueButton2);
        frame.add(softButton2);
        frame.setSize(700, 300);
        frame.setLocation(150, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        alphaChanger = new Timer(30, new ActionListener() {

            private float incrementer = -.03f;

            @Override
            public void actionPerformed(ActionEvent e) {
                float newAlpha = softButton1.getAlpha() + incrementer;
                if (newAlpha < 0) {
                    newAlpha = 0;
                    incrementer = -incrementer;
                } else if (newAlpha > 1f) {
                    newAlpha = 1f;
                    incrementer = -incrementer;
                }
                softButton1.setAlpha(newAlpha);
                softButton2.setAlpha(newAlpha);
            }
        });
        alphaChanger.start();
        Timer uiChanger = new Timer(3500, new ActionListener() {

            private final LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
            private int index = 1;

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel(laf[index].getClassName());
                    SwingUtilities.updateComponentTreeUI(frame);
                    opaqueButton1.setText(laf[index].getClassName());
                    softButton1.setText(laf[index].getClassName());
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
                index = (index + 1) % laf.length;
            }
        });
        uiChanger.start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ButtonTest().createAndShowGUI();
            }
        });
    }

    private static class SoftJButton extends JButton {

        private static final JButton lafDeterminer = new JButton();
        private static final long serialVersionUID = 1L;
        private boolean rectangularLAF;
        private float alpha = 1f;

        SoftJButton() {
            this(null, null);
        }

        SoftJButton(String text) {
            this(text, null);
        }

        SoftJButton(String text, Icon icon) {
            super(text, icon);

            setOpaque(false);
            setFocusPainted(false);
        }

        public float getAlpha() {
            return alpha;
        }

        public void setAlpha(float alpha) {
            this.alpha = alpha;
            repaint();
        }

        @Override
        public void paintComponent(java.awt.Graphics g) {
            java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            if (rectangularLAF && isBackgroundSet()) {
                Color c = getBackground();
                g2.setColor(c);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            super.paintComponent(g2);
        }

        @Override
        public void updateUI() {
            super.updateUI();
            lafDeterminer.updateUI();
            rectangularLAF = lafDeterminer.isOpaque();
        }
    }
}
like image 25
mKorbel Avatar answered Feb 26 '23 09:02

mKorbel