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());
	public int getWidth() {
		return width;
	public int getHeight() {
		return height;
	public abstract void onClick();
	public void draw() {
		if (clicked) {
		} else {
		          (x + width) / 2f - text.length() * textSize,
		          (y + height) / 2f - text.length() * textSize);
		// 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;
			case MouseEvent.RELEASE:
				clicked = false;
				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(){

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

void draw(){

class someClass{
  PGraphics a,b;
    a = createGraphics(w,h);
    b = createGraphics(w,h);
  void setup(){
  void draw(){

note that whilst




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.