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

    Default Java how to have static or non-static method reference as parameter?

    First, whilst the example below is AWT related and is what I'm trying to do, I wouldn't know how to do this and I'm trying to find out in any way of doing this at all, not just for Color. Its just easier for me to use my current situation than come up with some random numbers scenario...

    Let's say I had a TestColor class which contained methods to change hue, saturation, brightness, red, green, blue of TestColor's instances but also had static methods which take in an additional parameter for an instance of TestColor and returns the affected instance of TestColor. Now instead of having one method for every possible color effect to be applied to an image, how can I have one method that takes in an Image parameter, a static or non-static method reference from TestColor parameter and lastly an intensnity value parameter. This is so that I can make an affectedImage object instance inside the method and a Graphics2D object for drawing to each pixel of the new image, now I have one for loop and one nested for loop for the x and y pixels of width and height of the old image and inside the nested for loop I'd create a TestColor by calling getRGB on the image's pixel. Then I would apply the static or non-static method reference somehow to change the color with the intensnity value and after applying it draw to the new Image with Graphics2D. How to would I parametize a method reference and be able to use it in such way?
    Last edited by augustas656; 11-18-2014 at 08:45 PM.
    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: Java how to have static or non-static method reference as parameter?

    It is easy to have a class modify an instance of itself. Just pass the class as an argument and provide methods to modify the desired effect. Then return that instance. You can also create another method which takes an image and a TestClass instance and applies the methods to the image. I do not understand the need for method references in this case.

    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: Java how to have static or non-static method reference as parameter?

    Ehmm, let me write an example of what I mean:

    TestColor class:
    Java Code:
    package net.example.test;
    
    import java.awt.Color;
    
    public class TestColor {
    	Color color;
    	public Color get() { return color; }
    	
    	public float[] getHSB() {
    		float[] HSB = new float[3];
    		int red = color.getRed();
    		int green = color.getGreen();
    		int blue = color.getBlue();
    		Color.RGBtoHSB(red, green, blue, HSB);
    		return HSB;
    	}
    	
    	public TestColor(int rgb) {
    		color = new Color(rgb);
    	}
    	
    	public void changeBrightness(float brightness) {
    		brightness += getHSB()[2];
    		brightness = Math.min(1.0F, brightness);
    		brightness = Math.max(0.0F, brightness);
    		float hue = getHSB()[0];
    		float sat = getHSB()[1];
    		color = new Color(Color.HSBtoRGB(hue, sat, brightness));
    	}
    	
    	public void changeSaturation(float saturation) {
    		saturation += getHSB()[1];
    		saturation = Math.min(1.0F, saturation);
    		saturation = Math.max(0.0F, saturation);
    		float hue = getHSB()[0];
    		float bri = getHSB()[2];
    		color = new Color(Color.HSBtoRGB(hue, saturation, bri));
    	}
    	
    	public static TestColor changeBrightness(TestColor color, float brightness) {
    		color.changeBrightness(brightness);
    		return color;
    	}
    	
    	public static TestColor changeSaturation(TestColor color, float saturation) {
    		color.changeSaturation(saturation);
    		return color;
    	}
    }
    TestImage class:
    Java Code:
    package net.example.test;
    
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    
    
    public class TestImage {
    	public static void main(String[] args) {
    		
    	}
    	
    	BufferedImage image;
    	public BufferedImage get() { return image; }
    	
    	public TestImage(int width, int height) {
    		int type = BufferedImage.TYPE_INT_RGB;
    		image = new BufferedImage(width, height, type);
    	}
    	
    	public TestImage(BufferedImage image) {
    		this.image = image;
    	}
    	
    	public void set(BufferedImage image) {
    		this.image = image;
    	}
    	
    	public BufferedImage applyColorEffect(BufferedImage image, colorEffectReference, int intensity) {
    		BufferedImage affectedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    		Graphics2D g = affectedImage.createGraphics();
    		for (int x = 0; x < image.getWidth(); x++) {
    			for (int y = 0; y < image.getHeight(); y++) {
    				TestColor color = new TestColor(image.getRGB(x, y));
    				apply colorEffectReference
    				g.setColor(color.get());
    				g.drawLine(x, y, x, y);
    			}
    		}
    		g.dispose();
    		return affectedImage;
    	}
    }
    This is an example I just wrote, this is what I'm trying to do...
    Regards
    augustas656

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

    Default Re: Java how to have static or non-static method reference as parameter?

    First, I still do not know what the "apply colorEffectReference" is supposed to do. Are you trying to apply it to the entire buffered image in a single operation. What are you applying it to?

    I presume you are trying to set a pixel with your drawLine(x,y, x,y). BufferedImage has getRGB() and setRGB() methods which may facilitate
    matters.

    And using the variable color for Color in one location and for TestColor in another location is very confusing. I almost flagged your static return types as an error.

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

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

    Default Re: Java how to have static or non-static method reference as parameter?

    I like to keep things short, and I wrote this code in eclipse, apply colorEffectReference is suppose to call the method reference from TestColor of either one of the static methods or non-static methods that's a parameter in the applyColorEffect method in TestImage class. The way I wrote apply colorEffectReference is a syntax error however the whole point of this thread is finding out how to use method references in this or atleast a very similar manner, which I don't know how to do and can't find online.
    Regards
    augustas656

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

    Default Re: Java how to have static or non-static method reference as parameter?

    Quote Originally Posted by augustas656 View Post
    apply colorEffectReference is suppose to call the method reference from TestColor of either one of the static methods or non-static methods that's a parameter in the applyColorEffect method in TestImage class.
    I don't believe using references is what you want (though I could be mistaken). You still need to provide the appropriate parameters that the function reference expects. Here is an example. I am using the JDK supplied functional interface BiFunction to take advantage of the dynamic typing per call using generics. I have defined callMethod to demonstrate passing a function reference to another method and invoking it. Your situation is even more complicated in that the number of arguments varies between the methods.

    Java Code:
    import java.util.function.BiFunction;
    
    public class MethodRefDemo {
       public static void main(String[] args) {
         
          BiFunction<Integer,Integer,Integer> add = (a,b)->a + b;
          BiFunction<Integer,Integer,Integer> mul = (a,b)->a * b;
          BiFunction<String, String, String> concat = (a,b)->a + b;
          
          int sum = callMethod(add::apply,10, 20);
          System.out.println(sum);
          int prod = callMethod(mul::apply, 9, 12);
          System.out.println(prod);
          String str = callMethod(concat::apply, "Hello, ", "World!");
          System.out.println(str);
       }
       
       public static <T,U,R> R callMethod(BiFunction<T,U,R> foo, T first, U second) {
          return foo.apply(first, second);
       }
    }
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Java how to have static or non-static method reference as parameter?

    Do you think I could use some workaround for what I'm trying to do, that would shorten the code length?
    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: Java how to have static or non-static method reference as parameter?

    Why not just use an enum and pass the enum to the method? Each enum constant can have its own specific method implementation which calls other methods in the same enum. I already provided an enum example in another thread. You must read and try some examples on your own to understand how they work.

    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: Java how to have static or non-static method reference as parameter?

    I can't think of anyway to adapt your enum example to support custom parameters. The values inside the brackets of a constant must be static (what my eclipse says) and then they are fixed values if they're static, how would I make a public method to apply these effects...
    Regards
    augustas656

Similar Threads

  1. Replies: 5
    Last Post: 03-31-2013, 03:30 AM
  2. static reference to non static method
    By TSTONESIX in forum New To Java
    Replies: 7
    Last Post: 03-13-2013, 03:07 PM
  3. Cannot make a static reference to the non-static method
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 12:42 AM
  4. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  5. Replies: 7
    Last Post: 09-24-2010, 05:09 AM

Posting Permissions

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