# PG Graphics not working - bet you can't work this out!

**URL:** <https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250>\
**Category:** Coding Questions\
**Created:** [January 20, 2021, 8:17pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250 "2021-01-20T20:17:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [January 20, 2021, 8:17pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/1 "2021-01-20T20:17:51Z")

</div>

I want to create pg graphics size x,y in black.  
In middle I want a non black point.

Randomly searching through all pg graphics, I want to find this point in a routine iterating over and over.  
Once found - return that random point as the matched x,y coordinate values that were initially chosen (mid of pg graphics)

Soooo easy with any language !!!  
But cannot do it in Processing.  
Why not ??

Please help!!

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [January 20, 2021, 8:57pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/2 "2021-01-20T20:57:08Z")

</div>

Do you mean something like this?

```auto
PGraphics pg;

void setup() {
  size(50, 50);
  pg = createGraphics(width, height);
}

void draw() {
  int rand_x;
  int rand_y;
  pg.beginDraw();
  pg.background(0);
  pg.stroke(255);
  pg.point(pg.width / 2, pg.height / 2);
  pg.endDraw();
  image(pg, 0, 0);
  rand_x = (int)random(pg.width);
  rand_y = (int)random(pg.height);
  if (red(pg.get(rand_x, rand_y)) > 0) {
    noLoop();
    background(127);
    text(rand_x + ", " + rand_y, 5, height - 5);
  }
}

```

It may take a while for it to find the point, but eventually it will.

Modify as needed; please let us know whether it does what you want.

---

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [January 20, 2021, 9:05pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/3 "2021-01-20T21:05:20Z")

</div>

Can’t I just find the point outside void draw??

You set up a pg screen size. Say 100 by 100  
In black.  
In middle you put a point at 50,50 in white.

Set up done.

Now you randomly select x,y points until you find that point which is not black. The mid point.

Then you return that point as x,y coordinates.

Want this as a sub routine outside draw.

Really is simple but don’t understand how to use it in set up or draw. ???

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [January 20, 2021, 9:22pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/4 "2021-01-20T21:22:00Z")

</div>

> [@IRIGIMA](#):
>
> Can’t I just find the point outside void draw??

Yes, you can do it that way by writing a function that takes a `PGraphics` argument, looks for the point, then returns the coordinates.

> [@IRIGIMA](#):
>
> … don’t understand how to use it in set up or draw. ???

You can also do it that way. Either way you do it, call `noLoop()` within `draw()` prior to performing the search, if you choose to include the search within an explicit `while` loop.

Which do you prefer, to look for the point within `draw()`, or within an external function?

---

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [January 20, 2021, 10:01pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/6 "2021-01-20T22:01:52Z")

</div>

Actually still not working.

This is what I got so far::

PGraphics pg;

float col=10;  
int count=0;

int xsize=1500;  
int ysize=500;  
int tempx;  
int tempy;  
int temppixel;  
int px;  
int py;  
float pixheight;

int pixel;

void setup(){  
size(1500, 500);  
noSmooth();  
colorMode(RGB,255);  
pg=createGraphics(xsize, ysize);  
pg.colorMode(RGB,255);  
pg.noSmooth();  
pg.beginDraw();  
pg.background(0,0,0);  
pg.loadPixels();  
pg.fill(255);  
pg.strokeCap(SQUARE);  
frameRate(10);

```
// put a point in centre of pg graphics
int px=xsize/2;
int py=ysize/2;
pixel=(px+(py*xsize));
pg.pixels[pixel] = color(200,200,200);
pg.updatePixels();
image(pg,0,0,1500,500);

noLoop();
position();    

```

}

// get a point that is not 0  
void position() {  
px=int((random(1,xsize-1)));  
py=int((random(1,ysize-1)));  
pixel=(px+(py\*xsize));  
float rr=(red(pg.pixels[pixel]));

if(rr==0) {  
position();  
}  
draw();  
}

// main loop

void draw() {  
noLoop();  
println(px);  
}

I’ll get there one day!!  
Just can’t understand why so difficult in Processing and not any other languages.

Here it is in basic sub routine: position being the sub routine.

position:

randomize

x=(int(rnd(xmax)))

y=(int(rnd(ymax)))

get pixel x,y color r2,g2,b2

if r2=0 then goto position

return

Can anyone do this in Processing because I have tried everything.  
Something sooooo simple can’t be done.

It’s as easy as 2+2 !  
???

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [January 20, 2021, 11:07pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/7 "2021-01-20T23:07:50Z")

</div>

Is this what you are trying to do?

```auto
PGraphics pg;
void setup() {
  size(50, 50);
  pg = createGraphics(width, height);
  pg.beginDraw();
  pg.background(0);
  pg.stroke(255);
  pg.point(pg.width / 2, pg.height / 2);
  pg.endDraw();
  image(pg, 0, 0);
  noLoop();
}

void draw() {
  int rand_x;
  int rand_y;
  while(true) {
    rand_x = (int)random(pg.width);
    rand_y = (int)random(pg.height);
    if (red(pg.get(rand_x, rand_y)) > 0) {
      background(127);
      text(rand_x + ", " + rand_y, 5, height - 5);
      break;
    }
  }
}

```

First, it draws a black square with a white dot in the middle.

It loops, choosing random coordinates until it finds the point.

Finally, it displays the coordinates of the white point on a gray background.

Screen capture of final result:

![Screen Shot 2021-01-20 at 6.04.08 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dd3d77b8fbfb654e595fcd7a187be17409941cbe.png)

_Edited on January 20, 2021 to display the result on a gray background._

---

<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:** [January 21, 2021, 7:10am UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/8 "2021-01-21T07:10:04Z")

</div>

You should not call draw

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [January 21, 2021, 7:41am UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/9 "2021-01-21T07:41:51Z")

</div>

> [@Chrisir](#):
>
> … should not call draw

We could do this:

```auto
PGraphics pg;
void setup() {
  size(400, 400);
  pg = createGraphics(width, height);
  pg.beginDraw();
  pg.background(0);
  pg.stroke(255);
  pg.point(pg.width / 2, pg.height / 2);
  pg.endDraw();
  image(pg, 0, 0);

  int rand_x;
  int rand_y;
  while(true) {
    rand_x = (int)random(pg.width);
    rand_y = (int)random(pg.height);
    if (red(pg.get(rand_x, rand_y)) > 0) {
      background(127);
      text(rand_x + ", " + rand_y, 5, height - 5);
      break;
    }
  }
}

```

It’s fast, and shows the result even before the black background with the white dot in the middle appears.

_Edit (January 21, 2021):_

This will also work, with no `setup()` or `draw()` functions:

```auto
size(100, 100);
PGraphics pg = createGraphics(width, height);
pg.beginDraw();
pg.background(0);
pg.stroke(255);
pg.point(pg.width / 2, pg.height / 2);
pg.endDraw();
image(pg, 0, 0);
int rand_x;
int rand_y;
while(true) {
  rand_x = (int)random(pg.width);
  rand_y = (int)random(pg.height);
  if (red(pg.get(rand_x, rand_y)) > 0) {
    text(rand_x + ", " + rand_y, 5, height - 5);
    break;
  }
}

```

![Screen Shot 2021-01-21 at 7.26.51 AM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/59d2947e0eccaca0924616a5455ef91bcb0aa68d.png)

@IRIGIMA, why is it that you want to use `PGraphics`?

---

<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:** [January 21, 2021, 1:59pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/10 "2021-01-21T13:59:27Z")

</div>

here is my version

recursion is not good for this imho

```auto
PGraphics pg;

float col=10;
int count=0;

int xsize=1500;
int ysize=500;
int tempx;
int tempy;
int temppixel;
int px;
int py;
float pixheight;

int pixel;

int rr; 

void setup() {
  size(1600, 600);

  background(100); 
  noSmooth();
  // colorMode(RGB, 255);
  pg=createGraphics(xsize, ysize);
  // pg.colorMode(RGB, 255);
  pg.noSmooth();
  pg.beginDraw();
  pg.background(0, 0, 0);
  pg.loadPixels();
  pg.fill(255);
  pg.strokeCap(SQUARE);

  // put a point in centre of pg graphics
  int px=xsize/2;
  int py=ysize/2;
  pixel=(px+(py*xsize));
  pg.pixels[pixel] = color(200, 200, 200);
  pg.updatePixels();

  image(pg, 0, 0);

  pg.loadPixels();

  println("start position"); 
  position();
}

// get a point that is not 0
void position() {
  while (true) {
    px=int((random(1, xsize-1)));
    py=int((random(1, ysize-1)));
    pixel=(px+(py*xsize));
    rr =int(red(pg.pixels[pixel]));

    // found? 
    if (rr!=0) {
      // Yes, found 
      println(px);
      return;
    }
  }//while
}

// --------------------------------------------------------------------------------
// main loop

void draw() {

  noFill();
  stroke(255, 0, 0); 
  ellipse(px, py, 19, 19);
  // noLoop();
}
//

```

---

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [January 21, 2021, 6:32pm UTC](https://discourse.processing.org/t/pg-graphics-not-working-bet-you-cant-work-this-out/27250/11 "2021-01-21T18:32:37Z")

</div>

Thank you all for your help!  
Although I use Processing daily for my work - I totally forgot how it’s structured and converting real complex code can be a pain. (This was just a very small part of it)

The initial problem was not a direct problem, but similar to what I needed. So thanks for input. Appreciated.

If interested in what I’m up to - have a look on Reddit.  
r/Irigima

Irigima
