简体   繁体   English

如何在Java Swing应用程序中修改颜色强度

[英]How to modify color intensity in a java swing application

I am using java swing to create an interface where the user draws a couple of points. 我正在使用java swing创建一个界面,用户可以在该界面上绘制几个点。 What I want to do is after those points are drawn to change automatically the intensity of the color from really bright to dark till the point fades. 我要做的是在绘制这些点后自动将颜色的强度从真正的亮变为暗,直到该点消失。 Does anyone know any tutorials on how to change the color intensity because I cannot find something to help me. 没有人知道任何有关如何更改颜色强度的教程,因为我找不到任何可以帮助我的东西。

EDIT: Thank you for your answers they helped me understand better how to work with Color class. 编辑:谢谢您的回答,他们帮助我更好地了解了如何使用Color类。 I fixed it so I am uploading the thread part to help if anyone anyone else needs to do something similar... I am working on a black background so I darken the colors instead of lightening them. 我已修复它,因此我上载了线程部分,以帮助其他人是否需要做类似的事情...我在黑色背景上工作,所以我加深了颜色而不是使它们变浅。

public class MyThread extends Thread {

    private Canvas canvas;
    private int sleepingTime = 5000;
    private Color color;
    private int red, green, blue, alpha;

    public MyThread(Canvas canvas) {
        super();
        this.canvas = canvas;
        setDaemon(true);

    }

    public void run(){
        while (true){
            try {
                System.out.println("going to sleep...");
                Thread.sleep(sleepingTime);

            } catch (InterruptedException e) {
                System.out.println("sleep interrupted..");
                return;
            }
            System.out.println("woke up!");
                int size = canvas.points_list.size();
                int i =0;
                while (size > 0) {
                    color = canvas.points_list.get(i).getForeground();

                    red = (int) Math.round(Math.max(0, color.getRed() - 255 * 0.25f));
                    green = (int) Math.round(Math.max(0, color.getGreen() - 255 * 0.25f));
                    blue = (int) Math.round(Math.max(0, color.getBlue() - 255 * 0.25f));

                    alpha = color.getAlpha();

                    canvas.points_list.get(i).setForeground(new Color(red, green, blue, alpha));
                    size--; 
                    i++;
                }

                canvas.repaint();
        }
    }
}

您需要使用RGB形式,通过添加蓝色和绿色并使红色值保持不变,使它成为从明亮到黑暗的渐变,在for循环中连续进行此操作,最后将其设置为背景色,使其不可见。

Take a look at the HSB color model. 看一下HSB颜色模型。 It allows you to specify a "intensity" (saturation and brightness). 它允许您指定“强度”(饱和度和亮度)。

Example (red background becoming black - alternatively, modify the "s" parameter for fading the color to white): 示例(红色背景变成黑色-或者,修改“ s”参数以将颜色淡化为白色):

final Frame frame = new Frame();

Timer timer = new Timer(500, new ActionListener() {

    float b = 1.0f;

    @Override
    public void actionPerformed(ActionEvent e) {
        int color = Color.HSBtoRGB(0, 1, b);
        frame.setBackground(new Color(color));

        b -= 0.05f;
    }
});
timer.start();

frame.setSize(200, 200);
frame.setVisible(true);

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

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