Java tutorial
//package com.java2s; //License from project: Open Source License import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JDialog; import javax.swing.Timer; public class Main { /** * 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); } }