Create a cool form-based Java applet for your website using Processing.

If you already use Processing, you probably know that it's based on Java. You may even be using it as an introduction to the Java programming language, intending to move away from Processing later. However, did you know that Processing is so deeply connected to Java that you can include native Java code in your Processing project, at least in the current version? Here, to demonstrate this, we have a form-based java applet, written in Processing, that uses common graphical user-interface (GUI) labels, text inputs and button controls.

So, what does our example applet do? Well, it calculates the velocity of electromagnetic signals (e.g. radio waves) from an entered apparent permittivity. Don't worry if you don't know what that means: apparent permittivity is just a simple geophysics representation of a signal's velocity and it's very easy to convert between the two using the formula below:

How to calculate signal velocity using an apparent permittivity.

where c is the velocity of light in a vacuum (around 299790000 metres per second). Common apparent permittivities vary between 1 (a vacuum or air) and 80 for water, and for some materials (including those studied in geophysics) can be even higher. As the code has standard Java GUI elements, it's also very suitable for creating your own unit conversion applet if desired. The sample applet also looks much more like a standard Java program than a Processing one, as you'll see in the applet below.

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

If you've previously tried programming applets in Java, the first thing you might notice in the Processing code (which you can download by clicking here) is that we aren't embedding our code in a class that extends the Applet class: Processing will take care of that when we export the applet in the 'File' menu. Also, while we're using a fairly standard Processing structure for our code, we don't have a draw() function, which you might normally expect to see in a Processing program. However, GUI elements don't need continuous redrawing, unlike many of the graphical things Processing is often used for (they only redraw when something changes, such as when we change a label's text string).

Also, 'pure' Processing usually supports just buttons that you have to draw graphically and handle yourself using mouse events. However, by using standard Java GUI code, we are able to create a button that draws and handles itself, with a callback function that we can use to take action when the button is pressed. So, what we end up with is a program that has elements of both Java and Processing, as a sort of hybrid of the two that can be run within the Processing development environment, or exported as an applet for your website.

One final note, however. Many thanks to Robert for pointing out that the Processing code may not work without adding an extra line at the start (import java.awt.*;). That line is now included in the Processing code linked to above and seems to work on Windows and Linux but, if you have problems, try commenting out the first line of code by putting // at the start of the line.