简体   繁体   中英

Processing: Can you draw outside of the Processing Class?

I am trying to organize code to where I can draw outside of the main processing class, However, I am getting a NullPointerExeption whenever I attempt to do so.

public class Processing extends PApplet {

    PApplet p = new PApplet();
    Screen homeScreen = new Screen(new Vector2(0, 0), p);

    public static void main(String[] args) {
        PApplet.runSketch(new String[]{"Processing"},
                new Processing());


    }

    public void settings() {
// Setup
        size(500, 500);

        TextBox t = new TextBox(new Vector2(0, 0), new Vector2(300, 300), new PApplet());
        homeScreen.addContent(t);
    }
}

I am calling the display() method in the TextBox Class shown below.

public void display() {

        // Text Box Body
        p.pushMatrix();
        p.fill(240);
        p.rect(pos.getPos()[0], pos.getPos()[1], dimensions.getPos()[0], dimensions.getPos()[1]);

    }

p is a PApplet Object passed through the constructor. The Exeption is shown below.

java.lang.NullPointerException
    at processing.core.PApplet.pushMatrix(PApplet.java:13149)
    at TextBox.display(Processing.java:154)
    at Screen.display(Processing.java:83)
    at Processing.draw(Processing.java:29)
    at processing.core.PApplet.handleDraw(PApplet.java:2426)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Process finished with exit code 0

If I comment out pushMatrix() the same error occurs, just pointing to the fill() function.

Thanks for the help!

Caution: You are creating multiple instances of PApplet , this will make your code confusing, and you will encounter errors. The Processing class already extends PApplet , so delete this line PApplet p = new PApplet(); and replace it with Processing p = this; or instead of new Screen(new Vector2(0, 0), p); you can use the this keyword like so: new Screen(new Vector2(0, 0), this); - Thanks @sorifiend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM