[JAVA] Take a peek at how Processing 3 works

Introduction

In this article, I will explain how the settings (), setup (), and draw () of Processing 3.3.5 (latest as of 7/2) work. It is not a description of the IDE. If you have any mistakes or more interesting information, please let us know in the comments!

How to figure out

https://github.com/processing/processing

This is the official source code for the Processing Development Environment (PDE), the “core” and the libraries that are included with the download.

So I will read the official source code of Processing.

You can read more about how Processing works in core / src / processing.

Definitions of settings (), setup (), and draw ()

All three of these functions are defined in PApplet.java. Let's look at each one.

settings()

  public void settings() {
    // is this necessary? (doesn't appear to be, so removing)
    //size(DEFAULT_WIDTH, DEFAULT_HEIGHT, JAVA2D);
  }

code link

setup()

  public void setup() {
  }

code link

draw()

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

code link

Almost nothing is written ...

The code you are writing in Processing is actually a class that inherits from PApplet. When I run the following code, it returns true.

println(this instanceof PApplet);

Processing is written on the assumption that it will override settings (), setup (), and draw () of PApplet. Writing @Override before the code does not give an error.

Where are each function used?

settings () is called by a function called handleSettings (). Also, setup () and draw () are called by the function handleDraw (). Both are in PApplet.

handleSettings() After this, in PApplet, it will be called in the order ofrunSketch ()main (). As an aside, a boolean variable called ʻinsideSettingsis used in this function. Processing'ssmooth ()andnoSmooth () only work if the variable ʻinsideSettings is true. Note that processing can only be done in the true state in settings (). There was actually a person in need.

code link

handleDraw()

setup () is called when frameCount == 0, anddraw ()is called otherwise. This is also a small story, but since frameCount is a public variable, you can freely change the numerical value. If you do it inside frameCount = -1 anddraw (),setup ()will be called. You won't have a chance to use it, but it's one of the processing vulnerabilities.

code link

AnimationThread

A Thread called AnimationThread is defined in the class PSurfaceNone. handleDraw () is called by callDraw () in it, and continues to loop in run () until the end condition is met by the while statement.

Summary

So far, I have explained how the settings (), setup (), and draw () that we always wrote in Processing work. I hope you are satisfied with this article.

Finally again a small story

handleDraw () is public and can be overridden. Just put the following code in someone else's code and setup () and draw () will not be called.

void handleDraw() {
}

This is also a Processing vulnerability. Is it okay Processing ...

Recommended Posts

Take a peek at how Processing 3 works
How jul-to-slf4j works
How to take a screenshot with the Android Studio emulator
I took a peek at the contents of Java's HashMap
Let's take a look at the Hotspot JVM startup procedure
Let's take a look at the screen of Quant Analyzer!
Take a look at Kotlin from an Effective Java perspective