This is a set of short Java code examples that use the Processing libraries.
These examples may be useful to computer science educators who are teaching a course with the Java language (e.g. AP Computer Science A). Processing provides an easy entry point to computer graphics programming. The Processing libraries are open source and can be used directly from Java programs.
Computer graphics applications are a natural place to use object oriented programming techniques. In addition, concepts such as 2D arrays and recursion can be illustrated nicely with computer graphics.
Processing (Pay attention to where you install it!)
Text editor or Integrated Development Environment (IDE).
This is the most important part, and the biggest headache:
Setting the CLASSPATH environment variable
Assuming you’ve set your CLASSPATH
environment variable correctly, you can
run all the examples on the command line:
javac HelloProcessing.java
java HelloProcessing
PApplet
classHere’s the red pill. When you run a Processing sketch, all of your global
stuff (variables, functions like setup()
, draw()
, keyPressed()
, etc.)
gets embedded in a class, and that class is a subclass of PApplet
. The
PApplet
class contains all the Processing functions that you are familiar
with, like size()
and ellipse()
.
Some things to note:
settings()
method, which is where size()
(or fullScreen()
)
must be called. The settings()
function is called before setup()
.PApplet
class has a static main()
function that you call with the
name of your class.You can write your own Java classes, but you need to give them a reference to
the main PApplet
class so that it can call the Processing drawing functions.
HelloProcessing2.java
Ball.java
Object oriented programming techniques are very useful in computer graphics
programming, e.g. the Ball
class in the HelloProcessing2
example. Here are
some other ideas for illustrating concepts from AP Computer Science A with the
Processing libraries.
2D array application: Image Processing
Recursion application: Fractals
These code examples are licensed under the GPL v2.0, which means you are free to copy and use the code, but if you distribute a modified program, you must also distribute your code.
You can download all the code examples from the Github repository:
https://github.com/dkessner/ProcessingLibraryExamples
Dr. Darren Kessner
Darren.Kessner@marlborough.org
STEM+ Program
Marlborough School, Los Angeles