- Download the Java code Hwa4.java from the Labs section of
the PHY307 homepage. Save it to the directory public_html/PHY307 of
your SUnix account. We will modify this code so that the applet
when loaded will show a simple animation based on the words
Hello World! which will be under control of a button.
- Using pico edit the code Hwa4.java. After the
line private Font newfont; add the line
private Thread runner;
- After the final closing curly brace of the
init() method add the following code which
will constitute a new method start() which
starts the animation
public void start(){
runner = new Thread(this);
runner.start();
}
- Immediately after this add the following code to define a new
method for stopping the animation when you move off the Web page.
public void stop(){
if(runner!=null)
runner.stop();
}
- Finally locate the template for another new method run() located
near the bottom of the code. Type the following code into the main body of
this method.
while(true){
ycoord=ycoord+inc;
if((ycoord==15) || (ycoord==-15))
inc=-inc;
try{Thread.sleep(10);}
catch(InterruptedException e){}
repaint();
}
- Exit the editor and compile the code with javac. If successful
you will have a file Hwa4.class in the same directory. Change the
permissions as usual with the command chmod 644 Hwa4.class.
- Finally, create a file Hwa4.html (perhaps by copying your
Hwa3.html file) to launch the new applet Hwa4.class. Make
sure this new file has the correct permissions.
- Launch Netscape and check it out!