简体   繁体   English

我如何淡化JPanel和儿童?

[英]How can I fade in a JPanel & children?

I know that I can fade in a panel, by adding the alpha value to the background color & a timer. 我知道我可以通过将alpha值添加到背景颜色和计时器来淡入面板。 But how can I fade in a panel with child components (like a JLabel )? 但是,我如何淡化具有子组件的面板(如JLabel )?

EDIT 编辑

_fadeTimer = new Timer(40, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (_alpha == 255) {
                _fadeTimer.stop();
            } else {
                pnl_hint.setBackground(new Color(
                        bgColor.getRed(),
                        bgColor.getGreen(),
                        bgColor.getBlue(),
                        (_alpha += 1)));

                pnl_hint.updateUI();
            }
        }
    });
    _fadeTimer.start();

Another option would be to capture a screenshot of your panel and then let it paint itself with an increasing alpha composite. 另一个选择是捕获面板的屏幕截图,然后让它自己绘制增加的alpha合成。

Here is a small demo of that (although I am not sure this is really clean): 这是一个小的演示(虽然我不确定这真的很干净):

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class TestFading {

    private static class FadingPanel extends JPanel {
        private BufferedImage buffer;
        private boolean isFading = false;
        private long start;
        private float alpha = 1.0f;

        @Override
        public void paint(Graphics g) {
            if (isFading) {// During fading, we prevent child components from being painted
                g.clearRect(0, 0, getWidth(), getHeight());
                ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                g.drawImage(buffer, 0, 0, this);// We only draw an image of them with an alpha
            } else {
                super.paint(g);
            }
        }

        public void fade(int time) {
            start = System.currentTimeMillis();
            buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
            this.print(buffer.getGraphics());// Draw the current components on the buffer
            isFading = true;
            final int timeInMillis = time * 1000;
            final Timer t = new Timer(50, null);
            t.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    long elapsed = System.currentTimeMillis() - start;
                    if (elapsed > timeInMillis) {
                        start = 0;
                        isFading = false;
                        buffer = null;
                        repaint();
                        t.stop();
                    } else {
                        alpha = 1.0f - (float) elapsed / timeInMillis;
                        repaint();
                    }
                }
            });
            t.start();
        }
    }

    protected void initUI() throws MalformedURLException {
        JFrame frame = new JFrame(TestFading.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final FadingPanel panel = new FadingPanel();
        JTextField textfield = new JTextField(60);
        JLabel image = new JLabel(new ImageIcon(new URL("http://helios.gsfc.nasa.gov/image_mag_stamp.jpg")));
        JButton button = new JButton("Fade");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // I use an invoke later to allow the button to release itself
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        panel.fade(5);// Fade the panel in 5s.
                    }
                });
            }
        });
        panel.add(textfield);
        panel.add(image);
        panel.add(button);
        frame.add(panel);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new TestFading().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

Untested pseudo-code. 未经测试的伪代码。

public void fade(Container c) {
    Component[] children = c.getComponents();
    for (Component component : components)  {
        // do fade thing with component
        if (component instanceOf Container) {
            fade( (Container)component );
        }
    }
}

Instead of setting background override paintChildren method. 而不是设置后台覆盖paintChildren方法。 Call super.paintChildren() and then fill component's rect with semi transparent color. 调用super.paintChildren()然后用半透明颜色填充组件的rect。

I added a JLabel with an image and set the JPanel opaque to false! 我添加了一个带有图像的JLabel,并将JPanel设置为不透明!

In the overriden paintComponent method i set the AlphaComposite with my alpha before the super.paintComponent() call. 在重写的paintComponent方法中,我在super.paintComponent()调用之前使用我的alpha设置AlphaComposite。

How do I fade an image in swing? 如何在摇摆中淡化图像?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM