# ScreenCapture with Robot Class (code not working)

**URL:** <https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551>\
**Category:** Coding Questions\
**Created:** [June 7, 2021, 9:08am UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551 "2021-06-07T09:08:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [June 7, 2021, 9:08am UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/1 "2021-06-07T09:08:15Z")

</div>

i have an irl card that has the rgb value:11, 96, 133.  
i made a code that, scans the screen for that color so when you have the app camera open it says kaart gevonden (found card in dutch) on the screen. but it is not working, any ideas why?  
here is the code

```auto

```

```auto
int x=1;
int y=1;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
color pas = color(11, 96, 133);
 
Robot robot;
void setup() {
  size(1500, 3000);
  try {
    robot = new Robot();
  } 
  catch (Exception e) {
    println(e.getMessage());
  }
}
void draw() {
  background(0);
  Rectangle r = new Rectangle(y, x, width, height);
  BufferedImage img1 = robot.createScreenCapture(r);
  PImage img2 = new PImage(img1);
  image(img2, 0, 0);
  get(x,y);
  x++;
  y++;
  color colorFromGet = get(x,y);
     
     if(colorFromGet==(colorFromGet+pas)/2){
       textSize(50);
       text("kaart gevonden", width/2, height/2);
     
}
}

```

```auto

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 7, 2021, 10:15am UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/2 "2021-06-07T10:15:15Z")

</div>

Hello,

This adapted code will:

- screen capture a small area at the center of your display monitor
- display the image at the center of the sketch window
- check for color(0xFF, 0xFF, 0xFF) (white) at the center of the sketch window
- I used white so I can move a window in and out of capture area
- it will display "“kaart gevonden” if white is at center

```auto
// Screen Scanner
// v1.0.0
// GLV 2021-06-07

// Adapted from: 
// http://www.magicandlove.com/blog/tag/screen-capture/
// https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551

int x=1;
int y=1;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
color pas = color(0xFF, 0xFF, 0xFF);
 
Robot robot;
void setup() 
  {
  size(500, 600);
  try 
    {
    robot = new Robot();
    } 
  catch (Exception e) 
    {
    println(e.getMessage());
    }
  }

void draw() 
  {
  background(0);
  Rectangle r = new Rectangle(displayWidth/2-100, displayHeight/2-100, 300, 400);
  BufferedImage img1 = robot.createScreenCapture(r);
  PImage img2 = new PImage(img1);
  imageMode(CENTER);
  image(img2, width/2, height/2);
  
  //get(x,y); // Not used in this code!
  //x++;       
  //y++;
  
  color colorFromGet = get(width/2, height/2); //Gets color from center of sketch window   
  // Examples of what you can do here:
  //color colorFromGet = get(x, y);
  //color colorFromGet = get(mouseX, mouseY);
     
   if(colorFromGet == pas)
     {
     textSize(50);
     fill(0, 255, 0);
     textAlign(CENTER, CENTER);
     text("kaart gevonden", width/2, height/2);
     println("kaart gevonden"); 
     }
  println(frameRate);
  println(hex(get(mouseX, mouseY))); //Gets color from mosue location and displays as hex
  }

```

I just did a _quick exploration_ of this; there are certainly other ways to do this.  
You will have to go through code line by line and understand it.  
Modify it as you wish.

There are resources (tutorials, references, examples, etc.) here:  
_[https://processing.org/](https://processing.org/)_

Center of screen:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5a6a2bec9faf12323c98316e8bcc3452c5a3a30a.png)

Center of screen with a window (this could be your app) moved into focus:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9f33bc11da5de46e742add40e5e89ed54641c29f.png)

`:)`

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [June 7, 2021, 1:52pm UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/3 "2021-06-07T13:52:00Z")

</div>

thank you, what did you change the pas color into? it was the wrong rgb value and when i wanted to change it i saw it was something i didnt recognise

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 7, 2021, 2:19pm UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/4 "2021-06-07T14:19:11Z")

</div>

> [@MoonAlien822](#):
>
> what did you change the pas color into?

I already answered that.

> [@MoonAlien822](#):
>
> it was the wrong rgb value

It was the correct RGB value for the code I provided to test this.

> [@glv](#):
>
> You will have to go through code line by line and understand it.  
> Modify it as you wish.

There are tutorials to help you understand color.

`:)`

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [June 7, 2021, 2:38pm UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/5 "2021-06-07T14:38:25Z")

</div>

what kind of colour did you change it into? is it binary rgb?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 7, 2021, 3:16pm UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/6 "2021-06-07T15:16:44Z")

</div>

[https://processing.org/reference/color\_datatype.html](https://processing.org/reference/color_datatype.html)

[https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

```auto
// These all represent white

color c;

void setup()
{
c = #FFFFFF;
println(red(c), green(c), blue(c));

c = color (255, 255, 255);
println(red(c), green(c), blue(c));

//0xFF = 255 = 0b11111111
c = color(0xFF, 0xFF, 0xFF); //Hexadecimal notation 
println(red(c), green(c), blue(c));
int b = 0b11111111; //Binary notation
c = color(b, b, b);
println(red(c), green(c), blue(c));

c = 0xFFFFFFFF;
println(red(c), green(c), blue(c));
}

``:)``
```

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [June 7, 2021, 5:20pm UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/7 "2021-06-07T17:20:39Z")

</div>

thank you for your help. sadly it doesnt work because on different distances it has a different colour value and wont recognise it. but thanks anyway

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 8, 2021, 9:50am UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/8 "2021-06-08T09:50:21Z")

</div>

> [@MoonAlien822](#):
>
> sadly it doesnt work because on different distances it has a different colour value and wont recognise it. but thanks anyway

I see that as an opportunity and a challenge.

Consider using these:

- [if / Reference / Processing.org](https://processing.org/reference/if.html)
- [Logical Operators / Examples / Processing.org](https://processing.org/examples/logicaloperators.html)

I provided you with a tool to display the colors on the screen to get a sense of them:

`println(hex(get(mouseX, mouseY))); //Gets color from mouse location and displays as hex`

You can use this to get more of the color details and then write some code for that.  
`println(hex(c), red(c), green(c), blue(c), hue(c), saturation(c), brightness(c)); //Gets color from mouse location and displays as hex`

I found hue() to be useful and got this working for a range of hues (desktop folder in a screen capture in my case).

You are going to have to read the Processing color tutorial and look through color references and examples.

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 8, 2021, 11:02am UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/9 "2021-06-08T11:02:52Z")

</div>

I explained it in your other thread

I cannot understand why you ignored it

There are so many errors…

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [June 30, 2021, 10:19am UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/10 "2021-06-30T10:19:16Z")

</div>

> [@glv](#):
>
> println(hex(get(mouseX, mouseY))); //Gets color from mouse location and displays as hex

i tried to use it but it only displayed one colour even if i moved my mouse arround

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 30, 2021, 10:43am UTC](https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551/11 "2021-06-30T10:43:35Z")

</div>

> [@MoonAlien822](#):
>
> i tried to use it but it only displayed one colour even if i moved my mouse arround

Try this program from examples:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e33cff1ee2d4bcbfb91de445770ecf08b176b924.png)

Add this line of code to draw():  
`println(mouseX, mouseY, hex(get(mouseX, mouseY)));`

`:)`
