为什么此计时器中的Alpha在此Java Swing面板中位于自身之上? [英] Why does the alpha in this timer draw on top of itself in this Java Swing Panel?

查看:103
本文介绍了为什么此计时器中的Alpha在此Java Swing面板中位于自身之上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class Main {
        
        
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setLayout(new FlowLayout());
            frame.setSize(new Dimension(100, 100));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            TestPanel panel = new TestPanel();
            panel.setPreferredSize(new Dimension(50,50));
            frame.add(panel);
            frame.setVisible(true);
        }
        
        static class TestPanel extends JPanel implements ActionListener{
            
            private static final long serialVersionUID = 8518959671689548069L;
            
            public TestPanel() {
                super();
                Timer t = new Timer(1000, this);
                t.setRepeats(true);
                t.start();
            }
            
            int opacity = 10;
            @Override
            public void actionPerformed(ActionEvent e) {
                if(opacity >= 250) {
                    opacity = 0;
                }
                else {
                    this.setBackground(new Color(255, 212, 100, opacity));
                    this.repaint();
                    opacity+=10;
                    System.out.println("opacity is " + opacity);
                }
            }
            
        }
    }

alpha更改的速度快于应有的速度.在达到某个点之后,不透明度下降,而控制台中打印的不透明度小于250.它,使Alpha正确.

The rate the alpha changes is faster than it should be. After it reaches a certain point, the opacity drops, while the the opacity printed in the console is less than 250. Resizing the window "resets" it, making the alpha correct.

我如何使其正确绘制Alpha?

How do I make it actually draw the alpha correctly?

推荐答案

this.setBackground(new Color(255, 212, 100, opacity));

Swing不支持透明背景.

Swing does not support transparent backgrounds.

Swing希望组件为:

Swing expects a component to be either:

  1. opaque-表示组件将在进行自定义绘画之前首先使用不透明的颜色重新绘制整个背景,或者
  2. 完全透明-在这种情况下,Swing将在进行自定义绘制之前首先绘制第一个不透明父级组件的背景.

setOpaque(...)方法用于控制组件的不透明属性.

The setOpaque(...) method is used to control the opaque property of a component.

在任何一种情况下,这都可以确保删除所有绘画瑕疵,并且可以正确完成自定义绘画.

In either case this makes sure any painting artifacts are removed and custom painting can be done properly.

如果要使用透明度,则需要自己进行自定义绘制以确保清除背景.

If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.

面板的自定义绘画为:

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false); // background of parent will be painted first

使用透明性的每个组件都需要类似的代码.

Similar code would be required for every component that uses transparency.

或者,您可以查看具有透明度的背景用于可在将为您完成上述工作的任何组件上使用的自定义类.

Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.

这篇关于为什么此计时器中的Alpha在此Java Swing面板中位于自身之上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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