如何淡化JLabels?

时间:2019-03-12 17:16:44

标签: java transparency jlabel

我在制作自己的游戏时遇到麻烦。我正在尝试使用Timer淡入JLabel。到目前为止,我已经能够将JLabel设置为透明,并且已经能够使计时器工作。但是以某种方式,我无法将它们结合在一起。我可以将JLabel的背景设置为

l.setBackground(new Color(0, 0, 0, 200));

(当然,要让JLabel更改其颜色) 而且有效。 JLabel是透明的。 但是我无法将其放入计时器以使颜色褪色:

public static void fadeLabel(JLabel lab, int steps, int target) {
    class fade extends Thread {
        public void run() {
            Timer t = new Timer(10, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(lab.getBackground().getAlpha());
                    lab.setBackground(new 
                    Color(lab.getBackground().getRed(), 
                    lab.getBackground().getGreen(), 
                    lab.getBackground().getBlue(), 
                    lab.getBackground().getAlpha() + steps));
                    if (lab.getBackground().getAlpha() == target) {
                        ((Timer) e.getSource()).stop();
                    }
                }
            });
            t.start();
        }
    }
    new Thread(new fade()).start();
}

相反,它只是从黑色变为透明。两者之间没有。它还会引发错误java.lang.IllegalArgumentException: Color parameter outside of expected range: Alpha。但是,我将步长设置为-5(在这种情况下,它逐渐消失的速度,因为它是负数),如果您还记得,我将alpha设置为可以被5整除的东西,有一个到达目标的检查器,以便在到达目标时停止计时器。我做错了什么吗? JLabel的背景更新是否太快而无法显示?任何帮助都会很棒。

编辑: 我已经修复了java.lang.IllegalArgumentException: Color parameter outside of expected range: Alpha。但是关于JLabel不褪色的另一个问题仍然存在,我不知道如何解决。

2 个答案:

答案 0 :(得分:0)

我没有直接回答您的问题。一个写了一些代码,看看:D

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyWindow extends JFrame {

    public MyWindow() {
        initComponents();
    }

    private void initComponents() {

        setTitle( "Fading labels :)" );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        setSize( 400, 400 );
        setLocationRelativeTo( null );
        setLayout( new FlowLayout() );

        for ( int i = 0; i < 550; i++ ) {
            JLabel lab = new JLabel( "x" );
            add( lab );
            fadeLabel( lab, 10, i / 10, 0 );
        }

    }

    private void fadeLabel( JLabel label, int steps, int skipSteps, int targetAlpha ) {
        new LabelFader( label, steps, skipSteps, targetAlpha ).start();
    }

    private class LabelFader {

        JLabel label;
        int steps;
        int skipSteps;
        int targetAlpha;

        LabelFader( JLabel label, int steps, int skipSteps, int targetAlpha ) {
            this.label = label;
            this.steps = steps;
            this.skipSteps = skipSteps;
            this.targetAlpha = targetAlpha;
        }

        void start() {

            Color startColor = label.getForeground();
            int startAlpha = startColor.getAlpha();
            int deltaAlpha = startAlpha - targetAlpha;

            if ( deltaAlpha > 0 ) {

                int alphaStep = deltaAlpha / steps;

                new Thread( new Runnable() {
                    @Override
                    public void run() {

                        int totalIterations = steps + skipSteps;
                        int currIteration = 1;

                        for ( int i = 0; i < totalIterations; i++ ) {

                            if ( skipSteps == 0 ) {

                                int newAlpha = startAlpha - alphaStep * currIteration++;
                                newAlpha = newAlpha < 0 ? 0 : newAlpha;

                                Color newColor = new Color( 
                                        startColor.getRed(), 
                                        startColor.getGreen(), 
                                        startColor.getGreen(),
                                        newAlpha );

                                label.setForeground( newColor );

                            } else {
                                skipSteps--;
                            }

                            try {
                                Thread.sleep( 100 );
                            } catch ( InterruptedException exc ) {
                                exc.printStackTrace();
                            }

                        }

                        Color newColor = new Color( 
                                startColor.getRed(), 
                                startColor.getGreen(), 
                                startColor.getGreen(),
                                targetAlpha );

                        label.setForeground( newColor );

                    }
                }).start();

            }

        }

    }

    public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
            public void run() {
                new MyWindow().setVisible( true );
            }
        });
    }

}

答案 1 :(得分:0)

第二编辑:

这是一些有效的示例代码。它将抛出您已经修复的Exception,但这仅是为了演示:)。

label.setOpaque及其背景,告诉自己发生了什么。如果需要绘制标签背景,则将该标志设置为true。

如果不需要绘制其背景,请不要修改标签的背景,也不要将其设置为不透明,并且修改前景色应该足够了! :)

package main;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {

    JLabel label;
    JButton btn;

    public Main() {
        setSize(500,500);
        setLayout(new FlowLayout());
        label = new JLabel("My super awesome label");
        //label.setOpaque(true); //Play with these lines and see what happens
        //label.setBackground(Color.BLUE); //Play with these lines and see what happens

        btn = new JButton("Click me");
        btn.setSize(200,200);
        btn.addActionListener(
                (ActionEvent evt) -> {
                    Timer t = new Timer(100,
                            (ActionEvent event) -> {
                                Color foreground = label.getForeground();
                                Color background = label.getBackground();

                                foreground = decreaseAlpha(foreground);
                                background = decreaseAlpha(background);

                                label.setForeground(foreground);
                                label.setBackground(background);
                            }
                        );

                    t.start();
                }
        );

        this.add(label);
        this.add(btn);
    }

    public static void main(String[] args) {
        (new Main()).setVisible(true);
    }

    public Color decreaseAlpha(Color original) {
        return new Color(original.getRed(), original.getGreen(), original.getBlue(), original.getAlpha() - 10);
    }
}

编辑:

您可能正在更改标签的背景而不是其前景...

Timer t = new Timer(10, (ActionEvent evt)-> {
    l.setForeground(new 
        Color(l.getForeground().getRed(), 
        l.getForeground().getGreen(), 
        l.getForeground().getBlue(), 
        l.getForeground().getAlpha() - 1)
    );
    l.invalidate();
});
t.start();

成为l标签

也不要这样做:

new Thread(new fade()).start();

您的fade类已经是Thread,因为它正在扩展它。请改用(new fade()).start();