Saturday, November 22, 2014

Unwanted changes in objects




I can't think of a good title because I barely understand my problem myself but I do know that it's a problem. I can't come up with a simple scenario so I'll use my actual code to explain the problem, and it's going to be related to java.awt.Color, but my problem is general, not awt dependent. Although I will shorten my code to show only what I think is relevant to the problem.






Java Code:






public class GameColor {
public abstract class Effect {
public int strength;
public Effect(int strength) {
this.strength = strength;
}

public abstract Color get(Color color);
}

public class SetHue extends Effect {
public SetHue(int strength) { super(strength); }
public Color get(Color color) {
int h = strength;
int s = getS(color);
int b = getB(color);

int a = color.getAlpha();
return getColor(h, s, b, a);
}
}

public static final GameColor RED = new GameColor(255, 0, 0);

protected Color color;

public GameColor(int r, int g, int b) { color = new Color(r, g, b); }

public void setHue(int hue) { color = new SetHue(hue).get(color); }

private static int getH(Color color) { return getHSB(color)[0]; }
private static int getS(Color color) { return getHSB(color)[1]; }
private static int getB(Color color) { return getHSB(color)[2]; }

private static int[] getHSB(Color color) {
float floatHSB[] = new float[3];
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
Color.RGBtoHSB(red, green, blue, floatHSB);
int hue = Math.round(floatHSB[0] * 360.0F);
int saturation = Math.round(floatHSB[1] * 255.0F);
int brightness = Math.round(floatHSB[2] * 255.0F);
return new int[] {hue, saturation, brightness};
}

public static Color getColor(int H, int S, int B, int A) {
float hue = (float) H / 360.0F;
float saturation = (float) S / 255.0F;
float brightness = (float) B / 255.0F;
Color color = new Color(Color.HSBtoRGB(hue, saturation, brightness));

int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
return new Color(red, green, blue, A);
}




I have my reasons for having a SetHue inner class and a setHue method that practically do the same thing. The inner abstract class Effect and inner class SetHue was static but I changed it when I testing what could cause this problem, later I will need them to be static, but since they don't affect the problem I don't think it matters if they're static or not.

So down to the problem, I create three new GameColor instances which all are 255, 0, 0, basically red. Then I do setHue(0) method on the first one, setHue(20) on the second one, and setHue(40) on the last one. And for some reason all of them have a hue of 40, whereas the last one should've had this, so basically the last time I call setHue on the instances of GameColor, changes the hue of all of these colours to that hue. I want it to be individually affected, but all of them are affected. This is where I apply these changes:






Java Code:






colo1.setHue(0);
colo2.setHue(20);
colo3.setHue(40);




And how I initialise the colors:



Java Code:






public GameColor colo1 = GameColor.RED;
public GameColor colo2 = GameColor.RED;
public GameColor colo3 = GameColor.RED;







No comments:

Post a Comment