Creating Java AppletsThe Java DriverThe GsharpWE has a Java driver which can convert a graph into a Java draw method. To create this method, select the Java driver, set the filename and then print your graph. The GSL for this looks like:hardcopy.XuNdevice = "ljava"; hardcopy.XuNplotFileName = "gsharp.java"; print;The size of the plot is based on the size of the canvas at the time print is called. The page size is given in mm, The Java driver assumes 1 mm = 0.3 pixel. The page size can be set using the GSL statement: page_1.XuNsize = (400*.3,300*.3);A convenience function make_image() is provided in $UNIDIR/lib/libhtml.gsl to simplify this process. The GSL statements shown above can be reduced to the single function call: make_image(400,300,"gsharp.java");make_image() recognizes the filename extensions .gif, .jpg, .png, and .java and calls the appropriate Gsharp driver. A Simple Java appletHaving generated a Java drawing class with GsharpWE as described above, the following applet, gsharpApplet.java, can be used to view this draw method: import java.applet.*; import java.awt.*; public class gsharpApplet extends Applet { private static Image i = null; public void paint(Graphics g) { if (i == null) { i = createImage(size().width,size().height); gsharp.draw(this, i.getGraphics()); } g.drawImage(i,0,0,this); } }The string "gsharp" in the above code (gsharp.draw) must match the name used as the output file name in the GSL statement make_image(400,300,"gsharp.java"). You should save this program as gsharpApplet.java. The file name must match the name of the class, and names are case sensitive. Your directory should now contain two files: gsharp.java written by GsharpWE and gsharpApplet.java written by you. The Gsharp Java driver requires a number of utility classes which are found in the directory <destination>/GsharpWE/lib/mjava. Before your run your applet, you must create a link to this directory: ln -s <destination>/GsharpWE/lib/mjava mjavaYou can now compile up your Java applet with a Java compiler: javac gsharpApplet.javaThis will create .class files for both of the .java files. Your applet can now be included in an HTML file as follows: <HTML> <TITLE>My First Java Applet from Gsharp</title> <APPLET CODE="gsharpApplet.class" width=400 height=300> </APPLET> </HTML>This HTML file can now be viewed from a browser or with the utility program appletviewer that comes with your Java compiler. Writing Java from GSLYou can either write your own Java code or you can let Gsharp do it for you. Included with GsharpWE is the GSL library libjava.gsl which includes a number of functions for writing Java.The Java files described in the previous section could all be created from the following GSL script: include $UNIDIR/lib/libhtml.gsl include $UNIDIR/lib/libjava.gsl # You can replace the following line with your own graph template code. exec("$UNIDIR/example/Gsharp/flight.gsl"); appname = "graph"; #Create Graph output make_image(400,300,appname+".java"); #Create viewer applet f = fopen(appname+"App.java","w"); javaappletheaders(f); javaawtheaders(f); javastartappletclass(f,appname); fwrite(f,"private static Image i = null;"); javapaintmethod(f,"if (i == null) {"// " i = createImage(size().width,size().height);"// " "+appname+".draw(this,g);"// "}"// "g.drawImage(i,0,0,this);"); javaendclass(f); fclose(f); #Create HTML file f = fopen(appname+".html","w"); HTMLheader(f,"My First Java Applet from Gsharp","Anon"); HTMLapplet(f,appname+"App.class",400,300); HTMLfooter(f); fclose(f); #Create *.class system("javac "+appname+"App.java"); As well as using GSL to create the source code for a new applet, you may also use it to export Gsharp datasets for use as static variables by your applet. The functions javaexportfloat() and javaexportint() are included in libjava.gsl and will export float and integer variables of any dimension. Client-side InteractivityA complete Java applet example is provided in the example profiles. This example shows client-side interactivity using a mousedrag method. It also includes code to pass data from GSL to the applet for local manipulation. |