简体   繁体   English

在Java中更改RGB颜色

[英]change RGB color in Java

I have created a program in Java ( for exercise purposes) and I have a star where the color must go from lightred to dark red by scrolling the mouse.. ( using the MouseWheelListener ) and vica versa Everything works well with other kind of stars but only this one doesn't work.. instead of confusing you guys, i'm gonna show you some code! 我已经用Java创建了一个程序(出于锻炼目的),并且有一个星星,其中的颜色必须通过滚动鼠标从浅红色变为深红色。(使用MouseWheelListener),反之亦然。一切都可以与其他类型的星星配合使用,但是只有这个不起作用..而不是让你们困惑,我要向您展示一些代码!

here is the red star class 这是红星班

    public class StarRed extends Star {

    protected int r = 221;
    protected Color rood = new Color(r, 0, 0);

    public StarRed(int radius, int x, int y) {
        super(radius, x, y);


        this.color = rood;

        System.out.println(r);
    }

}

as you see I have tried to use the R variable to change the color.. 如您所见,我尝试使用R变量来更改颜色。

in my controller I do this 在我的控制器中,我这样做

    @Override
public void mouseWheelMoved(MouseWheelEvent e) {

    for(StarRed s: rs) {
        s.r += e.getWheelRotation();
    }
    repaint();
}

but the color doesn't change, can anyone tell me what i'm doing wrong? 但是颜色没有改变,有人可以告诉我我做错了吗?

The Color is immutable class, changing value of r does not change the value of protected Color rood Color是不可变的类,更改r的值不会更改protected Color rood的值

So what you need to do is add new method in your star class where the value of rood ie color to use is changed based on given parameters. 因此,您需要做的是在star类中添加新方法,其中rood的值(即要使用的颜色)将根据给定的参数进行更改。

Assuming that you've implemented the mouseWheelMoved correctly, the issue lies when you are adding to the value r. 假设您已正确实现mouseWheelMoved,则当您添加到值r时,问题就出在这里。

Although rood is: 尽管十字架是:

protected Color rood = new Color(r, 0, 0);

When you change the value of r after you've created the color, it will not change the value of the red part of your Color rood . 在创建颜色之后更改r的值时,它不会更改Color rood红色部分的值。

So instead, you want to make a function that adds to the red value of your Color rood and then changes the Color itself. 因此,您想创建一个函数,将其添加到色标中的红色值,然后更改色本身。

When you pass a variable of type int (or any other primitive type) to a method or constructor, you pass the value of the variable (a copy if you prefer). 当您将int类型的变量(或任何其他原始类型)传递给方法或构造函数时,您将传递变量的 (如果需要,则传递一个副本)。 You don't pass a reference to its value. 您不会传递对其值的引用 So changing the value of the variable won't change anything to the Color you created with previously this variable. 因此,更改变量的值不会更改您之前使用此变量创建的颜色。

You need to mutate the color object (but this is impossible bacause Color is immutable), or replace the color object itself with another one. 您需要更改颜色对象(但这是不可能的,因为颜色是不可变的),或将颜色对象本身替换为另一颜色对象。

You need to instantiate a new color each time you move the wheel, like this: 每次移动轮子时,都需要实例化一种新颜色,如下所示:

for(StarRed s: rs) {
    s.r += e.getWheelRotation();
    s.rood = new Color(r, 0, 0);
    s.color = s.rood;
}

Currently you are only changing the field r , and not the Color rood that r was used to construct. 目前只更改领域r而不是颜色roodr用于构造。

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

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