Why does PApplet fail silently when you don't implement draw()?

Looking at the source, this is draw’s implementation in PApplet:

public void draw() {
    // if no draw method, then shut things down
    //System.out.println("no draw method, goodbye");
    finished = true;
}

What’s the point of shutting things down silently here? Since an empty draw() function is valid, this seems pointless and counter-intuitive.

Perhaps PApplet should be an abstract class, and setup() and draw() should be abstract methods. This would help new users understand which methods they should implement. Alternatively, draw() could print a message explaining why the program has been halted. Thoughts?

1 Like

I agree that making PApplet, setup, and draw abstract would clarify requirements for users. But how about us CS teachers who would like to use Processing as an IDE from the beginning of their course, but begin with console input/output before easing into a GUI? A modification of this suggestion would be to keep PApplet as is, but not “shut things down” if the subclass doesn’t override draw. At the very least, please uncomment the helpful error message “no draw method, goodbye” in the current version of draw.

Are you talking about Processing PDE new users, writing in the processing language, or are you talking about Java developers who are importing Processing as a library? I’m asking because there are tutorials and documentation for each, but the documentation is different and use cases are different. Part of the reason PApplet works as it does is because its primary use case is PDE and the Processing to Java preprocessor. New Processing users of this kind don’t use PApplet at all; their code is transpiled to PApplet, and draw is usually overridden either explicitly or implicitly depending on the code style. One of the major design decisions very early on in Processing was that draw() would be optional in the Processing language, but writing a sketch without draw (immediate mode) would result in static rather than looping / animated output.

1 Like

Processing immediate mode / direct mode does this already – you can leave out setup() and draw(), and just write lines, which are assumed to be the contents of a single draw iteration (no loop). So this is a valid Processing sketch:

rect(20, 20, 60, 40);

You might also be interested in the Processing REPL mode!

1 Like

I love this is posted in the Beginners category.

1 Like

I was talking about importing Processing as a library. I didn’t know about the concept of static output, that makes a lot of sense!

1 Like

Sorry about that haha, I’m relatively experienced with Java, but very inexperienced with Processing and the forums… oops

1 Like