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.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 75

Thread: I had an old code it really sucked, so i now am making a new one

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    Did you add the return statement at the end of the method?
    The {} should be aligned like this. Indented every nesting level
    {
        {  
              {
     
               }
        }
    } // end of method

  2. The Following User Says Thank You to Norm For This Useful Post:

    Lurie1 (February 1st, 2012)

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    ok fixed all those errors yay thanks Norm but now i go on to step 2 of my new flow chart and boom another problem!!..!!.. annoying!

    <
     
    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 GB{
    public boolean checkColor(Color inputColor) throws Exception
    	{
     
    	Robot robot = new Robot();
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
     
    		BufferedImage image = robot.createScreenCapture(rectangle);
    		for(int x = 0; x < rectangle.getWidth(); x++) 
    		{
    			for (int y = 0; y < rectangle.getHeight(); y++) 
    			{
    				if (image.getRGB(x, y) == inputColor.getRGB()) 
    				{
    					return true;
    				}
    		    return false;
    			}
    			{
    				while (!checkColor(inputColor)) 
    				{
    					inputColor = new Color(196, 195, 181);
     
    					robot.mouseMove(x, y);
    				    robot.mousePress(InputEvent.BUTTON1_MASK);
    					robot.delay(150);
    					robot.mouseRelease(InputEvent.BUTTON1_MASK);
    				}
    			}
    		return false;
    		}
    	return false;
    	} 
    } 
    >

    i get 1 error:

    GB.java:36: cannot find symbol
    symbol : variable y
    location class GB

    line 36:

    robot.mouseMove(x, y);

    what i don't get is why it found x but not y. because i know its goina be asked, the symbols x, y i want are the ones that the method came up with when it searched for the pixel. the line for this is:

    if (image.getRGB(x, y) == inputColor.getRGB())

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    The variable y is only known within the for loop where it is defined. It is not known outside of the loop.

    Look at the logic of your code. In the for(y) loop, it can sometimes return true or it will return false.
    If it enters the loop, it well never exit the loop. It will always do a return.

    What are the extra pair of {}s used before the while() statement used for ?

    The formatting of the code is much better except for the placement of the return false statements. They should be indented within the {}s that they are in.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    ok ill fix that code, so how would i be able to call that variable, for say the mouse move, or assign (x, y) something if i want to check a specific pixel, i would have to declare them as variables like the inputColor right?

  6. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    Yes you could define the y outside of the for loop.
    What value do you want y to have outside of the loop? It would always have the last value it had when it exited the loop.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    well i want (x, y) to be the pixel coordinates of the pixel either just found through a call with a new input color or the coordinates being called to find the color.

  8. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    In your other thread for this problem:
    http://www.javaprogrammingforums.com...tone-wall.html
    I said:
    you need to work out the logic for all the different cases and program them using if statements etc.
    There are many techniques for working out logic. I use flowcharts. Some use pseudo code.
    Do you have a technique for working out program logic?
    Bottom line, you need to work out the logic before you try to code it.

    What you just posted is close to pseudo code, try writing the logic making a list of the steps that need to be decided and the marking the loop back points for re starting the search.
    This code you have posted here has no logic to it. There are method calls and loops here and there but no logical connection between them. You need to stop coding and spend some time designing what the code is supposed to do.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    i suppose it should have 0 and so should x, the only thing is when it returns with a x,y and pixel as true or false those x,y values would be the ones used for mouseMove and in the next run through i could use those values again say if it returns false check at same x,y again.

  10. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    Can you describe in detail what the checkColor method is supposed to do?
    What arguments are passed to it,
    what processing it does
    What results it returns and what values it changes


    Then take each of the steps you have listed and put them into the code as comments.
    Each section of the code should be describe by an item/step that you have written down before you write the code.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    ok will do ill post it when im finished

  12. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    The idea is to do the design BEFORE you write the code.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    ok let me explain it's concept to you:

    the program when launched will recognize two colors, it will click on them one after the other to open the game window. when the cage window is open the program will make 4 opening moves, a click drag release, click drag release, click drag release, the fourth move will be depend of if a pixel, is one color or another and the program needs to keep checking the pixel until it becomes the desired color, then it will do the fourth and final opening move. that is all i need it to do.

  14. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    the program when launched will recognize two colors,
    it will click on them one after the other to open the game window.
    when the cage window is open the program will make 4 opening moves,
    1)a click drag release,
    2)click drag release,
    3)click drag release,
    4)the fourth move will be depend of if a pixel is one color or another
    and
    the program needs to keep checking the pixel until it becomes the desired color,
    then it will do the fourth and final opening move.

    Now write detailed specifications for what the method that will check for the desired color: 4)
    What are the steps that it needs to do?
    Make a list of simple steps or pseudo code that describes how it will work. I don't see any design in what you have coded for that method.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    okey dokey one sec

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    java GB.java

    program searches for color (196, 195, 181)
    program moves mouse to color
    program clicks color
    program releases mouse

    program searches for color (95, 118, 127)
    program moves mouse to color
    program clicks color
    porgram releases mouse

    program searches for a color
    if color is not found searches again

    when color is found program moves mouse to new color (252, 200, 111)

    from color program moves mouse -15 pixels on y axis
    program clicks
    program drags (right 5 pixels)
    program releases mouse button

    program moves mouse +50 pixels on the y axis
    program clicks program drags (left 5 pixels)
    program releases mouse button

    program moves mouse +200 pixels on x axis
    program clicks
    program drags mouse (left 5 pixels)

    program finds pixel using current mouse pozition and taking away 395 pixels on the x axis and -60 pixels on the y axis

    it gets current pixel color
    checks color ever 50 miliseconds

    when pixel color = not the same as orriginal pixels color it moves mouse by -15 pixels on the y axis

    clicks
    drags right by 5 pixels
    releases button

    is that what you wanted?

  17. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    Is all that the logic for the one method: checkColor? This method you are trying to code:
    public boolean checkColor(Color inputColor) {
        BufferedImage image = robot.createScreenCapture(rectangle);
        for(int x = 0; x < rectangle.getWidth(); x++) {
            for (int y = 0; y < rectangle.getHeight(); y++) {
                if (image.getRGB(x, y) == inputColor.getRGB()) {
                    return true;
                }
            }
        }
        return false;
    	{
     
    		while (!checkColor(newColor)) {
    		new Color = new Color(196, 195, 181);
    	}
    	}
    }

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    no you remember my original code? well i had it there so each time i wanted a pixel returned i had a different get pixel method, i was told and agree that that was a bad way to go about it, what i have so far is supposed to be a method i can call on whenever i want a pixel checked or found:

    <
    public boolean checkColor(Color inputColor) throws Exception
    	{
     
    	Robot robot = new Robot();
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
     
    		BufferedImage image = robot.createScreenCapture(rectangle);
    		for(int x = 0; x < rectangle.getWidth(); x++) 
    		{
    			for (int y = 0; y < rectangle.getHeight(); y++) 
    			{
    				if (image.getRGB(x, y) == inputColor.getRGB()) 
    				{
    					return true;
    				}
    		    return false;
    			}
    			{
    				while (!checkColor(inputColor)) 
    				{
    					inputColor = new Color(196, 195, 181);
     
    					robot.mouseMove(x, y);
    				    robot.mousePress(InputEvent.BUTTON1_MASK);
    					robot.delay(150);
    					robot.mouseRelease(InputEvent.BUTTON1_MASK);
    				}
    			}
    		}
    >

  19. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    What is the while (!checkColor(inputColor)) for?
    When is it executed?

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    oh i see a problem that is suppose to call the method to find the pixel indicated 196, 195, 181 and return the x, y values and true or false depending on if it found it or not

  21. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    How does that code fit into the design for what checkColor is supposed to do?
    I don't see any relationship.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    ya i was just looking at that and it dose not do anything

  23. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    Also see post#28

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    <
    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 GB{
     
    int x;
    int y;
     
    public boolean checkColor(Color inputColor) throws Exception
    	{
     
    	Robot robot = new Robot();
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
     
    		BufferedImage image = robot.createScreenCapture(rectangle);
    		for(int x = 0; x < rectangle.getWidth(); x++) 
    		{
    			for (int y = 0; y < rectangle.getHeight(); y++) 
    			{
    				if (image.getRGB(x, y) == inputColor.getRGB()) 
    				{
    					return true;
    				}
    		    return false;
    			}
    		} return false;
    	} 
    } 
    >

    ok wait so how would i change that so it will return with three values, the x cordinent the y cordinent and the pixel RGB value, it wouldn't be Boolean anymore right? It would be an int right?
    Last edited by Lurie1; February 2nd, 2012 at 10:00 PM.

  25. #49
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I had an old code it really sucked, so i now am making a new one

    Are you changing the design of what the method is supposed to do now?
    A method can only return one value. To return more than one value it would have to put all of them into a class as member variables, create an instance of that class with the values and return that instance.

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

    Default Re: I had an old code it really sucked, so i now am making a new one

    well according to my outline this method needs to be able to return all or one of the following:
    x
    y
    RGB color

    now how would i got about putting those three returns in to a class as a member?

    <
    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 GBpixel{
     
    public int x;
    public int y;
    public int inputColor;
     
    public int checkColor(Color inputColor) throws Exception
    	{
     
    	Robot robot = new Robot();
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
     
    		BufferedImage image = robot.createScreenCapture(rectangle);
    		for(int x = 0; x < rectangle.getWidth(); x++) 
    		{
    			for (int y = 0; y < rectangle.getHeight(); y++) 
    			{
    				if (image.getRGB(x, y) == inputColor.getRGB()) 
    				{
     
    				}
    			}
    		}
    	return 0;
    	}
    } 
    >

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Making change
    By Jerick in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 7th, 2011, 06:49 PM
  2. Making a clock
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2011, 04:36 PM
  3. making my code a little better, if needed.
    By vendetta in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2010, 03:40 AM
  4. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM
  5. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM