Java JDialog Fade fadeIn(final JDialog dialog, int delay, final float incrementSize)

Here you can find the source of fadeIn(final JDialog dialog, int delay, final float incrementSize)

Description

Creates an animation to fade the dialog opacity from 0 to 1.

License

Apache License

Parameter

Parameter Description
dialog the dialog to fade in
delay the delay in ms before starting and between each change
incrementSize the increment size

Declaration

public static void fadeIn(final JDialog dialog, int delay, final float incrementSize) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.Timer;

public class Main {
    /**//from   w w  w  . j a v a2  s .  co m
     * Creates an animation to fade the dialog opacity from 0 to 1. Using a
     * default delay of 5 ms and an increment size of 0.05.
     *
     * @param dialog the dialog to fade in
     */
    public static void fadeIn(final JDialog dialog) {
        fadeIn(dialog, 5, 0.05f);
    }

    /**
     * Creates an animation to fade the dialog opacity from 0 to 1.
     *
     * @param dialog the dialog to fade in
     * @param delay the delay in ms before starting and between each change
     * @param incrementSize the increment size
     */
    public static void fadeIn(final JDialog dialog, int delay, final float incrementSize) {
        final Timer timer = new Timer(delay, null);
        timer.setRepeats(true);
        timer.addActionListener(new ActionListener() {
            private float opacity = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                opacity += incrementSize;
                dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
                if (opacity >= 1) {
                    timer.stop();
                }
            }
        });

        dialog.setOpacity(0); // requires java 1.7
        timer.start();
        dialog.setVisible(true);
    }
}

Related

  1. fadeIn(final JDialog dialog)
  2. fadeInAndOut(final JDialog dialog)