Custom class that extends PGraphics class not working

I have this custom Button class that extends PGraphics but I can’t figure it out how to make it working, the draw function just do nothing, I inspected the pixels and before sketch(this, x, y);, all pixels are equals to 0 :thinking:

package fr.ayfri.minecraft_art;

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import processing.event.MouseEvent;

import java.util.Arrays;

public abstract class Button extends PGraphics {
	private final int color;
	private final int clickedColor;
	private final int x;
	private final int width;
	private final int height;
	private final int y;
	private String text = "";
	private boolean clicked = false;
	protected final Main sketch;
	
	public Button(Main sketch, final int x, final int y, final int width, final int height) {
		this.sketch = sketch;
		this.x = x;
		this.width = width;
		this.height = height;
		this.y = y;
		color = this.color(200f);
		clickedColor = this.color(100f);
		setSize(getWidth(), getHeight());
		setParent(sketch);
	}
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}
	
	public abstract void onClick();
	
	public void draw() {
		beginDraw();
		if (clicked) {
			background(clickedColor);
		} else {
			background(color);
		}
		stroke(color(0f));
		strokeWeight(4f);
		text(text,
		          (x + width) / 2f - text.length() * textSize,
		          (y + height) / 2f - text.length() * textSize);
		endDraw();
		loadPixels();
		// Here, all the pixels are equals to '0' :/

		sketch.image(this, x, y); 
	}
	
	public void mouseEvent(MouseEvent event) {
		switch (event.getAction()) {
			case MouseEvent.PRESS:
				clicked = true;
				break;
			
			case MouseEvent.RELEASE:
				clicked = false;
				break;
			default:
				throw new IllegalStateException("Unexpected value: " + event.getAction());
		}
	}
	
	public String getText() {
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
	public boolean isClicked() {
		return clicked;
	}
	
	public void setClicked(final boolean clicked) {
		this.clicked = clicked;
	}
}

So how to make it working ?

If the class you’re extending has parameters, you’re gonna need to call super() inside the child’s constructor. Checkout this related question.

I think calling super() is not necessary in this case, see Self contained layers by extending PGraphics

But I did need to call setParent(), setPrimary() and setSize().

Ah looking at the code, I’m not sure the PGraphics can draw itself onto the main canvas. Can’t you call image(theButton, 20.0, 20.0) from whoever owns this Button?

One more thing, if you are using Idea / Eclipse or similar, you could use callbacks as I show in "Variable in variable"/putting a variable in the name behind void / calling methods by name

That way you don’t need to use abstract in your class, you can just tell the button which function to call when it’s pressed. (this would not work in the Processing IDE)

not sure what you are trying to achieve as I get a lot of bugs trying to run the code. However if you want to instance a PGraphics layer for the canvas, here is a template I wrote. Change variables name appropriately.

int w = 1200,h = 600;
PGraphics a,b,c;
someClass s;
void settings(){
  size(w,h,FX2D);
};

void setup(){
  s = new someClass();
};

void draw(){
  s.draw();
};

class someClass{
  PGraphics a,b;
  someClass(){
    setup();
    a = createGraphics(w,h);
    b = createGraphics(w,h);
  };
  
  void setup(){
    
  };
  
  void draw(){
    a.beginDraw();
    a.fill(255);
    a.rect(0,0,w,h/2);
    a.endDraw();
    image(a,0,0);
  };
  
  
};

note that whilst

setup(){
size(a,b,FX2D);
};

settings(){

};

whilst settings and setup can accept FX2D as the renderer, createGraphics(); cant and therefore can only be called with no renderer which defaults to P2D (I think), P2D, or JAVAFX2D. Whilst not necessarily a problem, different renderers have different methods of rendering text and or shapes, which may leave you with a non ideal gui experience.