Search code examples
javaarraysrandomrgbawtrobot

how to add two coordinates (x,y) to a arraylist


My code reads all the pixels from the screen until it finds a pixel with the RGB value that I specified, which I managed to do, however if my screen has multiple pixels of that RGB value, I want to be able select a random pixel within that RBG value, instead of only working with the first pixel that my code finds I want to work with a random one. A workaround that I made generates a random number between 1 and 100 and everytime it checks a pixel it has a 1% chance of working with it, however this isn't the best method because of the way that my code scans the screen, (top-down, left-right) that means most of the time the pixel will end up on the upper left corner with isn't exactly random. I thought a good solution would be evertime the code goes through the if statement it stores the values x,y in a arraylist then I would get a random Index from that arraylist, but I couldn't figure out how to do that. I appreciate any suggestions and help :)

            int rng = new Random().nextInt(100) + 1;
            
            if (color.equals(iron) && rng == 5){
                if (r != 215 || g != 215 || b != 215){
                    Robot move = new Robot();
                    mousemoving.mouseGlide(x,y,1250,1000);
                    Runecraft.randomInt(35, 481);
                    move.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                    move.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

                    System.out.println(iron);
                    flag = true;
                    break;

Solution

  • how to add two coordinates (x,y) to a arraylist

    Check out the Point or Point2D classes. They have exactly what you want.

    Then

    List<Point> points = new ArrayList<>();