Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Loop not working

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Loop not working

    <
    {
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
    	{
    		while(true)
    		{
            BufferedImage image = robot.createScreenCapture(rectangle);
            search: for(int x = 0; x < rectangle.getWidth(); x++)
    			{
                for(int y = 0; y < rectangle.getHeight(); y++)
    				{
    					if(image.getRGB(x, y) == color1.getRGB())
    						{
    							return true;
    						}
    					return false;
    				}
    			}
    		}
    	}
    }
    >

    what im having a problem with is that the loop while(true) is not working and i need it to work

    this method is supposed to search through a screenshot and find a pixel and if it dose not take another shot and look again but it is not doing that when i run the program


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop not working

    the loop while(true) is not working
    Please explain what the code does and why you think it is not working.

    When the code executes the return false; statement, that will end the loop. Are you sure that you want to end the loop the first time the colors don't match?


    The formatting of your code makes it very hard to read. The {}s should be aligned and not left and right as yours are.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Loop not working

    Saying "it's not working" is as useful to us as us saying "then fix it" is useful to you. Please provide an SSCCE that demonstrates the actual problem, and ask a specific technical question. What doesn't work about your loop? What does it do that you don't expect? Have you stepped through this with a debugger, or simply added some print statements, to figure out the flow of the program? What are all of those variables whose declarations are missing? Is that really how you indent your code?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Loop not working

    That loop only ever looks at a single pixel in a single image. To see that try:

    Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
    {
        while(true)
        {
            System.out.println("About to take screen shot");
            BufferedImage image = robot.createScreenCapture(rectangle);
            search: for(int x = 0; x < rectangle.getWidth(); x++)
            {
                for(int y = 0; y < rectangle.getHeight(); y++)
                {
                    System.out.println("Looking at pixel " + x + "/" + y);
                    if(image.getRGB(x, y) == color1.getRGB())
                    {
                        System.out.println("About to finish and return true");
                        return true;
                    }
                    System.out.println("About to finish and return false");
                    return false;
                }
            }
        }
    }

    You might to replace the "true" while(true) with something that better expresses your intent that the loop should continue until a pixel of a specific colour has been found. Also remove the label "search:" - it isn't being used.

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loop not working

    ok explanation, no i want that chunk of code to repeat over and over again until it returns with a true (an i found the pixel mate) and im hoping the while loop is the best one but im open to suggestions.
    Last edited by Lurie1; February 15th, 2012 at 07:19 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop not working

    Remove the return false from inside of the loop.

  7. #7
    Member
    Join Date
    Jan 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loop not working

    ok so where would i put it? i took it out completely and its still not looping, do i need to put it in at the end of the method?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop not working

    its still not looping,
    Add a printlns as shown in post#4 in the loop to show if it is looping.
    If the method does not execute return true, where is it executing?
    The return appears to be the ONLY way to exit the loop. The loop should loop forever.

  9. #9
    Member
    Join Date
    Jan 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loop not working

    <
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Robot;
    import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Color;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
     
    public class Gun{
    	static void Main() throws Exception 
    	{
    	Robot robot = new Robot();
    	Color color1 = new Color(196, 195, 181);
    	Color color2 = new Color(95, 118, 127);
    	Color color3 = new Color(108, 25, 85); 
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
    	{
    	BufferedImage image = robot.createScreenCapture(rectangle);
    	search: for(int x = 0; x < rectangle.getWidth(); x++)
    		{
    		for(int y = 0; y < rectangle.getHeight(); y++)
    			{
    			if(image.getRGB(x, y) == color1.getRGB())
    				{
    				robot.mouseMove(x, y);
    				robot.mousePress(InputEvent.BUTTON1_MASK);
    				robot.delay(10);
    				robot.mouseRelease(InputEvent.BUTTON1_MASK);
    				break search;
    				}
    			}
    		}
        }
        {
        BufferedImage image = robot.createScreenCapture(rectangle);
        search: for(int x = 0; x < rectangle.getWidth(); x++)
            {
                for(int y = 0; y < rectangle.getHeight(); y++)
                {
                    if(image.getRGB(x, y) == color2.getRGB())
                    {
                        robot.mouseMove(x, y);
    					    robot.mousePress(InputEvent.BUTTON1_MASK);
    						robot.delay(10);
    						robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        break search;
                    }
                }
            }
        }
    	}
    }
    static boolean openingboard() throws Exception
    {	
    	Robot robot = new Robot();
    	Color color3 = new Color(108, 25, 85);
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
    		while(true)
    		{
            BufferedImage image = robot.createScreenCapture(rectangle);
            search: for(int x = 0; x < rectangle.getWidth(); x++)
    			{
                for(int y = 0; y < rectangle.getHeight(); y++)
    				{
    					if(image.getRGB(x, y) == color3.getRGB())
    					{
    							System.out.println("About to finish and return true");
    							return true;
    					}
    					System.out.println("About to finish and return false");
    				}
    			}
    		}
    	}
    }
     
     
     
     
     
    >

    ok now im getting the exception in thread "main" nosuchmethod error i have a Main class but do i have to have a main method? i don't know why im getting the error for the above code (yes its messy sorry ill fix and repost)
    Last edited by Lurie1; February 15th, 2012 at 08:34 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop not working

    exception in thread "main" nosuchmethod error i have a Main class
    When you get an error, please copy and paste here the full text of the error message.

    do i have to have a main method?
    If you want to have the java program execute the class it must have a proper main method.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop not working

    Are you conscious that with the while(true) statement you are creating a infinite loop ?

    Oscar Gómez
    Engineer at PSL
    PSL S.A. - CMMi 5 nearshore software development and outsourcing from Latin America - Home

  12. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop not working

    Are you conscious that with the while(true) statement you are creating a infinite loop ˇ

    Oscar Gómez
    Engineer at PSL
    PSL S.A. - CMMi 5 nearshore software development and outsourcing from Latin America - Home

  13. #13
    Junior Member
    Join Date
    Mar 2012
    Location
    Behala, Kolkata
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop not working

    replace return false statement from inside the for loop. It should be outside the while(true) loop.

Similar Threads

  1. Hey guys, my While loop isn't working correctly.
    By metanoia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 26th, 2011, 11:15 PM
  2. Can someone please tell me why my do while loop isn't working?
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 20th, 2011, 03:52 PM
  3. Loop conditions not working
    By bmxershane in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 1st, 2011, 02:25 AM
  4. [SOLVED] Loop Breaking Not Working
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2010, 09:06 PM
  5. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM