# Forcing a program to end

**URL:** <https://discourse.processing.org/t/forcing-a-program-to-end/37251>\
**Category:** Coding Questions\
**Created:** [May 31, 2022, 4:42pm UTC](https://discourse.processing.org/t/forcing-a-program-to-end/37251 "2022-05-31T16:42:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timeless\_Mage](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Timeless\_Mage](https://discourse.processing.org/u/Timeless_Mage)\
**Post date:** [May 31, 2022, 4:42pm UTC](https://discourse.processing.org/t/forcing-a-program-to-end/37251/1 "2022-05-31T16:42:05Z")

</div>

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.

```auto
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;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 3, 2022, 9:00am UTC](https://discourse.processing.org/t/forcing-a-program-to-end/37251/2 "2022-06-03T09:00:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [June 3, 2022, 9:02am UTC](https://discourse.processing.org/t/forcing-a-program-to-end/37251/3 "2022-06-03T09:02:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 3, 2022, 11:46am UTC](https://discourse.processing.org/t/forcing-a-program-to-end/37251/4 "2022-06-03T11:46:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 3, 2022, 1:17pm UTC](https://discourse.processing.org/t/forcing-a-program-to-end/37251/5 "2022-06-03T13:17:08Z")

</div>

Hi,

Just for info…

this …

> [@Timeless\_Mage](#):
>
> ```auto
> 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;
> }
> }
> 
> ```

… can be written like …

```auto
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 …

```auto
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
