# Array and PGraphics

**URL:** <https://discourse.processing.org/t/array-and-pgraphics/44344>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 24, 2024, 12:44pm UTC](https://discourse.processing.org/t/array-and-pgraphics/44344 "2024-04-24T12:44:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erif](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/erif/32/15932_2.png) [@Erif](https://discourse.processing.org/u/Erif)\
**Post date:** [April 24, 2024, 12:44pm UTC](https://discourse.processing.org/t/array-and-pgraphics/44344/1 "2024-04-24T12:44:51Z")

</div>

Hi everyone,  
I’m trying to run this PGraphics example where the output is shown in an off-screen graphics buffer (3000x3000 canvas), however it seems the mouseX,mouseY coordinates are being taken from the actual 500x500 canvas. I suspect the render function is missing somewhere.

Here’s the code:

```auto
PGraphics render;
int printWidth = 10;
int printHeight = 10;
int printDpi = 300;
int renderWidth = printWidth * printDpi;
int renderHeight = printHeight * printDpi;

int[] x;
int[] y;

void setup() {
  size(500,500);
  render = createGraphics(renderWidth, renderHeight);
  x = new int[50]; //the most recent 50 MouseX,MouseY values
  y = new int[50];
}

void draw() {
  background(0);
  render.beginDraw();
  render.noStroke();
  render.fill(255,102);

    for(int i = y.length - 1; i > 0; i--) {
      x[i] = x[i-1];
      y[i] = y[i-1];
    }
    
    //add new values to the beginning
    x[0] = mouseX;
    y[0] = mouseY;

    for(int i = 1; i < y.length; i++) {
      render.ellipse(x[i],y[i],i/2,i/2);
    }
  render.endDraw();
  image(render,0,0,width,height);
}

```

Can you help me identify where the problem is? Thanks!

 ![pgraphics](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/2/9237360ec475bc96e48531fc372619695b6c054e.png)

---

<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:** [April 24, 2024, 12:58pm UTC](https://discourse.processing.org/t/array-and-pgraphics/44344/2 "2024-04-24T12:58:01Z")

</div>

Hello @Erif ,

> [@Erif](#):
>
> it seems the mouseX,mouseY coordinates are being taken from the actual 500x500 canvas.

That is correct!

```auto
    x[0] = mouseX;
    y[0] = mouseY;
    
    println(mouseX, mouseY);

```

You could try scaling it up:

```auto
    x[0] = mouseX*6;

```

`:)`

---

<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:** [April 24, 2024, 1:24pm UTC](https://discourse.processing.org/t/array-and-pgraphics/44344/3 "2024-04-24T13:24:24Z")

</div>

Hello @Erif ,

This may be helpful for mouse location outside of sketch window:  
[https://alvinalexander.com/java/java-mouse-current-position-location-coordinates/](https://alvinalexander.com/java/java-mouse-current-position-location-coordinates/)

```auto
import java.awt.Point;
import java.awt.MouseInfo;

Point p;

void setup()
  {
  size(500, 500);
  }

void draw()
  {
  p = MouseInfo.getPointerInfo().getLocation();
  println(p.x, p.y);
  }

```

`:)`

---

<div class="post-metadata">

**Author:** ![Erif](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/erif/32/15932_2.png) [@Erif](https://discourse.processing.org/u/Erif)\
**Post date:** [April 24, 2024, 8:04pm UTC](https://discourse.processing.org/t/array-and-pgraphics/44344/4 "2024-04-24T20:04:50Z")

</div>

Thanks, that’s an easy one and worked pretty well! 😄

---

<div class="post-metadata">

**Author:** ![Erif](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/erif/32/15932_2.png) [@Erif](https://discourse.processing.org/u/Erif)\
**Post date:** [April 24, 2024, 8:05pm UTC](https://discourse.processing.org/t/array-and-pgraphics/44344/5 "2024-04-24T20:05:55Z")

</div>

I need to try this one out as wasn’t aware of this trick. Thanks again!!
