简体   繁体   中英

why is the Color of my object changing randomly?

I have two copies of an object in my container, and they are synchronized (not in the Java sense, only in that whatever I do to one, I also do to the other). They both trace out a pattern drawn using the arrow keys.
The problem is that the Color randomly reverts to black occasionally, unpredictably (and not at the same time for both objects). Here is what I believe to be all the relevant code; surely all the times setColor is called:

public class UserRavelDialog extends Component implements Runnable {
...
in init():
colors = new Color[] { 
            new Color (245, 240, 80),   //set colors for the elements
            new Color (100, 50, 50),    
            new Color (255, 0, 0),      
            new Color (255, 0, 200),    
            new Color (0, 0, 200)};     

    bb.setColor(colors[0]);  //bb is the backbuffer graphics object

public void render(){    //this draws the current color around a black cursor, or white if inactive
    Color temp = bb.getColor();

    if(temp.equals(Color.black))
        System.out.print("!");

    if (!isActive)
        bb.setColor(Color.white);
    bb.fillRect((int)p.x - 1, (int)p.y - 1, 3, 3); //p is a Point2D.Double for the cursor position
    bb.setColor(Color.black);
    bb.fillRect((int)p.x, (int)p.y, 1, 1);
    bb.setColor(temp);

    update(getGraphics());
}

private void toggleColour(int arg) {
    if (arg < colors.length)
        bb.setColor(colors[arg]);
}

public void keyPressed(KeyEvent e){
for (int i = 0 ; i < colors.length ; i++){
            if (e.getKeyCode() == keys[i+9])
                toggleColour(i);
        }
}

So, setColor is called in init, when I create the possible color options, and in toggleColour when the user presses a key to change the color, and it is used in render, but always re-set to the current color.

The weird thing is the if (temp.equals(Color.black)) condition gets entered when the flip happens, so it seems like the bb.setColor(temp) just didn't happen on the previous render...
Why does this happen and how can I fix it?

This solution solves the problem, even though I don't understand why the original program doesn't work.
If you create a (global) Color variable, and set this variable in init and toggleColour, you can use it in render and you don't need to worry about the temp variable (the only instance of the color you want to get back) somehow being lost. So:

in init():
currColor = colors[0];
bb.setColor(currColor);

public void render(){
    if (!iActive)
        bb.setColor(Color.white);
    bb.fillRect((int)p.x - 1, (int)p.y - 1, 3, 3);
    bb.setColor(Color.black);
    bb.fillRect((int)p.x, (int)p.y, 1, 1);
    bb.setColor(currColor);

    update(getGraphics());
}
private void toggleColour(int arg) {
    if (arg < colors.length) {
        currColor = colors[arg];
        bb.setColor(currColor);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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