ybt195
ybt195

Reputation: 115

Fading Jpanels in and out in a JFrame

I would like to make a game with multiple JPanels in a single global JFrame. I would like to switch these JPanels but with a nice animation like fade in and fade out. How would you be able to fade a JPanel out, switch the JPanel to a new one, and fade the new in (preferably without an external library)?

Thanks a lot

Upvotes: 2

Views: 7456

Answers (4)

user6549728
user6549728

Reputation:

I suggest creating a dummy javafx node, setting an animation for it, and binding the opacity property of the dummy node with your swing item. it would be something like this

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.util.Duration;    
import javax.swing.*;

class transitions
{
    Button fade_reference = new Button();
    transitions.fade fade_transistion;
    JFrame frame = new JFrame();
    transitions()
    {
        fade_transistion = new fade(fade_reference, 250, 0.2, 0.9);
        fade_reference.opacityProperty().addListener(observable-> {
            frame.setOpacity((float)fade_reference.getOpacity());
        });
    }

    class fade
    {
        private final Timeline show_timeline, hide_timeline;
        Node node;
        double min_opacity;
        double max_opacity;
        int annimation_duration;

        fade(Node node, int annimation_duration, double min_opacity, double max_opacity)
        {
            this.min_opacity = min_opacity;
            this.max_opacity = max_opacity;
            this.node = node;
            this.annimation_duration = annimation_duration;
            show_timeline = setupShowTransition();
            hide_timeline = setupDismissTransition();

        }

        private Timeline setupShowTransition()
        {
            return create_timeline(max_opacity);
        }

        private Timeline setupDismissTransition()
        {
            return create_timeline(min_opacity);
        }

        private Timeline create_timeline(double target_opacity)
        {
            Timeline tl = new Timeline();

            KeyValue kv1 = new KeyValue(node.opacityProperty(), node.getOpacity());
            KeyFrame kf1 = new KeyFrame(Duration.ZERO, kv1);

            KeyValue kv2 = new KeyValue(node.opacityProperty(), target_opacity);
            KeyFrame kf2 = new KeyFrame(Duration.millis(annimation_duration), kv2);

            tl.getKeyFrames().addAll(kf1, kf2);
            return tl;
        }

        void fade_up()
        {
            show_timeline.play();
        }

        void fade_down()
        {
            hide_timeline.play();
        }
    }
}

Upvotes: 0

trashgod
trashgod

Reputation: 205885

For reference, FlashTest shows how to fade by changing a color's saturation, while AlphaTest shows how to fade by changing a color's alpha.

Upvotes: 1

Houtman
Houtman

Reputation: 2879

Maybe http://filthyrichclients.org/
has some nice ideas and examples(online) to get started. It contains many swing animation details.

Upvotes: 4

user489041
user489041

Reputation: 28312

Could possibly do something like this on the panel and components to make them fade out:


Color bgColor = getBackground();

for(int alpha = bgColor.getAlpha(); alpha > = 0; alpha--)
{
    setBackground(new Color(
    bgColor.getRed(),
    bgColor.getGreen(),
    bgColor.getBlue(),
    alpha));
}

You will probably need to throw a Thread.sleep() in there.

Also have a look at:

com.sun.awt.AWTUtilities.setWindowOpacity(this,opacity );

But note that this does the entire application

Upvotes: 2

Related Questions