Creating graphic animations with Processing
Spinning Pictures
The Java application known as Processing can make a computer artist of a non-programmer. We'll show you how to create moving objects and publish a Flash-style applet.
If you have ever tried to program a rotating cube in OpenGL, you have probably experienced hours of debugging fun in the process. The lack of libraries and incorrect variable declarations make life hard on amateur programmers.
Processing
A graphics programming tool called Processing brings graphic effects back to the everyday user. Processing targets artists who have ideas but no computer science degree. The tool is ideal for users who would like to improve the presentation of their data without having to resort to boring charts and monotonous multi-colored pie diagrams.
The Code Swarm [1] project, for example, uses Processing to visualize the development of various open source projects over the course of time. The fascinating results resemble a beehive, which is sometimes quiet and sometimes populated by large numbers of very active bees.
Processing lets you write a simple program and then just press Play; the code either runs or it doesn't. In the latter case, you are given fairly intelligible feedback to help you troubleshoot. If you want to publish your results on the Internet, you can output a ready-made Java applet at the press of a button, and you can upload it to your web space or web server.
Processing, which first saw the light of day around 2001 at MIT Labs, is often compared with Flash; however, unlike Flash, it is an open source project. Instead of Flash plugins, the viewer simply needs a Java plugin for the browser, an extension that is typically installed in next to no time.
Installation
To enjoy the 3D capabilities of the Processing environment, you need either a graphics card with 3D acceleration and driver support or the software 3D engine that comes with the Java application, P3D. The P3D 3D engine needs slightly more in the way of resources than OpenGL, but it is a big help if you do not have a working 3D driver for your graphics card on Linux.
Processing is available for download from the project website [2]. First, unpack the TGZ archive in a folder below your home directory, then pop up a console, change to the directory created in the first step, and enter ./processing to launch the application.
After launching the program, you will see an empty field in which you can start typing code (Figure 1). Why not try it out right way? Just type the program shown in Listing 1 in the window, and then press the icon with the triangle at the top left. Now you should see the object shown in Figure 2 (left).
Listing 1
Drawing a Simple Figure
When you run the code in Listing 1, you should see a small window depicting a series of concentric circles. Click Help | Reference in the Processing GUI for a command reference that gives you a short and terse explanation of the individual functions.
If you prefer a more detailed description, select File | Examples to discover one of the many sample programs included with the Processing files. The code is annotated in all cases.
Also, you can find some books on Processing. The tome by Ira Greenberg [3] that I recommend targets artists and non-programmers.
The first two lines of the sample program in Listing 1 open a 200x200-pixel workspace with a white background (background(255)). If you just enter one value, you are selecting one of 255 possible grayscales from 0 (black) to 255 (white). If you enter three comma-separated values, you are specifying the sRGB color space. A background(255, 0, 0) entry would give you a pure red background, for example. To add a black brush stroke, follow the same approach (stroke(0)).
The noFill() function tells Processing not to fill the figures – otherwise the largest circle would cover all the others. An anti-aliasing effect gives smoother edges and is enabled by the aptly named smooth() keyword, which acts on all the following figures. Next, a for() loop executes a specific function until a stop condition occurs (in this case, when i reaches a value of 200).
The loop starts by setting the integer counter to zero (int i =0) and then incrementing in steps of 10 (i+=10). The loop continues while the counter is less than 200 (i < 200). As soon as it reaches the target value, the loop terminates. Each round executes the ellipse() function in line 7. The function draws a circle with the specifications given in the parentheses. The values 100, 100 position the center of the circle at the center of the 200x200-pixel workspace. The two i variables define the vertical and horizontal diameters of the circle. Because i increments in steps of 10, Processing starts by drawing a circle with a diameter of 10, then 20 pixels, and so on.
Changes
Small changes can have drastic effects in programming. For instance, if you change the second i in the brackets following ellipse and replace it with 100, as in
ellipse(100, 100, i, 100)
you change the circle to an ellipse (Figure 2, right). While the horizontal diameter continues to grow, thanks to the remaining i, the vertical diameter remains constant at 100 pixels.
The next step adds a random element. Replace the for() loop in Listing 1 with the loop in Listing 2.
Listing 2
Changing a Parameter
The new loop introduces a new element with the random variable r. In each round, the line float r = random(200); creates a floating point number (float) between 0 and 200 and stores it in variable r. This continually changing number explains the irregular horizontal distances in Figure 3. Adding a second random number (Listing 3) and leaving the vertical diameter to chance (Figure 4) adds more randomness. The figure looks more spatial, but it is by no means three dimensional.
Listing 3
Adding Randomness
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.