如何将淡入/淡出效果添加到JLabel [英] How to add fade/fade out effects to a JLabel

查看:148
本文介绍了如何将淡入/淡出效果添加到JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我正在尝试创建一个Java游戏,需要在我的标签上添加一些效果。我有以下问题

Hello I am trying to create a Java game and need to add some effects to my label. I have the following questions


  1. 如何为我的标签添加淡入/淡出效果。

  2. 我有一个JLabel,但我需要一个形状,可能是矩形或云形。我怎么能完成这件事?


推荐答案

你可以使用 AlphaComposite 更改组件的opactiy级别...

You can use a AlphaComposite to change the opactiy level of a component...

现在,通过巧妙地使用计时器,您可以制作标签淡入淡出或简单地控制不透明度...

Now, with a little clever use of a timer, you can make the label fade in and out or simply control the opacity as you please...

public class TestFadeLabel {

    public static void main(String[] args) {
        new TestFadeLabel();
    }

    public TestFadeLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        private float direction = -0.05f;
        private FadeLabel label = new FadeLabel();

        public MainPane() {
            setLayout(new BorderLayout());
            JLabel background = new JLabel();
            background.setLayout(new GridBagLayout());
            try {
                background.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Storm.jpg"))));
            } catch (Exception e) {
                e.printStackTrace();
            }
            add(background);

            label = new FadeLabel();
            background.add(label);

            Timer timer = new Timer(100, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    float alpha = label.getAlpha();
                    alpha += direction;
                    if (alpha < 0) {
                        alpha = 0;
                        direction = 0.05f;
                    } else if (alpha > 1) {
                        alpha = 1;
                        direction = -0.05f;
                    }
                    label.setAlpha(alpha);
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }
    }

    public class FadeLabel extends JLabel {

        private float alpha;
        private BufferedImage background;

        public FadeLabel() {
            try {
                background = ImageIO.read(getClass().getResource("/Cloud.png"));
            } catch (Exception e) {
            }
            setText("Hide and go seek");
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
            setAlpha(1f);
        }

        public void setAlpha(float value) {
            if (alpha != value) {
                float old = alpha;
                alpha = value;
                firePropertyChange("alpha", old, alpha);
                repaint();
            }
        }

        public float getAlpha() {
            return alpha;
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        public void paint(Graphics g) {
            // This is one of the few times I would directly override paint
            // This makes sure that the entire paint chain is now using
            // the alpha composite, including borders and child components
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha()));
            super.paint(g2d);
            g2d.dispose();
        }

        @Override
        protected void paintComponent(Graphics g) {
            // This is one of the few times that doing this before the super call
            // will work...
            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g.drawImage(background, x, y, this);
            }
            super.paintComponent(g);
        }
    }
}

这篇关于如何将淡入/淡出效果添加到JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆