Results 1 to 7 of 7
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Method not working as intended

    I have created a simple nudging method, which nudge a BufferedImage downwards.

    Java Code:
    	private static BufferedImage nudge (BufferedImage image, int times)
    	{
    		if (0 >= times)
    			throw new IllegalArgumentException ("Argument must exceed 0: " + times);
    			
    		BufferedImage nudgedImage = new BufferedImage (image.getWidth (), image.getHeight(), image.getType());
    		
    		Loop:
    		for (int yPixel = 0; yPixel < image.getHeight (); yPixel++)
    			for (int xPixel = 0; xPixel < image.getWidth (); xPixel++)
    			{
    				try
    				{
    					nudgedImage.setRGB(xPixel, yPixel + 1, image.getRGB (xPixel, yPixel));
    				}
    				catch (Exception e)
    				{
    					break Loop;
    				}
    			}
    
    		if (--times != 0)
    			nudge (nudgedImage, times);
    		
    		return nudgedImage;
    	}
    The nudging works, but it only nudge one frame down, ignoring the purpose of the second argument.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: Method not working as intended

    Try debugging the code by adding printlns to show what image is being changed and what image is returned.
    Are you always returning the image that was changed at the first call and not using the image that was changed at a nested call?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Method not working as intended

    You're throwing out the results returned from recursion. Try something like:

    Java Code:
    if (--times != 0) { // always use curly brackets!
         return nudge (nudgedImage, times);  // return here!
    } else {
        return nudgedImage;
    }

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Method not working as intended

    Fubarable: if I inserted the return statement in a else block, the compiler would be pissed.

    anyway, I added debug codes and it does not make sense at all. I am clueless.


    Java Code:
    	public static void main(String[] args) throws Exception
    	{
    		BufferedImage img = nudge ( ImageIO.read(new java.io.File ("C:\\Users\\Pojahn\\Desktop\\img.png")), 10);
    		ImageIO.write(img, "PNG", new java.io.File ("C:\\Users\\Pojahn\\Desktop\\imgn.png"));		
    	}
    Java Code:
    	static int counter = 0;
    	private static BufferedImage nudge (BufferedImage image, int times)	// NUDGAR ENDAST EN GÅNG ---------------------------------------
    	{
    		if (0 >= times)
    			throw new IllegalArgumentException ("Argument must exceed 0: " + times);
    			
    		BufferedImage nudgedImage = new BufferedImage (image.getWidth (), image.getHeight(), image.getType());
    		
    		Loop:
    		for (int yPixel = 0; yPixel < image.getHeight (); yPixel++)
    			for (int xPixel = 0; xPixel < image.getWidth (); xPixel++)
    			{
    				try
    				{
    					nudgedImage.setRGB(xPixel, yPixel + 1, image.getRGB (xPixel, yPixel));
    				}
    				catch (Exception e)
    				{
    					break Loop;
    				}
    			}
    
    		if (--times != 0)
    		{
    			System.out.println (counter++);
    			nudge (nudgedImage, times);
    		}
    		System.out.println ("return");
    		return nudgedImage;
    	}
    and the results are:

    0
    1
    2
    3
    4
    5
    6
    7
    8
    return
    return
    return
    return
    return
    return
    return
    return
    return
    return
    The output file(imgn.png) is only nudged one pixel down. it should be ten.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Method not working as intended

    Quote Originally Posted by Pojahn_M View Post
    Fubarable: if I inserted the return statement in a else block, the compiler would be pissed.
    You didn't look carefully at my answer. The return statement goes in both the if and the else block. I even had a comment in the code to point this out.

    And that's the key: You are doing recursion, and must return the results of the recursion (the if block) unless you're at a stopping condition (the else block).
    Last edited by Fubarable; 01-08-2012 at 10:37 PM.

  6. #6
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Method not working as intended

    Ops... Interesting strategy. It works now. I still dont see why my first code did not work though...

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Method not working as intended

    Quote Originally Posted by Pojahn_M View Post
    Ops... Interesting strategy. It works now. I still dont see why my first code did not work though...
    You should walk through your code in your head a bit to see what happens.

    Say you call the method with a times parameter of 3. This is what happens:

    Java Code:
    code nudges image
    gets to if block, sees that --3 (or 2) is not 0 and calls nudge with times param of 2
       code nudges image
       gets to if block, sees that --2 (or 1) is not 0 and calls nudge with times param of 1
          code nudges image
          gets to if block, sees that --1 (or 0) is in fact 0 
          method returns its nudged x 3 image
       this method ignores what was returned above but instead simply returns its nudged x 2 image
    this method ignores what was returned above but instead simply returns its nudged x 1 image.

Similar Threads

  1. Output does not dispay as intended -- HELP!!
    By janalyzer in forum New To Java
    Replies: 5
    Last Post: 01-24-2012, 05:14 AM
  2. Method Not Working
    By Jacob1028555 in forum New To Java
    Replies: 5
    Last Post: 06-10-2011, 01:08 AM
  3. keyPressed method doesn't work as intended
    By JohnPringle83 in forum New To Java
    Replies: 6
    Last Post: 05-31-2011, 09:22 PM
  4. ugh another method that isn't working
    By jjth39347 in forum New To Java
    Replies: 14
    Last Post: 04-17-2011, 09:57 PM
  5. long to double trouble (Rhyme scheme not intended)
    By sdeverteuil in forum New To Java
    Replies: 2
    Last Post: 08-18-2009, 11:02 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
  •