I have a problem now - when I call frame.setState(Frame.ICONIFIED)
with my custom button (I'm not using default JFrame minimize button - JFrame set to setUndecorated(true)
), the JFrame just goes to Taskbar without any animation. In normal situation it should gradually go to Taskbar minimizing itself. But if I press iconfied JFrame on Taskbar, it restores with animation to normal size. The situation is on Windows XP, not tested on other systems, but I suppose it would behave in the same way.
Since you want it to be platform independent, my other answer will not work for you. Also, each platform will react differently when you minimize or hide a window. You mentioned it would be nice to see a Java code fragment that would give you a consistent animation. Here's some code for you.
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class FadeUtilityClass
{
private static final int TIME = 200;
private static final int MILLIS_PER_FRAME = 33;
private static final float DELTA = MILLIS_PER_FRAME / (float)TIME; //how much the opacity will change on each tick
/**
* @param frame the frame to fade in or out
* @param in true if you are fading in, false if you're fading out
*/
public static void fade(final JFrame frame, final boolean in)
{
frame.setOpacity(in ? 0f : 1f); //if we're fading in, make sure our opacity is 0, and 1 if we're fading out
if (in) //set the state back to normal because we might have been minimized
frame.setState(JFrame.NORMAL);
final Timer timer = new Timer();
TimerTask timerTask = new TimerTask()
{
float opacity = in ? 0f : 1f;
float delta = in ? DELTA : -DELTA;
@Override
public void run()
{
opacity += delta; //tweak the opacity
if (opacity < 0) //we're invisible now
{
frame.setState(JFrame.ICONIFIED); //hide frame
frame.setOpacity(1f); //then make it opaque again, so it'll reappear properly if they click the taskbar
timer.cancel(); //stop the timer
}
else if (opacity > 1) //we're fully visible now
{
frame.setOpacity(1f); //make the opacity an even 1.0f
timer.cancel(); //stop the timer
}
else
frame.setOpacity(opacity);
}
};
timer.scheduleAtFixedRate(timerTask, MILLIS_PER_FRAME, MILLIS_PER_FRAME);
}
}
It's a utility class that will make your undecorated frame fade in or out. Since the location of the taskbar and minimized window changes based on the platform and you would need to use platform specific api's to find that, I just made the animation fade the window out without shrinking it down to where the taskbar might be.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With