Create a Mathlet

Task

Convert the applet from the previous task into a Mumie mathlet. Use a SingleG2DCanvasApplet as applet class and a ControlPanel for your controls. The mathlet must be multilingual and contain a help.

Classes to be used:
  • SingleG2DCanvasApplet, ControlPanel

Applet Skeleton

We change the applet's super class from JApplet to SingleG2DCanvasApplet since this template fits best our needs. Then we set the call super.init() in our init method as the first statement:

public class PolarApplet2 extends SingleG2DCanvasApplet {
//  MathletRuntime runtime = MathletRuntime.createStaticRuntime(null);
  ...
  public void init() {
    super.init();
    ...
  }
}

This ensures that the runtime will be loaded before using e.g. visualisations. Also note that the manual runtime initialisation is no longer necessary.

Next we must change the title because our basic applet uses an own layout:

//getContentPane().add(new JLabel("Polar Coordinates"), BorderLayout.NORTH);
setTitle("Polar Coordinates");

Since our applet class already has a predefined canvas, we use this instance rather than our own:

//canvas = new MMG2DCanvas();
canvas = getCanvas2D();

The applet's layout has also a predefined place for our controls, the ControlPanel. It lies below the canvas and allows a "text editor" like layout. Content in applet classes is added via addControl or addText to the internal ControlPanel:

//JPanel controlPane = new JPanel();
//getContentPane().setLayout(new BorderLayout());
//getContentPane().add(canvas, BorderLayout.CENTER);
//getContentPane().add(controlPane, BorderLayout.SOUTH);
//controlPane.add(point.getAsContainerContent());
//controlPane.add(rho.getAsContainerContent());
//controlPane.add(phi.getAsContainerContent());

addText("Cartesian Coordinates:");
addControl(point.getAsContainerContent());
insertLineBreak();
addText("Rho:");
addControl(rho.getAsContainerContent());
insertTab();
addText("Phi:");
addControl(phi.getAsContainerContent());

We also added some labels and line breaks into the layout.

Reset

We add a reset button to our layout:

addResetButton();

Then we define its action by implementing a reset method:

public void reset() {
  point.setX(2);
  point.setY(2);
  point.invokeUpdaters();
  canvas.renderScene();
}

Here our point's coordinates are set to their initial values again. The dependsOn constructions are updated through the invokeUpdaters call which will adjust the non-cartesian coordinates. At last the scene is rendered again.

Error Handling

Inside a Mumie environment, mathlets can report errors through the integrated quality feedback. Inside a mathlet's class, every possible error can be caught with the following statement surrounding the pieces of code to be secured:

try {
  ...
} catch(Throwable error) {
  reportError(error);
}

We use this for our init and reset methods too.

Multilinguality

A multilingual mathlet can be achieved by replacing a concrete text with a placeholder referencing the text by an unique key. Translating our mathlet means:
  1. create a folder PolarApplet2.files
  2. add a file Messages_en.properties
  3. add a pair key = text for every text in the mathlet
  4. retrieve the texts in the mathlet using the keys
  5. translate the messages file to other languages

For example the applet's title could be defined in the file Messages_en.properties as follows:


applet.title = Polar Coordinates

The current translation can be retrieved using one of the getMessage methods. Our statement for the title changes to:

//setTitle("Polar Coordinates");
setTitle(getMessage("applet.title"));

Help File

Assuming we have a HTML help file for our applet in english, we put it into the PolarApplet2.files folder with the name Help_en.html. Now it will be accessible over the Help button in the mathlet.

Add picture from clipboard (Maximum size: 500 MB)