Version: 01.12.07
26.03.08: A similar experiment with JavaScript is here.
Documentation and updates for BeanShell, © Patrick Niemeyer,can be found at http://www.beanshell.org/, BeanShell is distributed under the terms of the LGPL
Documentation and updates for Geogebra, ©Markus Hohenwarter at www.geogebra.org, GeoGebra is distributed under the terms of the GPL v2
My humble contribution is to make some glue to make these two
wonderful programs cooperate,
which makes it possible to control GeoGebra and manipulate
GeoGebra objects from an interactive scripting environment.
I have made two programs:
The application has to be downloaded and run locally, but a rough demonstration is here. (Bugs, just for ĺ quick test.)
The applet ScriptRunner is demonstrated and documented here.
This picture also gives a hint about how the local application looks:
(Might be neccessary to adjust the path to java.exe in sricpt.bat...)
(Source code: script.src.zip )
Help and information on the BeanShell script language:
This is for the more advanced GeoGebra users, who have some experience with scripting and programming. Some knowledge about Java is also needed.
Personally I think gluing together BeanShell and GeoGebra is a good illustration of what can be done, when programmers, like the GeoGebra and BeanShell developers,
BeanShell uses java syntax, but loose typing is accepted.
a=5; //define variable, loose typing
double a=5.0d; //strict typing
sum(a,b) {return a+b;}; //define function, loose typing
sum(2,3); //use function
sum("a","b"); //again ...
double sum(double a,double b){...} //strict typing
frame=new JFrame("tittel"); //call java-library and make objects frame.setVisible(true); //The whole JDK can be used, one of the largest frameworks available... btn=new JButton("Press"); f.add(btn);
c (){ //define a BS class i=5; //(can also define a Java class with normal, strict Java syntax, s="I am c"; // BS is nearly completely Java compatible and "eats" most Java source code!) method() { print(s); } return this; }
o=c(); //make object from class
print(o.i); //use class
o.m();
for(i=0;i<5;i++){ //loop... print(i); }
if(i>0) {...}else{...} // nothing new...
//switch is expanded to deal with objects! (See documentation BeanShell manual.)
The BeanShell Interpreter has the following internal commands:
print(String); //Outputs to console
show(); //Toggles displaying the results of expressions
source(String filename);//Loads and executes a local BeanShell script file.
pwd(); //Prints working directory
cd(String path); //Change directory
editor(); // Pops up an editor, where you can execute several lines of script at once
...and a lot more...see BeanShell manual.
I have myself added a ggb object
reference for communication with GeoGebra,
in order to do the usual things: (See GeoGebra's JavaScript/Java
Applet API.)
ggb.evalCommand(String); or for convenience, just: evalCmd(String);
ggb.setValue(String,double);
double=ggb.getValue(String);
and so on...
I have also added some other commands:
source(); //Without parameters gives a dialog filechooser, loads and runs BS scripts
wait(long); //wait milliseconds
sourceGgb(String filename); //Loads and executes GeoGebra commands from a local file
sourceGgb(); //Without parameters gives a dialog filechooser
editor(); //A simple editor which can load, edit, save and evaluate both BS-scripts and GGB-commands
(source(), sourceGgb([fn]), editor() can also be run from the buttons in the graphical user interface.)
source(String filename);or "Run as BS-file" in console (or "Eval as BS" in editor)
ggb.evalCommand("S=(3,3)"); ggb.evalCommand("r=5"); ggb.evalCommand("C=Circle[S,r]");
for(i=0;i<5;i++) { obj="P_"+i; //P_1, P_2, ... val="("+i+","+i*i+")"; // point (i,i*i) ggb.evalCommand(obj+"="+val); // Make point in ggb };
evalCmd("t=0"); ggb.setVisible("t",true); for(t=0;t<6.28;t+=0.1){ evalCmd("t="+t); wait(200); };
b=new JButton("ok"); cp=scriptapplet.getContentPane(); cp.add("South",b); scriptapplet.validate();
al=new ActionListener() { actionPerformed() { print("Button pushed!"); }; }; b.addActionListener(al);
f(x)=sin(x) g(x)=cos(x) a=5 P=(3,2) ...can also be put in txt-files and run with:
sourceGgb(Filename) //on BS command line sourceGgb() //picked with a filechooser loaded in the simple editor and run with the "Eval as GGB" button
I hope some of the nice people in the GeoGebra community will find what I have done useful.
I am open for suggestions.
H-P Ulven 20.11.07