JavaScript-Interface in Geometria


  [Home] [1 Intro] [2 Resp.-Anal.] [3 JavaScript] [4 JS Interf.] [5 Design] [6 Special] [7 Java-Interf.] [Reference

Content:

 

Allowing JavaScript to call Java functions

To allow JavaScript calling some public methods of an applet, the applet definition must be
extended with the MAYSCRIPT command. Example:

<applet code="Geometria" width="640" height="480" ... name="Geometria" MAYSCRIPT>

Calling a JavaScript-function from GeoScript

To invoke a JavaScript function “Param 1”, one had to use the following GeoScript syntax. The parameters “Param 2”, “Param 3”, etc. are given as string objects to the JavaScript function.

They are also used as parent objects of the JavaScript functions. This means,that the function is every time called, if the parent objects are updated.

Object label

Class name

Subclass name

Object data

<name [string]>

measure

JSfunction

<”Param 1” [string]>,

<”Param 2” [string]>,

<”Param 3” [string]>,

...

 

Defining a JavaScript function in a HTML document

The name of the JavaScript function can be chosen arbitrary. But the return value of the function
can’t be given back directly (this feature is not supported by Internet Explorer),
so there must be defined a global variable result, which is used for the return value.

After calling a JavaScript function, Geometria reads the return value from this JavaScript variable.

 

Getting values from Geometria to JavaScript

To get actual values from the Applet (e. g. coordinates of a special point) one can use the
command document.Geometria.getValue("name","property").

The "name" should be the label of an object, the "property" refers to an object property.
For example document.Geometria.getValue("A","x")
delivers the x-coordinate of the point A.

If one uses the getValue command without defining a property,
like document.Geometria.getValue("name"),
then "name" should be the label of an measure object.

Look at a first Example:

  • Triangle-Center (Coordinates of Point S are calculated by JavaScript) &
    GeoScript-File shows how the JavaScript functions are called.
    The point objects A, B, C are the parents of S and given as parameters.

  • Escher-Puzzle (Figure, Script)

 

Interaction with HTML-elements

If an HTML element (checkbox, button, etc.) changes the value of the JavaScript function,
then Geometria must be invoked to call the JavaScript function again.

This will be reached through document.Geometria.repaint().

Events and event handlers are very important for JavaScript programming. Events are mostly
caused by user actions, for example, if the user clicks on a button a Click-event occurs.

Examples with interaction between Geometria, HTML-elements und JavaScript:

 

Connection between Applets

An interesting possibility of the JavaScript interface is, to connect two or more Applets
within one web-pages.

An example of a connection between two Geometria-Applets is given by the sketch
AParabZeros.html
. In the web-pages are two Applets, which show a real and a complex plane.

The connection is realised in four steps:

  1. The first Applet (script) calls the JavaScript-function updateGeometria2(),
    when one of the controlers a, b, c are changed.

  2. The JavaScript-function updateGeometria2() forces the second Applet
    to update all object data by the JavaScript command document.Geometria2.repaint().

  3. If the second Applet will updated, it calls the JavaScript-function
    getImageCoordinates()
    which delivers some parameters depending
    on the controler values a, b, c.

  4. Depending on these values the position of the zeros in the complex plane in the
    second Applet is updated.

 

Another example, where three Applet are connected, is the sketch Hyperbolic Functions.


Exercises

 

Exercise 1

Extend the sketch Triangle-Center so that S is the Center of a dragable pentagon.

 

Exercise 2

Develop a figure which shows the Zeros of a Circle. The Sketch shoul consist of two Applets.
The first should show a circle with a dragable center and a radius, which is changable by a controller. The second Applet should visualize the zeros in the complex plane.