3. Compiling Your Work

3.1. Files

The following is a listing of the source code directory. As you can see, the sources for the for the GrapherGUI library are all located in one directory. You should place the compiled class files or a copy of the GrapherGUI directory in the same directory as your custom java sources. Our two files are called Web.java and Standalone.java. Notice also that by default, all custom image files should be placed in the same directory.`

bash-2.05b$ ls -al *
-rw-r--r--    1 bshi     users        1906 Jul 10 18:37 Standalone.java
-rw-r--r--    1 bshi     users        1435 Jul 10 15:38 Web.java
-rw-r--r--    1 bshi     users        8726 Jul  4 11:32 axis_x.jpg
-rw-r--r--    1 bshi     users        8850 Jul  4 11:31 axis_y.jpg
-rw-r--r--    1 bshi     users         182 Jul  9 20:01 index.html
-rw-r--r--    1 bshi     users        8804 Jul  4 11:29 param_g.jpg
-rw-r--r--    1 bshi     users        8595 Jul  4 11:30 param_h.jpg

GrapherGUI:
total 53
drwxr-xr-x    2 bshi     users         272 Jul 11 10:12 .
drwxr-xr-x    4 bshi     users         344 Jul 11 10:12 ..
-rw-r--r--    1 bshi     users        1312 Jul  8 11:41 Function.java
-rw-r--r--    1 bshi     users        7938 Jul  9 19:56 GUI.java
-rw-r--r--    1 bshi     users       18670 Jul  9 18:52 Graph.java
-rw-r--r--    1 bshi     users        4590 Jul  9 18:40 ParameterPanel.java
-rw-r--r--    1 bshi     users        3774 Jul  8 12:42 SliderMatrix.java
-rw-r--r--    1 bshi     users         877 Jul  8 14:35 Util.java
			

3.2. Compiling

Using the above directory structure as an example, to compile Web.java, cd into the same directory and use the following command:

javac Web.java

After compiling the Applet, it would be most convenient if we did not have to lug around the whole directory in order to utilize it. Using JAR (the Java archiver), we can package all the class files and data files (just images in this case) into one file. This way, we can easily distribute copies of the program. So after you have compiled the program using javac...

jar Web.jar *.class GrapherGUI/*.class *.jpg

This takes every single class file you have created and the image files you need for your program and packs them into a file called Web.jar. Now the only thing left to do is to create an HTML file into which we will embed the Applet.

Place the following lines anywhere in the body of an HTML page. A simple full example can be found here. (NOTE: you may have to tweak your HEIGHT and WIDTH to get everything to show depending on how large you have set the visual area in GUI. 530x720 will work for most people if you do not change the library code. Consult the source documentation for details.)

<applet
	CODE="Web.class"
	ARCHIVE="Web.jar"
	HEIGHT="720"
	WIDTH="530">
</applet>
			
			
All that needs to be done now is to place your HTML file and JAR file into a directory that is used by your webserver.

3.3. Alternative Organizational Schemes

The example provided in section 1.2 places three classes (the main applet class and two Function classes into a single file. This is not the only way to organize your classes.

If the code for your methods are very long, you can place the code for each of your methods into a separate file. If you do this, remember to make them public:

public class SpecialFunction extends Function {
   // ... code for this class ... 
}
		

If you have a number of functions you wish to reuse, you can organize them into your own personal library of functions. This way, you have quick access to a OK, here we are: collection of functions. To do this, create a directory called what you want the package name to be. Place all your function source files in that directory. If your package is called functionlibrary, then you would add the following line to the beginning of each file in the package directory

import functionlibrary.*;
		
to gain access to the functions included in the library. For more information, consult the Sun documentation on creating packages

3.4. Packaging

There are a number of ways you can archive everything. In order to run a GrapherGUI program or applet, you will need all the GrapherGUI class files and the class files for Function and Applet/Application classes that you have created.

One option is to not use anything packaging system at all. This is not recommended unless you are just testing because javac generates many class files which all need to be up to date in order for the program to run. If you are copying the package and miss one file, the application will fail.

For more information, consult the Sun documentation on JAR.