Still with us? Okay, let's reprise what we covered last time:
- We converted our "PeekaBoo" stand-alone application ("App"), into "PeekaBooApplet".
- So, "PeekaBoo", an extension of JFrame, became "PeekaBooApplet" an extension of Japplet.
- Our constructor method was renamed "init()" and was given type void.
- Our "paintComponent" method was renamed "paint".
- We were able to delete the "main" method entirely.
- We then added HTML code to our post to invoke the applet.
- The HTML included a codebase clause to tell our browsers where to look for the applet and data.
- Minimal code changes.
- Create a "mainClass.txt" file to identify the main entry point of the applet.
- Create a DOS batch file "makeJar.bat" to automate the archiving process.
- Modify our HTML to tell our browsers where the archive is stored.
public class PeekaBooAppletJar extends JApplet implements MouseListenerIn our "init()" method, we have to change the pathnames given for the image files from a relative pathname in a file-system to an absolute pathname within the ".jar".
(In practice, this usually just means sticking a "/" at the front.)
images [PEEKA] = loadImage("/Images/peeka.jpg"); images [BOO] = loadImage("/Images/boo.jpg");That's it for code changes - told you they were minimal.
We now create a text file "mainClass.txt" which passes information about the main entry point of our applet into the archive making program ("jar"). This is known as a manifest file.
Main-Class: PeekaBooAppletJarOne point to note there has to be a newline at the end of the "Min-Class:" line. We've no idea why - just stick a blank line at the end of the file.
We next need a batch file - "makeJar.bat" - to automate the process of making the ".jar".
@echo off echo Making PeekaBooAppletJar jar... jar cvmf mainClass.txt PeekaBooAppletJar.jar PeekaBooAppletJar.class Images echo. echo Indexing jar... jar i PeekaBooAppletJar.jar echo done
A ".jar" file is just like a ".zip" file (you can even open a ".jar" with "WinZip").
In our batch file, we're calling the "jar" utility twice. The first call is with flags
- c - create a new ".jar".
- v - verbose output.
- f - write the output to the specified ".jar" filename. (PeekaBooAppletJar.jar".)
- m - include manifest information from the specified manifest file. ("mainClass.txt".)
- i - create an index in the ".jar" of all the jars in the manifest.
One last step is to add an archive clause to our HTML.
<applet code="PeekaBooApplet.class" archive="http://mgt.yourhda.com/Java/PeekaBooAppletJar.jar" codebase="http://mgt.yourhda.com/Java/" width="237" height="315"> </applet>Putting it all together:
- Copy yesterdays "PeekaBooApplet.java".
- Make the above simple changes and save as "PeekaBooAppletjar.java".
- Compile using javac.
- Run makeJar.bat.
- Copy the resulting "PeekaBooAppletJar.jar" to your server.
- Modify the above HTML to point to your server and save to a blog post or ".html" file.
Click on Catwoman and move the mouse away to test.
That's quite enough on applets.
We'll give it a rest for a while. When we come back: playing MP3s in Java.