Riferimenti:API delle app GeoGebra

Da GeoGebra Manual.

Questo documento descrive i metodi pubblici disponibili in geogebra.GeoGebraApplet e utilizzabili in una pagina HTML con JavaScript.

Esempi

Esempio 1: il seguente frammento di codice HTML aggiunge un pulsante "Ripristina" a una pagina HTML, in modo da consentire all'utente il ripristino della costruzione allo stato iniziale:

 <form>
   <input type="button" value="Ripristina" onclick="document.applets[0].reset();">
 </form>

Esempio 2: questo codice aggiunge i pulsanti "Nascondi A" e "Mostra A", per modificare lo stato di visibilità di un oggetto di nome "A" presente nella costruzione:

 <form>
   <input type="button" value="Nascondi A" onclick="document.applets[0].setVisible('A', false);">
   <input type="button" value="Mostra A" onclick="document.applets[0].setVisible('A', true);">
 </form>

Esempio 3: una funzione JavaScript "unaPiccolaCostruzione()" è utilizzata per chiamare alcuni metodi di GeoGebraApplet. Nota: la lingua è impostata in Italiano ("it"), utilizzando il parametro "language" nelle tag dell'applet, per fare in modo che sia riconosciuto il comando Retta nella versione italiana.

 <applet name="ggbApplet" code="geogebra.GeoGebraApplet"
   archive="geogebra.jar"
   width=200 height=40>
 <param name="filename" value="circonf.ggb">
 <param name="framePossible" value="false">
 <param name="language" value="it">
 Installare <a href="http://www.java.com"> Java 1.4.2 </a> (o successivi) per utilizzare questa pagina.
 </applet>
 <script type="text/javascript">
   function miaPiccolaCostruzione() {
     var applet = document.ggbApplet;
     applet.evalCommand("A = (1,1)"); 
     applet.evalCommand("B = (3,2)"); 
     applet.evalCommand("s = Retta[A, B]"); 
   }
 </script>
 <form>
   <input type="button" value="Esegui costruzione" onclick="miaPiccolaCostruzione();">
 </form>

Esempio 4: Date un'occhiata al codice sorgente del seguente foglio di lavoro, che utilizza JavaScript.

 http://www.geogebra.org/source/program/applet/geogebra_applet_javascript_test.htm

Esempio 5: Questo foglio di lavoro utilizza un listener add in modo da verificare se è già stata completata una precedente costruzione (nel cui caso appare la scritta "Well done").

 http://www.geogebra.org/en/examples/javascriptAutomaticCheckingExercise.html

Metodi disponibili

Segnatura del metodo Da Descrizione
boolean evalCommand(String cmdString) 3.0 Valuta la stringa come se fosse digitata direttamente nella barra di inserimento di GeoGebra. Restituisce se la valutazione del comando ha avuto successo.
A partire da GeoGebra 3.2 è possibile passare più comandi contemporaneamente, separandoli con \n.
Nota: utilizzare il language parameter per fare in modo che siano applicati i comandi localizzati nella lingua corrente.
void setUndoPoint() 3.2 Imposta un punto di annullamento. Utile se si vuole fare in modo che l'utente possa annullare l'azione di evalCommand, ad esempio nel caso in cui sia stato creato un pulsante HTML che agisce da strumento personalizzato

Con questi metodi è possibile implementare la comunicazione da Applet a JavaScript. Utilizzando uno dei seguenti metodi, assicurarsi di avere inserito l'opzione MAYSCRIPT al termine della prima riga del tag dell'applet. Ad esempio:

 <applet name="ggbApplet" code="geogebra.GeoGebraApplet" codebase="." archive="geogebra.jar" width="500" height="250" MAYSCRIPT>

L'opzione MAYSCRIPT è necessaria solo per le comunicazioni da GeoGebraApplet a JavaScript; non è invece richiesta affinché JavaScript possa accedere ai metodi di GeoGebraApplet.

Ad esempio, i seguenti metodi possono essere utilizzati per:

Impostare lo stato degli oggetti

Metodi generali

Segnatura del metodo Da Descrizione
void deleteObject(String objName) 2.7 Elimina l'oggetto avente il nome indicato.
void setValue(String objName, double value) 3.2 Imposta il doppio valore dell'oggetto avente il nome indicato. Nota: se l'oggetto specificato è una variabile booleana, utilizzare il valore 1 per impostarlo come true e un qualsiasi altro valore per impostarlo comefalse. Per oggetti di tipo diverso, non si ha alcun effetto.
void setCoords(String objName, double x, double y) 3.0 Imposta le coordinate dell'oggetto avente il nome indicato. Nota: se l'oggetto specificato non è un punto o un vettore, non si ha alcun effetto.
void setColor(String objName, int red, int green, int blue) 2.7 Imposta il colore dell'oggetto avente il nome indicato.
void setVisible(String objName, boolean visible) 2.7 Imposta o nasconde nella Vista Grafica l'oggetto avente il nome indicato.
void setLabelVisible(String objName, boolean visible) 3.0 Imposta o nasconde nella Vista Grafica l'etichetta dell'oggetto avente il nome indicato.
void setLabelStyle(String objName, boolean visible) 3.0 Imposta lo stile dell'etichettatura nella Vista Grafica dell'oggetto indicato. Gli stili di etichettatura disponibili sono NAME = 0, NAME_VALUE = 1, VALUE = 2 e (da GeoGebra 3.2) CAPTION = 3
void setFixed(String objName, boolean flag) 3.0 Imposta l'oggetto indicato come fissato. Nota: gli oggetti fissati non possono essere modificati.
void setTrace(String objName, boolean flag) 3.0 Attiva o disattiva la traccia dell'oggetto indicato.
boolean renameObject(String oldObjName, String newObjName) 3.2 Rinomina oldObjName in newObjName. Indica se l'operazione ha avuto successo
void setLayer(String objName) 3.2 Imposta il livello dell'oggetto
void setLayerVisible(int layer, boolean visible) 3.2 Mostra o nasconde tutti gli oggetti appartenenti al livello indicato
void setLineStyle(String objName, int style) 3.2 Imposta lo stile della linea dell'oggetto indicato (da 0 a 4)
void setLineThickness(String objName, int thickness) 3.2 Imposta lo spessore dell'oggetto (da 1 a 13, -1 è l'impostazione predefinita)
void setPointStyle(String objName, int style) 3.2 Imposta lo stile dei punti (-1 predefinito, 0 pallino pieno, 1 pallino vuoto, 2 croce, 3 più, 4 rombo pieno, 5 rombo vuoto, 6 triangolo (nord), 7 triangolo (sud), 8 triangolo (est), 9 triangolo (ovest))
void setPointSize(String objName, int size) 3.2 Imposta le dimensioni di un punto (da 1 a 9)
void setFilling(String objName, double filling) 3.2 Imposta il riempimento di un oggetto (da 0 a 1)

Animazione automatica

Segnatura del metodo Da Descrizione
void setAnimating(String objName, boolean animate) 3.2 Imposta la possibilità di animare un oggetto. Per avviare l'animazione utilizzare startAnimation().
void setAnimationSpeed(String objName, double speed) 3.2 Imposta la velocità di animazione di un oggetto.
void startAnimation() 3.2 Avvia l'animazione automatica di tutti gli oggetti con impostata una flag di animazione, vedere setAnimating()
void stopAnimation() 3.2 Interrompe l'animazione di tutti gli oggetti con impostata una flag di animazione, vedere setAnimating()
boolean isAnimationRunning() 3.2 Restituisce se un'animazione automatica è attiva o no.

Ottenere lo stato degli oggetti

Segnatura del metodo Da Descrizione
double getXcoord(String objName) 2.7 Restituisce l'ascissa dell'oggetto avente il nome indicato. Nota: restituisce 0 se l'oggetto non è un punto o un vettore.
double getYcoord(String objName) 2.7 Restituisce l'ordinata dell'oggetto avente il nome indicato. Nota: restituisce 0 se l'oggetto non è un punto o un vettore.
double getValue(String objName) 3.2 Restituisce il doppio valore dell'oggetto indicato (ad es. la lunghezza di un segmento, l'area di un poligono). Nota: restituisce 1 se l'oggetto è una variabile booleana a valore true. In ogni altro caso restituisce 0.
String getColor(String objName) 2.7 Restituisce il colore dell'oggetto indicato sotto forma di stringa esadecimale, ad es. "#FF0000" è il rosso. La stringa esadecimale inizia sempre con # e non contiene lettere minuscole.
boolean getVisible(String objName) 3.2 Restituisce true o false a seconda che l'oggetto sia visibile o meno nella Vista Grafica. Restituisce false se l'oggetto non esiste.
String getValueString(String objName) 2.7 Restituisce una stringa contenente il valore dell'oggetto indicato.
String getDefinitionString(String objName) 2.7 Restituisce una stringa contenente la definizione dell'oggetto indicato.
String getCommandString(String objName) 2.7 Restituisce una stringa contenente il comando dell'oggetto indicato.
String getObjectType(String objName) 2.7 Restituisce una stringa contenente il tipo dell'oggetto indicato (come"punto", "retta", "circonferenza", ecc.).
boolean exists(String objName) 2.7 Restituisce se un oggetto avente il nome indicato esiste o meno nella costruzione.
boolean isDefined(String objName) 2.7 Restituisce se il valore dell'oggetto indicato è correntemente valido.
String [] getAllObjectNames()
Sconsigliato dalla v. 3.0
2.7 Restituisce una matrice contenente i nomi di tutti gli oggetti presenti nella costruzione. Nota: l'utilizzo di matrici in JavaScript crea problemi con alcuni browser. Utilizzare preferibilmente getObjectNumber() e getObjectName().
int getObjectNumber() 3.0 Restituisce il numero di oggetti nella costruzione.
String getObjectName(int i) 3.0 Restituisce il nome dell'n-esimo oggetto della costruzione.
String getLayer(String objName) 3.2 Restituisce il livello a cui appartiene l'oggetto.
int getLineStyle(String objName) 3.2 Rileva lo stile della linea dell'oggetto indicato (da 0 a 4)
int getLineThickness(String objName) 3.2 Rileva lo spessore della linea (da 1 a 13)
int getPointStyle(String objName) 3.2 Rileva lo stile dei punti (-1 predefinito, 0 pallino pieno, 1 pallino vuoto, 2 croce, 3 più, 4 rombo pieno, 5 rombo vuoto, 6 triangolo (nord), 7 triangolo (sud), 8 triangolo (est), 9 triangolo (ovest))
int getPointSize(String objName) 3.2 Rileva la dimensione di un punto (da 1 a 9)
double getFilling(String objName) 3.2 Rileva il riempimento di un oggetto (da 0 a 1)

Costruzione / Interfaccia utente

Segnatura del metodo Da Descrizione
void setMode(int mode) 2.7 Imposta la modalità mouse (cioè come strumento) nella Vista Grafica (vedere Riferimenti: barra degli strumenti e i parametri dell'applet "showToolBar" and  "customToolBar" )
void openFile(String strURL) 2.7 Apre una costruzione a costruzione da un file Web (indicato da una stringa con URL assoluto o relativo)
void reset() 2.7 Ricarica la costruzione iniziale (indicata nel parametro filename) dell'applet corrente.
void refreshViews() 2.7 Aggiorna tutte le Viste. Nota: tutte le tracce presenti nella Vista Grafica saranno eliminate.
void setOnTheFlyPointCreationActive(boolean flag)
3.2 Attiva (true) o disattiva (false) la creazione immediata di punti nella Vista Grafica. Nota: è utile quando si vuole evitare la creazione di punti come "effetto collaterale" di uno strumento. Ad esempio, quando questa flag è false, lo strumento "Retta - per due punti" non crea punti quando si fa clic nella Vista Grafica.
void hideCursorWhenDragging(boolean flag)
3.2 Nasconde (true) o mostra (false) il cursore del mouse (puntatore) quando si trascina un oggetto per modificare la costruzione.
void setRepaintingActive(boolean flag)
2.7 Attiva (true) o disattiva (false) il repaint dell'applet. Nota: questo metodo è efficiente specialmente quando vengono invocati più metodi.
void setErrorDialogsActive(boolean flag) 3.0 Attiva (true) o disattiva (false) la visualizzazione di finestre di dialogo relative agli errori. Nota: molto utile se utilizzato assieme a evalCommand().
void setCoordSystem(double xmin, double xmax, double ymin, double ymax) 3.0 Imposta il sistema di coordinate cartesiane della Vista Grafica.
void setAxesVisible(boolean xAxis, boolean yAxis) 3.0 Mostra o nasconde gli assi x e y del sistema di coordinate della Vista Grafica.
void setGridVisible(boolean flag) 3.0 Mostra o nasconde la griglia della Vista Grafica.

Comunicazione tra GeoGebraApplet e JavaScript

I seguenti metodi consentono l'implementazione della comunicazione tra Applet e JavaScript. Assicurarsi di avere inserito l'opzione MAYSCRIPT al termine della prima riga di tag dell'applet ogni volta che si utilizzano i metodi indicati di seguito. Ad esempio:

<applet name="ggbApplet" code="geogebra.GeoGebraApplet" codebase="." archive="geogebra.jar" width="500" height="250" MAYSCRIPT>. L'opzione MAYSCRIPT is è necessaria solo per la comunicazione da GeoGebraApplet a JavaScript; non è necessaria a JavaScript per accedere i metodi di GeoGebraApplet. For example, these methods can be used to:

Method Signature Since Description
void registerAddListener(String JSFunctionName) 3.0 Registers a JavaScript function as an add listener for the applet's construction. Whenever a new object is created in the GeoGebraApplet's construction, the JavaScript function JSFunctionName is called using the name of the newly created object as its single argument.

Example: First, register a listening JavaScript function:

ggbApplet.registerAddListener("myAddListenerFunction");

When an object "A" is created, the GeoGebra Applet will call the Javascript function

myAddListenerFunction("A");
void unregisterAddListener(String objName) 3.0 Removes a previously registered add listener, see registerAddListener()
void registerRemoveListener(String JSFunctionName) 3.0 Registers a JavaScript function as a remove listener for the applet's construction. Whenever an object is deleted from the GeoGebraApplet's construction, the JavaScript function JSFunctionName is called using the name of the deleted object as its single argument. Note: when a construction is cleared, remove is not called for every single object, see registerClearListener().

Example: First, register a listening JavaScript function:

ggbApplet.registerRemoveListener("myRemoveListenerFunction");

When the object "A" is deleted, the GeoGebra Applet will call the Javascript function

myRemoveListenerFunction("A");
void unregisterRemoveListener(String objName) 3.0 Removes a previously registered remove listener, see registerRemoveListener()
void registerUpdateListener(String JSFunctionName) 3.0 Registers a JavaScript function as a update listener for the applet's construction. Whenever any object is updated in the GeoGebraApplet's construction, the JavaScript function JSFunctionNameis called using the name of the updated object as its single argument. Note: when you only want to listen for the updates of a single object use registerObjectUpdateListener() instead.

Example: First, register a listening JavaScript function:

ggbApplet.registerUpdateListener("myUpdateListenerFunction");

When the object "A" is updated, the GeoGebra Applet will call the Javascript function

myUpdateListenerFunction("A");
void unregisterUpdateListener(String objName) 3.0 Removes a previously registered update listener, see registerUpdateListener()
void registerObjectUpdateListener(String objName, String JSFunctionName) 3.0 Registers a JavaScript function as an update listener for a single object. Whenever the object with the given name is updated, the JavaScript function JSFunctionNameis called using the name of the updated object as its single argument. If objName previously had a mapping JavaScript function, the old value is replaced. Note: all object updated listeners are unregistered when their object is removed or the construction is cleared, see registerRemoveListener() and registerClearListener().

Example: First, register a listening JavaScript function:

ggbApplet.registerObjectUpdateListener("A", "myAupdateListenerFunction");

Whenever the object A is updated, the GeoGebra Applet will call the Javascript function

myAupdateListenerFunction("A");

Note: an object update listener will still work after an object is renamed.

void unregisterObjectUpdateListener(String objName) 3.0 Removes a previously registered object update listener of the object with the given name, see registerObjectUpdateListener()
void registerRenameListener(String JSFunctionName) 3.0 Registers a JavaScript function as a rename listener for the applet's construction. Whenever an object is renamed in the GeoGebraApplet's construction, the JavaScript function JSFunctionName is called using the old name and the new name of the renamed object as its two arguments.

Example: First, register a listening JavaScript function:

ggbApplet.registerRenameListener("myRenameListenerFunction");

When an object "A" is renamed to "B", the GeoGebra Applet will call the Javascript function

myRenameListenerFunction("A", "B");
void unregisterRenameListener(String objName) 3.0 Removes a previously registered rename listener, see registerRenameListener()
void registerClearListener(String JSFunctionName) 3.0 Registers a JavaScript function as a clear listener for the applet's construction. Whenever the construction in the GeoGebraApplet is cleared (i.e. all objects are removed), the JavaScript function JSFunctionName is called using no arguments. Note: all update listeners are unregistered when a construction is cleared. See registerUpdateListener() and registerRemoveListener().

Example: First, register a listening JavaScript function:

ggbApplet.registerClearListener("myClearListenerFunction");

When the construction is cleared (i.e. after reseting a construction or opening a new construction file), the GeoGebra Applet will call the Javascript function

myClearListenerFunction();
void unregisterClearListener(String JSFunctionName) 3.0 Removes a previously registered clear listener, see registerClearListener()

GeoGebra's XML format

With these methods you can set everything in a construction (see XML Reference ).

Method Signature Since Description
void evalXML(String xmlString) 2.7 Evaluates the given XML string and changes the current construction. Note: the construction is NOT cleared before evaluating the XML string.
void setXML(String xmlString) 2.7 Evaluates the given XML string and changes the current construction. Note: the construction is cleared before evaluating the XML string. This method could be used to load constructions.
String getXML()
2.7 Returns the current construction in GeoGebra's XML format. This method could be used to save constructions.
String getXML(String objName)
3.2 Returns the GeoGebra XML string for the given object, i.e. only the <element> tag is returned.
String getAlgorithmXML(String objName)
3.2 For a dependent GeoElement objName the XML string of the parent algorithm and all its output objects is returned. For a free GeoElement objName "" is returned.

Miscellaneous

Method Signature Since Description
String evalMathPiper(String string) 3.2 Passes the string to MathPiper and returns the result as a String.
String getIPAddress() 3.2 Returns the IP address of the local computer as a String eg "192.168.0.4"
String getHostname() 3.2 Returns the hostname of the local computer as a String
void debug(String string) 3.2 Prints the string to the Java Console
© 2024 International GeoGebra Institute