Skip to content

Core Library Structure

codeanticode edited this page Mar 19, 2019 · 10 revisions

The Android core library follows very closely the structure of the Java core library. It is organized in six packages:

  • processing.a2d: Classes implementing the default renderer, Android2D: PGraphicsAndroid2D, PSurfaceAndroid2D, etc.
  • processing.android: Classes that wrap the Android components and services required to create a Processing app, wallpaper, or watch face: PFragment, PWallpaper, etc. They should not be used directly from the sketch.
  • processing.core: Basic Processing classes, including PApplet, PGraphics, PImage, and PFont.
  • processing.data: Data handling classes and utilities: Table, XML, JSONObject, etc.
  • processing.event: All related to event handling: Event, KeyEvent, MouseEvent, and TouchEvent.
  • processing.opengl: The classes implementing the OpenGL renderer: PGraphicsOpenGL, PGraphics2D, PGraphics3D, PShader, PGL.

The base PApplet class and Android apps

PApplet, similarly as in Processing Java, is the class that serves as the basis for any sketch. It contains all the methods that define the Processing API. PApplet is not, however, a subclass of Android's Activity class. The reason is for that is because, while regular apps have a main activity, other kinds of apps supported by Processing, like watch faces, live wallpapers, and VR apps, have different main classes, for example services or GVR activities.

PApplet contains a PSurface object that holds the context, activity, and service engine of the app containing the Processing sketch. While all apps have context, regular apps will not have a service engine, and wallpapers and watch faces will not have an activity. The PSurface object in PApplet also hold an AppComponent field that wraps the Android component that contains the Processing sketch in the running app: PFragment for regular apps, PWallpaper for live wallpapers, PWatchFaceCanvas and PWatchFaceGLES for watch faces, and PVR for VR apps. The component is created first when the app is launched, and then Processing stores a reference to it in the AppComponent field in the surface object inside PApplet, together with the context, activity, and service engine, depending on the case.

The renderer object

The renderer is Processing is the module that is in charge of converting the drawing commands present in a sketch into an actual rendering on the device's screen. Every sketch contains a reference to its renderer in a variable called g, of type PGraphics. However, there are different kinds of renderers that are built-in with the core library, and new renderers can be implemented as contributed libraries. The two built-in renderers in the core library are Android2D and OpenGL, with the first using Android's 2D rendering engine, and OpenGL relying on the hardware-accelerated rendering that's made accessible through the OpenGL ES API. Each renderer extends the base class PGraphics and implements its won specific functionality: PGraphicsAndroi2D and PGraphicsOpenGL.