0

Good morning, I have a problem. I need to manipulate a variable that is modificated in a thread.

new Timer(5000, new ActionListener() 
{
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        color=colorRandom();
    }
}).start();

Where: color is a global variable that is set on white, colorRandom() is a method that generates and returns a random Color (black, white or red).

Finally I tried to check if the ellipse's color (another object) is the same of rectangle's. So I did this:

public boolean checkColor()
{
    return ellipseColor.equals(rect.color);
}

This does not work. It doesn't return me the right value. Even if the two object's have the same color it says that the colors are different. Also I tried this after setting the ellipse's and rectangle's color both to white

public boolean checkColor()
{
    return ellipseColor.equals(rect.color);
}

This returns true! so the method checkColor appears to be OK. The problem, in the first case, is that the variable is not correctly manipulated. Any suggestions? I tried also with the locks because a friend told me to try it but it still does not function.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • Damn, I wasn't aware of the `euqals` method, what does it do? – radoh Feb 24 '16 at 19:31
  • it compares two colors!! – VolfGING Feb 24 '16 at 19:32
  • on the primitives variables you can use == but on the objects you have to use . equals – VolfGING Feb 24 '16 at 19:33
  • @radoh is making fun of it. Check the spelling. – Hugo Alonso Feb 24 '16 at 19:33
  • 1
    Just a bit of harmless fun :). But why did you post the `checkColor` snippet 2 times? Didn't you mean to post some other code? – radoh Feb 24 '16 at 19:36
  • 1
    Try making the `color` variable `volatile`. sometimes threads use their own cache instead of going to the `heap`, which could result in inconsistent values between threads. – Daniel Feb 24 '16 at 19:38
  • In addition, there is no guarantee that a variable set by one thread will be correctly observed by another thread even if that variable is global (you should understand that global variables are bad in almost every way that matters, but I digress). You will need to attend to safely publishing your data from the thread that modifies it. – scottb Feb 24 '16 at 22:42

1 Answers1

0

The problem lies with the method you use to compare the colors. For a correct way to compare colors see answer here how i compare colors in java.

Using ColorA == ColorB will only work some of the time. Specifically, if both references refer to the Color.WHITE object. This will not work if one is Color.WHITE and the other is new Color(255, 255, 255) because they are different objects.

Community
  • 1
  • 1
Orvil Nordström
  • 325
  • 6
  • 18
  • They are the same type.. both Color!! I tried to do, for example, ' if(ellipseColor.getRGB()==rectColor.getRGB()) ' but it is like saying 'ellipseColor.equals()'... I don't really figure out how to solve this.. I am stack since 4 days :/ – VolfGING Feb 26 '16 at 08:16