Forcing a program to end

I am writing code to click different colored pixels on my screen, and I need a way to stop it. I have tried the noLoop() function in multiple places, but that did not work. Does anyone have any idea of how to reliably stop it when exported to an executable program? I am open to optimizations as well.

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
Robot colourSearcher;
boolean isRunning;
boolean hasClicked;
int dist;
void setup()
{
  size(10, 10);
  try
  {
    colourSearcher= new Robot();
  }
  catch(AWTException e)
  {
    System.out.println("Your robot could not be created!");
  }
  isRunning= false;
}

void draw()
{
  BufferedImage screenShot= colourSearcher.createScreenCapture(new Rectangle(0, 0, displayWidth, displayHeight));
  for (int x= 1; x<screenShot.getWidth(); x++)
  {
    for (int y= 0; y<screenShot.getHeight(); y++)
    {
      int pixelValue= screenShot.getRGB(x, y);
      if (pixelValue!=screenShot.getRGB(x-1, y))
      {

        colourSearcher.mouseMove(x, y);
        colourSearcher.keyPress(InputEvent.BUTTON1_MASK);
        colourSearcher.keyRelease(InputEvent.BUTTON1_MASK);
        hasClicked= true;
        if (hasClicked) break;
      }
      if (hasClicked) break;
    }
  }
}

Im assuming noLoop has an issue with one of the imports, for me, your example code makes my mouse go crazy moving on its own lol, not sure how to fix the issue for you but maybe put the draw loop in an if statement controlled by a boolean

Just call exit(). Your program will stop running.

2 Likes

Was going to say that but wasnt sure if they wanted to quit the program or to stop doing things but keep it displayed

Hi,

Just for info…

this …

… can be written like …

outter:
  for (int x= 1; x<screenShot.getWidth(); x++)
  {
    for (int y= 0; y<screenShot.getHeight(); y++)
    {
      int pixelValue= screenShot.getRGB(x, y);
      if (pixelValue!=screenShot.getRGB(x-1, y))
      {
        colourSearcher.mouseMove(x, y);
        colourSearcher.keyPress(InputEvent.BUTTON1_MASK);
        colourSearcher.keyRelease(InputEvent.BUTTON1_MASK);
        hasClicked= true;
        if (hasClicked) break outter;
      }
//      if (hasClicked) break;
    }
  }

… regarding noloop …

void draw()
{
  boolean stopcallingdrawagain=false;

  BufferedImage screenShot= colourSearcher.createScreenCapture(new Rectangle(0, 0, displayWidth, displayHeight));
outter:  
  for (int x= 1; x<screenShot.getWidth(); x++)
  {
    for (int y= 0; y<screenShot.getHeight(); y++)
    {
      int pixelValue= screenShot.getRGB(x, y);
      if (pixelValue!=screenShot.getRGB(x-1, y))
      {

        colourSearcher.mouseMove(x, y);
        colourSearcher.keyPress(InputEvent.BUTTON1_MASK);
        colourSearcher.keyRelease(InputEvent.BUTTON1_MASK);
        hasClicked= true;
        if (hasClicked) {
          stopcallingdrawagain=true;
          break outter;
        }
      }
    }
  }
  if (stopcallingdrawagain)
     noLoop(); // or even exit if you want to quit the entire programm
}

Cheers
— mnse

1 Like