Results 1 to 5 of 5
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default 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;
    Regards
    augustas656

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Unwanted changes in objects

    all of them have a hue of 40,
    Often that has something to do with the use of static variables.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Unwanted changes in objects

    Yeah that's what I'm thinking but I can't find anything in my project, that's why I tried making the abstract Effect class and inner class non-static as you see it now but it still doesn't work...

    EDIT: Unless could it be that the public static final GameColor RED = new GameColor(255, 0, 0) that I start off with changes? It can't be, because it's final, but Idk..

    EDIT 2: That was actually the cause... But it's final? Is there any way I change have public static final GameColor constants that can't change data?

    EDIT 3: Making the inner class Effect and inner classes for each effect static also affects this...
    Last edited by augustas656; 11-22-2014 at 02:22 PM.
    Regards
    augustas656

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Unwanted changes in objects

    any way I change have public static final GameColor constants that can't change data?
    final means the variable's value can not be changed. It does not stop the object that it is referring to from being changed.

    If that was possible, the calls to setHue() wouldn't work.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Unwanted changes in objects

    You see my class SetHue, I want to make it static, but if I do it's strength variable will also be static. I want to make my SetHue inner class static because I want to be able to store Effect child instances in an ArrayList<Effect>. But as I said if I do make my inner classes that extend effect static then they're not individual anymore, know any way how to solve this, a workaround maybe?

    EDIT: The reason I'm not making regular classes outside of GameColor class is to keep it tidy and I have like 10 effects, it would be not nice to have so many classes.
    Last edited by augustas656; 11-22-2014 at 02:34 PM.
    Regards
    augustas656

Similar Threads

  1. how to remove unwanted tags from xml
    By abhi140291 in forum New To Java
    Replies: 4
    Last Post: 11-26-2013, 10:07 AM
  2. Unwanted Null output.
    By steinarsen in forum New To Java
    Replies: 8
    Last Post: 11-08-2012, 10:01 AM
  3. Unwanted image fragment
    By polmos in forum New To Java
    Replies: 6
    Last Post: 09-12-2012, 03:00 PM
  4. unwanted bug. JMenu
    By ocean in forum New To Java
    Replies: 7
    Last Post: 11-20-2009, 09:29 PM
  5. Repositioning An unwanted JInternalFrame
    By marco.c84 in forum AWT / Swing
    Replies: 8
    Last Post: 03-18-2009, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •