[CSUSB]
>> [CNS]
>> [Comp Sci Dept]
>> [R J Botting]
>> [CSci620]
>>
lab11
[Source]
Goals
Your task is to try some the experiments on this page.
Hints
You can use this page as a model for your own examples of Java Applets.
Demonstration 2
Here
[ test.Henrici.html ]
is a demonstration of something that was easy to program in
Java. Have a look at it.... the graphic is produced by the
Java applet in the HTML page. You can also follow the two links
to the Java source code and to the documentation generated by the
source code.
Note. You don't have to understand why this picture is called an Henrici spiral or what the mathematics means. Its just a formula that generates rather nice looking graphics -- enjoy it.
Your First Java Program
Here you will need to have two windows working at one time: One running
a browser (say Netscape on your workstation) and the other to edit, compile, and
run commands in the Java Development Kit(JDK). The various versions
can be listed by inputing the UNIX command into a command/terminal window:
ls -ld /share/j*and
/share/javais linked to the last of the reliable ones!
In your browser window look at this:
import java.lang.*;
public class Hello {
public static void main(String argv[]){
System.out.println("Hello, World!");
}
}It is a traditional first C program written in Java.
Use your favorite editor to create a copy of the program above in a file called.
Hello.javaNotice: not hello.java, not h.java, but Hello.java. This is important!
Compile it like this:
/share/java1.4.1/bin/javac Hello.javaNote: Do not hold your breath waiting for this to compile, the compiler was very slow when I test it.
javac Hello.javabut this did not work for me recently.
PATH="$PATH:/share/j2sdk1.4.1/bin"
Run the compiled class like this:
/share/java1.4.1/bin/java Hello
Your Second Java Application
Modify your Hello.java program -- using your favorite editor --
to output a message with your name on it.
(use the same name for the file and the class)
Then try out my quickie or Q program to compile,
document and run your program.
Q Hello.java
If your shell reports that 'Q can not be found', then you can
~dick/bin/Q Hello.javainstead.
PATH="$PATH:/share/bin"
A Third Silly Application
This is an application that was used to teach me what a loop
was, back in the 1960s. It also shows how Java Input/Output
works.
The documentation for the "Luv" example is at
[ Luv.html ]
Study the documentation a bit before you get your own copy of the source code [ Luv.java ] Compile and test the application.
A Modern Version
These days users expect to point and click rather than input
text to answer questions. To see how the previous program looks
as an event-driven applet follow this link:
[ test.Luv5.html ]
While there follow the links to the Code and Documentation.
Your First Applet
Your task is to construct a public WWW page that has an applet
that displays some text. The name of the page is:
test.HelloWorld.htmland it should contain the following HTML:
<head><title>Test Hello World</title></head><body>
<APPLET CODE="HelloWorld.class" HEIGHT=150 WIDTH=150>
You can not see this brilliant Java Applet.
</APPLET>
</body>You now need a file called:
HelloWorld.javacontaining
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
public void init() {
setSize(150,25);
}//init
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}//paint
}//HelloWorld
The Applet is compiled just like any other program:
/share/java1.4.1/bin/javac HelloWorld.javaThis will generate a file with "HelloWorld.class". Notice that the compiler forces you to name the file
HelloWorld.javaand the Class
HelloWorldmust be public for it to be used over the World Wide Web.
Alternately you can copy all the *HelloWorld* files to your public directory and view them with Netscape..... BUT make sure it is Netscape 6.* not 4.* on your workstation. Or else try Konqueror or Mozilla (and turn on the Java security and console).
Note
Net
g.setColor(Color.blue);to HelloWorld.java. Compile and then publish to check the result. Make sure you shut netscape down and restart it.... otherwise you'll probably get the OLD cached version:-(
Next change the font in your graphic:
g.setFont(new Font("Helvetica", Font.BOLD, 24));
javac HelloWorld.java
java HelloWorldOnce this runs ok, change the color and form of the graphic string. Make this run.
Edit test.HelloWorld.html so that is has a links to the Source code and documentation after the end of the applet:
<a href="HelloWorld.java">Source Code</A>and publish all the files by copying them all to your public directory:
cp *HelloWorld* /www/public/..../or
cp *HelloWorld* /web/public/..../(depending where your public WWW/Web directory is).
Now use Netscape to look at the page... Follow the links to the code
and then to the documentation, and from there to the documentation
on String.
Tell Me
. . . . . . . . . ( end of section CS620: First Java Laboratory) <<Contents | Index>>
Glossary