Array and PGraphics

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:

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!

Hello @Erif ,

That is correct!

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

You could try scaling it up:

    x[0] = mouseX*6;

:)

2 Likes

Hello @Erif ,

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

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);
  }

:)

1 Like

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

1 Like

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

1 Like