Step 2: Make MovingRectangle a Processing PApplet

Launch Create draw() setup() Run Extras previous step next step

A Processing project is an applet that inherits (extends) the PApplet class.  The class extends (inherit from) the PApplet class that is defined in the processing.core package. The PApplet class has done a lot of the work of animation for us. It will connecting to the graphic features already available on your computer and provide an infinite loop that repeatedly draws the contents of the applet. All we need to do is learn the processing commands and how to draw figures, text, and other to the applet window. Sounds easy and it is ... at least for simple animations. Let's get started. 

To change from a standard Java application to PApplet we need to add the processing.core package to our project.

1. Add (copy) core.jar to your project.

  1. Click the Project folder (in the Package Explorer pane) to highlight the project.
  2. Click File -> Import.
  3. Expand the General menu
  4. Click File System and then Next
  5. Click Browse
  6. Navigate to the folder that you saved Processing files (or core.jar) and click Open.
    If logged into CS workstation, you can navigate to: S:\processing-2.2.1\amd64_rhel7\core\library
  7. Select the core.jar file (see figure)
    shows location of core.jar and which project it will be imported (copied) to. Click Finish to complete operation.
  8. Select Finish to complete the import (copy) operation.
    If Finish is grayed out, you may need to set the Into Folder field. Click the browse button and select the project folder you created earlier.

The core.jar file has now been copied to your project. 

eclipse window showing HelloProcessingWorld project with a core.jar file added.

2. Add core.jar to the build path

We must now tell Eclipse that this core.jar file should be included in the build path.  The build path is the list of folders that contain existing byte code needed to compile our project.  The standard JRE package is already in our build path and it's easy to add core.jar.

Right-click the core.jar file name in your package explorer view, and find the Build menu, select Add to build path from the Build menu. See figure below.

Voila.  Now, we're ready make our class a Processing applet. At this point, your workspace should like this (or very similar):

eclipse window showing core.jar in the Referenced Libraries section.

3. extends PApplet

  1. Open your MovingRectangle.java file for editing. 
  2. Indicate that you wish for your class to inherit methods from the PApplet class by adding the extends PApplet clause to your class header (before the opening brace). 
        public class MovingRectangle extends PApplet {
    // will put our methods here
    }

    This should cause Eclipse to complain.  The errors are because class PApplet is not in the standard java.lang package.

  3. Add an import statment to your class file.
        import processing.core.*;
    public class MovingRectangle extends PApplet {
    // will put our methods here
    }

As long as there are no red compiler errors, you know that your processing library has been installed correctly. There is not much code, but let's "run" it any way to see what happens.

4. Execute your PApplet

  1. Click the Run button
  2. Click the Run as menu
  3. Click Java Applet

You should find that the Applet Viewer window has popped up.Not much going on yet, but there it is.

empty window with title Applet Viewer

4. When you're done running an applet, click the red close button [X]to end the applet and close the window that was created for it.

The next step will make your applet draw something!  

Launch Create draw() setup() Run Extras previous step next step


2015 Deb Deppeler