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

    Default Most efficient way to store color effect methods to be used

    Hello,

    This isn't solely related to java.awt.Color, it's more of a question on how to store methods or redirections to use methods on any value passed in. To explain my question I'll describe a scenario:

    ClassA is an instanstiatable class
    ClassA has an instance of ClassB called cB
    ClassA has methods of affecting cB

    ClassD is also an instanstiatable class
    ClassD has an Array[x][y] of instances of ClassA

    I want to create a ClassE which I would initiate it with references to ClassA's methods or their actual methods and then I can use ClassE to easily transform ClassD's instances of ClassA by the same methods.

    What I'm actually doing:

    I made a class called GameColor, it has methods like changeHue, setRed, setHue, changeBrightness, setSaturation etc, these methods affect an instance of java.awt.Color in the GameColor class instances, so like they can change the hue or set it with changeHue or setHue etc...

    Now I want to do something like perform a single method on a whole BufferedImage, on each of it's pixels RGB. So, I'm thinking of creating a GameColorEffects class which I store references or actual methods of GameColor like changeHue, and then I use GameColorEffects somehow to change a whole BufferedImage more efficiently, what's the most efficient way I can store methods or their reference of GameColor and then later apply them on each pixel of a BufferedImage using GameColorEffects. So all I do is initiate GameColorEffects with methods or reference from GameColor, and then I can apply it to easily transform each pixel.

    Sorry, I can't explain too well, I hope everyone understands my point.

    Regards,
    augustas656

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Most efficient way to store color effect methods to be used

    So it sounds to me like you want an instance of GameColorEffects to contain references to instances of GameColor where the GameColor
    methods have been called with specific hue, staturation, brightness, etc values. Then you want to apply that GameColorEffects
    instance to a set of pixels to achieve all of those characteristics per pixel without have to make separate method calls from the GameColor
    class itself. Is that correct (or even close)?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Most efficient way to store color effect methods to be used

    I believe so... GameColorEffects being something that stores reference to methods from GameColor and can apply it to each pixel without having to call all those many methods. And yeah, what you said... What's the best way to do this? Not easiest, but most efficient...

    Regards
    Augustas

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

    Default Re: Most efficient way to store color effect methods to be used

    Okay, so I could just do it like this:

    Java Code:
    package net.unilite.game;
    
    import java.util.LinkedHashMap;
    import java.util.Map.Entry;
    
    public class GameColorEffects {
    	public LinkedHashMap<Integer, Integer> effects = new LinkedHashMap<Integer, Integer>();
    	
    	public static final int EFFECT_CHANGE_HUE = 0;
    	public static final int EFFECT_CHANGE_SATURATION = 1;
    	public static final int EFFECT_CHANGE_BRIGHTNESS = 2;
    	public static final int EFFECT_CHANGE_LIGHTNESS = 3;
    	public static final int EFFECT_CHANGE_DARKNESS = 4;
    	public static final int EFFECT_CHANGE_GRAYNESS = 5;
    	public static final int EFFECT_CHANGE_RED = 6;
    	public static final int EFFECT_CHANGE_GREEN = 7;
    	public static final int EFFECT_CHANGE_BLUE = 8;
    	public static final int EFFECT_SET_HUE = 9;
    	public static final int EFFECT_SET_SATURATION = 10;
    	public static final int EFFECT_SET_BRIGHTNESS = 11;
    	public static final int EFFECT_SET_LIGHTNESS = 12;
    	public static final int EFFECT_SET_DARKNESS = 13;
    	public static final int EFFECT_SET_GRAYNESS = 14;
    	public static final int EFFECT_SET_RED = 15;
    	public static final int EFFECT_SET_GREEN = 16;
    	public static final int EFFECT_SET_BLUE = 17;
    	
    	public void addEffect(int effect, int value) {
    		effects.put(effect, value);
    	}
    	
    	public GameColor applyEffects(GameColor color) {
    		GameColor newColor = new GameColor(color.get());
    		for (Entry<Integer, Integer> effect : effects.entrySet()) {
    			switch (effect.getKey()) {
    				case EFFECT_CHANGE_HUE: newColor.changeHue(effect.getValue());
    				case EFFECT_CHANGE_SATURATION: newColor.changeSaturation(effect.getValue());
    				case EFFECT_CHANGE_BRIGHTNESS: newColor.changeBrightness(effect.getValue());
    				case EFFECT_CHANGE_LIGHTNESS: newColor.changeLightness(effect.getValue());
    				case EFFECT_CHANGE_DARKNESS: newColor.changeDarkness(effect.getValue());
    				case EFFECT_CHANGE_GRAYNESS: newColor.changeGrayness(effect.getValue());
    				case EFFECT_CHANGE_RED: newColor.changeRed(effect.getValue());
    				case EFFECT_CHANGE_BLUE: newColor.changeRed(effect.getValue());
    				case EFFECT_CHANGE_GREEN: newColor.changeRed(effect.getValue());
    				case EFFECT_SET_HUE: newColor.setHue(effect.getValue());
    				case EFFECT_SET_SATURATION: newColor.setSaturation(effect.getValue());
    				case EFFECT_SET_BRIGHTNESS: newColor.setBrightness(effect.getValue());
    				case EFFECT_SET_LIGHTNESS: newColor.setLightness(effect.getValue());
    				case EFFECT_SET_DARKNESS: newColor.setDarkness(effect.getValue());
    				case EFFECT_SET_GRAYNESS: newColor.setGrayness(effect.getValue());
    				case EFFECT_SET_RED: newColor.setRed(effect.getValue());
    				case EFFECT_SET_BLUE: newColor.setRed(effect.getValue());
    				case EFFECT_SET_GREEN: newColor.setRed(effect.getValue());
    			}
    		} return newColor;
    	}
    }
    and

    Java Code:
    	public static BufferedImage applyEffects(BufferedImage image, GameColorEffects effects) {
    		BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    		Graphics2D gfx = newImage.createGraphics();
    		for (int x = 0; x < image.getWidth(); x++) {
    			for (int y = 0; y < image.getHeight(); y++) {
    				GameColor color = effects.applyEffects(new GameColor(image.getRGB(x, y)));
    				gfx.setColor(color.get());
    				gfx.drawLine(x, y, x, y);
    			}
    		} return newImage;
    	}
    And:

    Java Code:
    	@Override
    	public void initialise() {
    		new GameImage("Yellow Circle", "assets/images/Yellow Circle.png", true);
    		GameRegistry.registerSprite(new GameSprite("Yellow Circle", 50, 50), false);
    		GameStageRegistry.registerSprite("Yellow Circle", false);
    		
    		GameColor color = new GameColor(255, 0, 0);
    		
    		BufferedImage img = new BufferedImage(100, 100, GameSprite.defaultType);
    		Graphics2D gfx = img.createGraphics();
    		gfx.setColor(color.color);
    		gfx.fillRect(0, 0, img.getWidth(), img.getHeight());
    		gfx.dispose();
    		
    		GameColorEffects effects = new GameColorEffects();
    		effects.addEffect(0, 180);
    		GameSprite sprite = new GameSprite("Color", img, 150, 50);
    		sprite.applyEffects(effects);
    		
    		GameRegistry.registerSprite(sprite, false);
    		GameStageRegistry.registerSprite("Color", false);
    		
    	}
    But, is there any way that's more efficient than this?

    Note: I've tested this and it doesn't work, probably going to figure out why later, just if someone wants to help me with error, the error is:

    Java Code:
    Exception in thread "Thread-2" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red
    	at java.awt.Color.testColorValueRange(Unknown Source)
    	at java.awt.Color.<init>(Unknown Source)
    	at java.awt.Color.<init>(Unknown Source)
    	at net.unilite.game.GameColor.changeRed(GameColor.java:83)
    	at net.unilite.game.GameColorEffects.applyEffects(GameColorEffects.java:43)
    	at net.unilite.game.GameSprite.applyEffects(GameSprite.java:159)
    	at net.unilite.game.GameSprite.applyEffects(GameSprite.java:91)
    	at net.unilite.game.Game.initialise(Game.java:42)
    	at net.unilite.game.GameFrame.run(GameFrame.java:50)
    	at java.lang.Thread.run(Unknown Source)
    Regards
    Aqualight

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Most efficient way to store color effect methods to be used

    Without regard to what you want to do I will make the following observations:

    1. Final static constants are old school. They are still used in the JDK for backward compatibility
    but using enums is much more versatile.
    2. You need breaks after your case statements (assuming that a switch construct will still be used).

    In terms of what you want to do, I would think that if you could somehow combine the color modifications to an ultimate
    single resulting color, then all you have to do is a single change. But I am uncertain if that can be done in the general
    case or if it is even feasible. In general, I am unaware of any way to apply a specific operation to an image except pixel
    by pixel.

    Regards,
    Jim
    Last edited by jim829; 10-18-2014 at 09:50 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Most efficient way to store color effect methods to be used

    I was stuck on this for so long, but made such a stupid mistake, not using the break, eventhough the tutorial I used to revise in oracle docs showed me I had to use break, but I still didn't, OMG, thanks so much, it works perfectly, and thanks for the advise on the ENUM! :)

    Regards
    augustas656
    Last edited by augustas656; 10-18-2014 at 09:44 PM.

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

    Default Re: Most efficient way to store color effect methods to be used

    I never used enums before and it's one of those things I never learnt in tutorials, eventhough I had seen them before being used in like Colors, and Colors in different APIs etc. How would I make this into an enum?

    Regards
    augustas656

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Most efficient way to store color effect methods to be used

    Enums are much too broad for an answer here. Check them out in the tutorials in my signature. I also suggest you search the web
    for more in depth discussions on them. Once you try and experiment with them you can ask any specific questions here. Someone
    one be glad to assist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Most efficient way to store color effect methods to be used

    Yeah, I usually understand how different types work from examples, because I already know how interfaces and classes work, and the rest are similar in SOME ways.

    Regards
    augustas656

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Most efficient way to store color effect methods to be used

    Yep. In some cases you can change the word "class" to "enum" with no ill effects.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Store methods (or pointers) in variables?
    By Xyexs in forum New To Java
    Replies: 6
    Last Post: 11-12-2013, 08:49 AM
  2. jQuery effect
    By lilleza87 in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 02:36 PM
  3. fading effect
    By silversurfer2in in forum AWT / Swing
    Replies: 4
    Last Post: 06-11-2010, 03:23 PM
  4. Smoke Effect help
    By jammyjamsticy in forum AWT / Swing
    Replies: 1
    Last Post: 04-20-2009, 12:34 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
  •