Step 4: Life Cycle of a Processing project

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

In Eclipse, the process to build and run an applet is very similar to building and running a Java application.  Just click the run button and make sure that it is trying to run your project as an applet and not as an application.  While this is convenient for us, it is useful to understand some basic differences between Java Applications and Java Applets.

Java applications must have at least one public class that contains a public static void main(String []) method.  Thie main method is where the application's execution begins.  If there is more than one public class which contain a main method, then you will also need to specifiy which class is the application that you wish to run.  The application runs until the end of the main method is reached (or the program crashes).

Java applets are a little different.  Applets do not require a main method. If they do not have a main method they can not be run as a stand-alone application. If an applet does have a main method, then it can be run as an application with execution beginning at the main method as described earlier.

How is an applet executed?

Applet's (without main methods) are actually run by other programs.  For instance an applet viewer can be launched and the applet started by the applet viewer.  Think of this like running a video game by starting the video game controller and then launching the desired game within that system. Correcly configured web browsers can run applets that are embedded in web pages. 

If you wish to configure your web browser to run your applets, read http://java.com/en/download/help/jcp_security.xml for more information on how to run untrusted applets.  You will need to Configure Java to accept untrusted applets from only those sites that you choose.

If you wish to create trusted applets, here is one place to start: http://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm

What is the life cycle of a PApplet?

All Java applets must be iniitialized and started. Once the applet is started, it runs until it is stopped by the user.  In the case of a PApplet, the developer (you) only needs to define the draw() and setup() methods.  Processing handles the rest.  This diagrams shows the order that Java's Applet methods are called by PApplets.

init setup start paint draw (repeats until stopped by user) stop destroy

PApplets execute the setup() method and then repeatedly call the draw() method to continuously update the window.  If your draw() method does not change the position, color, size, etc of any of the items displayed, it will appear as if nothing is happening.  To create movement (animation), you must move your objects on each call to the draw() method. The degree of that objects are changed in each new frame will determine how "fast" the objects appear to be moving or otherwise changing. 

Call the frameRate(int) method to see your objects move slower and faster.

.


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


2015 Deb Deppeler