Physics 265
Physics 265
Introductory Scientific Computing
Assignment # 9: Web Computing Applets
Winter 2000, © R H Landau rubin@physics.orst.edu
Due: Wed 10 Mar (in class)
Web Computing Applets
As good and traditional scientists and engineers, we have pretty much
been using Java to write procedural programs, that
is, the type of programs written with conventional languages like
Fortran and C. Our source code has been in .java
files, while the compiled ("byte") code has been in
.class files. We then have run (interpreted)
the .class files by issuing the
java command from a prompt within
a shell.
One of the unique aspects of Java is that compiled .class
files can be run under most any
operating system as well as through a Web browser. If you have
a .class file containing your program
(an "application"), then by modifying only a few lines of
code at the front of the program, you can make your application into
an applet (small application). These applets are
activated to run by a Web browser, and then run on whatever computer
is running the browser. Now this is really quite different from
anything we have done before!
Browsers usually read HTML Hyper Text Markup Language, that is,
files with the suffix .html. Yet if your .class file
is in a public place on the computer and if it is referenced or
called by some .html file (the type that browsers read),
then anyone's browser can be used to run your program.
What's more, when they run your program it will run on their
computer, wherever it may happen to be, and not on the computer
where you happened to have stored your .class. (This is a
good thing because the Physics Department is terribly underfunded
and cannot afford to give out free computer time to entire world.)
To see how this all works (and make more sense of all these
words):
-
Point your browser to: Applet1.html.
This is an HTML file, and when opened
by your browser, it will run a program (Applet1.class)
that plots a graph within a window in your browser.
-
Make sure that your applet has drawn the graph. Pretty, isn't it?
-
Use the View/Page Source buttton to see how simple
the HTML file is. Note where it has a line calling the complied
Java program Applet1.class.
-
Tell your browser to save this file in the appropriate directory
for this week's assignment (it should have the name Applet1.html).
- Point your browser to Applet1.java.
This is the Java source code for the applet to draw a graph.
-
Note how your browser reads this file, lists it on your screen,
but does not compile or execute it.
-
Note how and where the Java program contains statements to
make the program an applet with Abstract Windowing Toolkit:
java.applet.Applet; java.awt.*; class Applet1
extends Applet; Graphics g;
-
Tell your browser to save this file as Applet1.java in
the appropriate directory for this week's work.
-
Applet1.class: This is a compiled,
byte code to be called by your browser as it reads Applet1.html.
-
Look at it, but it won't tell you much as it's not a
text file.
-
To get your own personal copy of Applet1.class, go to the
directory for this week and compile Applet1.java.
-
Have your browser pointed to this week's directory (File/Open
Page/Browse) and open your copy of Applet1.html.
-
This should
plot the graph again, at least if you did all the copies and
compilations correctly.
-
(Note, you can avoid using the browser by instead issuing
the command appletviewer Applet1.html.
This can save time, especially since browsers can be a little
cranky about reloading applets.)
-
Edit the Applet1.html and
Applet1.java files so that they
correspond to a different applet, XYZ, where XYZ is your initials.
-
You will need to compile XYZ.java
to produce XYZ.class, a renaming
of Applet1.class does not work.
-
Check that opening XYZ.html
in your browser draws the graph again.
-
Modify the program XYZ.html so that it also draws an
x axis and a y axis with the graph. It should look like Applet2.html.
-
Modify the source code so that it plots the function
.
|