This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / Add-ons & Links / J/Link / Part 1. Installable Java: Calling Java from Mathematica /

1.2.12 Hosting Applets

J/Link gives you the ability to run most applets in their own window directly from Mathematica. Although this may seem immensely useful, given the vast number of applets that have been created, most applets do not export any useful public methods. They are generally stand-alone pieces of functionality, and thus they benefit little from the scriptability that J/Link provides. Still, there are many applets that may be useful to launch from a Mathematica program.

Note that this section is not about writing applets that use the Mathematica kernel. That topic is covered in Part 2 of this User Guide.

Running applets.

J/Link includes an AppletViewer function for running applets. This function takes care of all the steps of creating the applet instance, providing a frame window to hold it, and starting it running. The first argument to AppletViewer is the fully qualified name of the applet class. The second argument is an optional list of parameters in "name=value" format, corresponding to the parameters supplied to an applet in an HTML page that hosts it. For example, if the <applet> tag in a web page that hosts an applet looks like this:

<applet code="SomeApplet.class" width=400 height=300>

        <param name=foo value=bar>


you would call AppletViewer like this:

You will typically supply at least "WIDTH=" and "HEIGHT=" specifications to control the width and height of the applet window. If you do not specify these parameters, the default width and height are 300 pixels.

An excellent example of an applet that is useful to Mathematica users is LiveGraphics3D, written by Martin Kraus. LiveGraphics3D is an interactive viewer for Mathematica 3D graphics. It gives you the ability to rotate and zoom images, view them in stereo, and more. If you want to try the example below, you will need to get the LiveGraphics3D materials, available from Make sure you put live.jar onto your CLASSPATH before trying the example below, or use the AddToClassPath feature of J/Link to make it available.

First, load the Graphics`Master` package and create the graphic to display. The LiveGraphics3D documentation gives a more general-purpose function for turning a Mathematica graphics expression into appropriate input for the LiveGraphics3D applet but, for many examples, using ToString, InputForm, and N is sufficient:

You specify the image to be displayed via the INPUT parameter, which takes a string giving the InputForm representation of the graphic.

The Live applet has a number of keyboard and mouse controls for manipulating the image. You can read about them in the LiveGraphics3D documentation. Try AltKey-S to switch into a stereo view.

When you are done with an applet, just click the window's close box.

If the applet needs to refer to other files, you should be aware that AppletViewer sets the document base to be the directory specified by the "user.dir" Java system property. This will normally be Mathematica's current directory (given by Directory[]) at the time that InstallJava was called.

Most applets expose no public methods useful for controlling from Mathematica, so there is nothing to do but start them up with AppletViewer and then let the user close the window when they are finished. The Live applet is an exception—it provides a full set of methods to allow the viewpoint, spin, and so on to be modified by Mathematica code. These methods are in the Live class, so to call them we need an instance of the Live class. The way we used AppletViewer earlier does not give us any instance of the applet class. The construction and destruction of the applet instance was hidden within the internals of AppletViewer. You can also call AppletViewer with an instance of an applet class instead of just the class name. This lets you manage the lifetime of the applet instance.

Now we can call methods on the applet instance. See the LiveGraphics3D documentation for the full set of methods. This scriptability opens up lots of possibilities, such as programming "flyby" views of objects, or creating buttons that jump the image into certain orientations or spins.

When we are done, we call ReleaseJavaObject to release the applet instance. This can be done before or after the applet window is closed.