在 JFrame 中淡入和淡出 Jpanels

发布于 2024-11-06 20:47:26 字数 140 浏览 1 评论 0原文

我想在一个全局 JFrame 中制作一个包含多个 JPanel 的游戏。我想切换这些 JPanel,但要有漂亮的动画,例如淡入和淡出。如何淡出 JPanel、将 JPanel 切换到新 JPanel,然后淡入新 JPanel(最好没有外部库)?

多谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兲鉂ぱ嘚淚 2024-11-13 20:47:26

也许http://filthyrichclients.org/
有一些不错的想法和示例(在线)可以开始使用。它包含许多摆动动画细节。

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

ら栖息 2024-11-13 20:47:26

可以在面板和组件上执行类似的操作以使它们淡出:


Color bgColor = getBackground();

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

您可能需要在其中抛出 Thread.sleep() 。

另请参阅:

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

但请注意,这会影响整个应用程序

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

仅冇旳回忆 2024-11-13 20:47:26

作为参考,FlashTest< /code>展示了如何通过更改颜色的饱和度来淡入淡出,而 AlphaTest 展示了如何通过更改颜色的 Alpha 来淡入淡出。

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.

╰沐子 2024-11-13 20:47:26

我建议创建一个虚拟 javafx 节点,为其设置动画,并将虚拟节点的不透明度属性与您的 swing 项目绑定。
会是这样的

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();
        }
    }
}

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文