Applet on a webpage.

advertisement
Applet on a webpage.
NetBeans & Applets
1. If your Applet is named CalcApplet, it is stored in a file called CalcApplet.java, and
when it is compiled, the bytecode (i.e. the intermediate form that can be run in the Java
Virtual Machine (JVM) via a browser) for it is stored in a file called CalcApplet.class.
2. When you run an Applet in NetBeans, it automatically creates an HTML file with the
same name as your Applet. When the Applet in the file named CalcApplet.java is run, a
file named CalcApplet.html is created.
3. NetBeans puts the HTML file it creates in the build directory (right next to the src
directory). This directory also has in it the classes directory, this is where the bytecode
file CalcApplet.class is stored. Since classes is a subdirectory of build the
<Applet...> tag it contains (see next line) works.
<APPLET codebase="classes" code="CalcApplet.class" width=350 height=200></APPLET>
specifies what directory to look in for the Applet (i.e. itʼs path); code= tells the
name of the file the Applet bytecode is in. Notice that in codebase="classes", “classes”
does not start with "homes", or “h:”, or the root of any disk, it is just “classes”. This is
what is known as a relative path. It means the “classes” subdirectory of the current directory. So, the Applet will only start if there is a directory named "classes" (containing a
file named CalcApplet.class) in the directory that the HTML file that contains the <Applet> tag is in.
codebase=
Starting the NetBeans Applet by hand from the web
The complete (absolute) path for the HTML file NetBeans wrote for the prototype calculator I did in class on 4/4 (whose project name was calcProto4_4) is:
http://willamette.edu/~levenick/cs130/examplesFromClass/calcProto4_4/build/CalcApplet.
html
If I enter that in the Address field in a browser, it works just fine. But, how can I link to it
from my home page?
How to put an Applet on your webpage
There are (at least) two ways to launch an Applet from your my page (whose complete
path is: /Volumes/homes/public_html/index.html. Either put a link to its HTML file on my
home page. I can add this line to my index.html file:
<a
href="http://willamette.edu/~levenick/cs130/examplesFromClass/calcProto4_4/build/CalcA
pplet.html "> test Applet</a>
Or I can embed the <Applet> tag directly (so the Applet starts up automagically when
someone visits my page). Here's the line I added:
<APPLET codebase="cs130/examplesFromClass/calcProto4_4/build/classes"
code="CalcApplet.class" width=350 height=200></APPLET>
This embeds the Applet in the web page itself (instead of opening it in a new window).
Changing the size of your Applet on the webpage
If you wish to make the Applet a different size on the webpage, simply modify the
width=350 height=200 in the <applet...> tag.
Updating your Applet
Applets on web pages can fail to update when you change them. You may find that after
you change your Applet, and recompile it in NetBeans, even if you reload the page, it
does not change. This is due (I think) to the browser caching Applets (so as not to reload them every time you visit the page). There may be an easier way to fix this, but one
way that works is to close the browser and restart it... if anyone finds an easier way to
update an Applet, please let me know!
Download