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 15 of 15

Thread: Search for the Array

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

    Default Search for the Array

    <
    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 Forage
    {
    	public static void main() throws Exception 
    	{
    	int[] anArray;
    	Robot robot = new Robot();
    	anArray = new int[5];
     
    	anArray[4] = (new Color(91, 147, 191)).getRGB();
    	anArray[3] = (new Color(215, 201, 79)).getRGB();
    	anArray[2] = (new Color(74, 93, 132)).getRGB();
    	anArray[1] = (new Color(74, 93, 132)).getRGB();
    	anArray[0] = (new Color(91, 147, 191)).getRGB();	
    	Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
    	BufferedImage image = robot.createScreenCapture(rectangle);
    	int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth()); 
     
    	for(int x = 0; x < rectangle.getWidth(); x++)
    		{
    		for(int y = 0; y < rectangle.getHeight(); y++)
    			{	
    			for (int i = 0; i < pixels.length; i++) 
    				{
    				if (Array.equals(anArray, Array.copyOfRange(pixels, i, i + anArray.length))) 
    					{
    					robot.mouseMove(x, y);
    					robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
    					robot.delay(10);
    					robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
    					System.out.println("opened");
    					}
    				}
    			}
    		}
    	}
    }
    >

    i am getting 6 errors:
    this error is the first 4 of the six, i thought image was defined by the buffered image line of code, line 29:

    java:27
    cannot find symbol
    variable image
    int[] pixels = image.getRGB(0, 0, image.getWidth()

    error 2:
    this error is the last 2 of the 6:

    java:36 cannot find symbol
    variable Array
    if (Array.equals(anArray,

    and i don't know how to solve this problem at all
    Last edited by Lurie1; February 17th, 2012 at 10:00 PM.


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

    Default Re: Search for the Array

    cannot find symbol
    variable image
    Where is the variable image defined? It looks like it is defined AFTER you try to use it.
    Define it BEFORE you try to use it.

    Think of it this way: If you went into a bank and tried to withdrawn money before you opened an account, the bank would object. The compiler is like that. It wants things done in the proper order.

    cannot find symbol
    variable Array
    Where is the variable Array defined? The compiler can not find a definition for it.

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

    Default Re: Search for the Array

    thanks norm i didn't realize i had them in the wrong order, i just updated the code to reflect this! but i don't have to define the variable i guess im using the wrong variable that bit of code where i use Array is suppose to search the image for the array of pixels so im guessing i should change Array to image right?

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

    Default Re: Search for the Array

    i didn't realize i had them in the wrong order
    The computer executes the statements in the order they are placed in the program. This is a VERY BASIC concept.
    If you don't understand that, you are going to have a very hard time writing programs.

    suppose to search the image for the array of pixels
    An image is not an array. You can get the RGB values for individual pixels using some methods and compare those obtained pixels against the contents of an array.
    But that is not what your code is doing. It has two arrays: anArray and pixels.
    It looks like you are trying to "compare" them?

    I suggest that you write a small simple program to work out the logic and methods to do this array searching.
    Define two arrays, one short and one longer with some values in them and then write a "compare" to see if the shorter array is contained in the longer array.
    This will allow you to test many different cases and be sure the code works before you move the logic into the current program you are working on.

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

    Lurie1 (February 18th, 2012)

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

    Default Re: Search for the Array

    ok will do
    ps i understood the above just did not check if they were in the correct order assuming something was wrong with the code not the placement.
    Last edited by Lurie1; February 17th, 2012 at 10:31 PM.

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

    Default Re: Search for the Array

    ok newbie question but once i set a rectangle using the robot class like the one above is there a way i can set that as the screen so when i say robot.mouseMove(x, y) it will move to the x, y inside of the rectangle not the x, y on the desktop?

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

    Default Re: Search for the Array

    Read the API doc for the mouseMove method. Does where do the x,y parameters refer to?

    You may have to compute the x,y relative to the rectangle given the rectangle's location on the screen. Take a piece of paper and draw them and see how to convert from one to the other.

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

    Default Re: Search for the Array

    well the problem because i could use screen coordinates for x, y but the rectangle will be in a different place every rime a random place on screen so would i have to say for each chunk of code set all x, y relative to a point on the screen or could i just define rectangle as the screen and all coordinates given are called inside rectangle? also mouseMove calls x, y on screen coordinates can i define screen as rectangle?

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

    Default Re: Search for the Array

    Did you try this:
    Take a piece of paper and draw the screen and the rectangle and see how to convert the x,y values from one to the other. The screen's x,y will be the rectangle's location x,y plus the x,y within the rectangle

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

    Default Re: Search for the Array

    yea i drew them out, ok well i guess if they have to be set like that they do...

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

    Default Re: Search for the Array

    You need to do what the methods require.

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

    Default Re: Search for the Array

    so the steps for specifying a point to either check would be, rectangle coordinates x, y + coordinates specified right?

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

    Default Re: Search for the Array

    Who are you giving the point's location to? That should determine how you specify its values.

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

    Default Re: Search for the Array

    to a robot function i want to move the mouse to that point

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

    Default Re: Search for the Array

    Then read the API doc for the method you want to use and see what it requires.

Similar Threads

  1. [SOLVED] Linear array search(without Arraylist)
    By Usoda in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 18th, 2011, 01:12 AM
  2. Array Search Method
    By Kyuubi426 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 08:31 PM
  3. Search for min and max values as columns position in array
    By susieferrari in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 07:39 AM
  4. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM
  5. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 10:47 PM