An Illustration of these Techniques
The following code will place a frame on a web page, and then save its position as it is moved, so that it
appears at the altered position when a new page is loaded. This demonstrates persistence.
1. An Applet Example
Place the following code in HTML Page 1:
<HTML>
<HEAD>
</HEAD>
<BODY>
<applet code=Applet1.class
name=Applet1 width=1 height=1>
</applet>
<A HREF="Page2.htm">Page2.htm</A>
</BODY>
</HTML>
Place the following code in HTML Page 2:
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<applet code=Applet1.class
name=Applet1 width=1 height=1 id=Applet1>
</applet>
<A HREF="Page1.htm">Page1.htm</A>
</BODY>
</HTML>
Compiling this code will generate the two files Applet1.class and Constants.class:
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet{
public void init(){
if(Constants.frame == null){
Constants.frame = new Frame();
Constants.frame.reshape((int)Constants.framex.intValue(),
(int)Constants.framey.intValue(),300,300);
}
Constants.frame.show();
}
public void stop(){
Constants.framex = new Integer(Constants.frame.bounds().x);
Constants.framey = new Integer(Constants.frame.bounds().y);
Constants.frame.dispose();
Constants.frame = null;
}
}
abstract class Constants{
static Frame frame;
static Integer framex = new Integer(225);
static Integer framey = new Integer(150);
}
When the applet is loaded, method init() is called. This creates a frame, and places it at the (x,y) position
defined by variables framex and framey. A look at class Constants tells us that these are initialized at 225
and 150 respectively. Suppose that while Page1 is up, the user moves the frame. Method stop() is called
automatically when Page1 is left by the user, and this determines the (x,y) position to which the frame was
moved by the user, and remembers it by changing variables framex and framey. When the page is reloaded,
method init() is called again. It seems obvious that the frame should be placed again at coordinates
(225,150), as defined in class Constants. That is the curious thing, however. The values of variables framex
and framey, when a reload occurs, are those set by method stop() when the previous page was left. The
initialization in class Constants is ignored by a reload. Why? The program is not being restarted; it is
simply being re-activated. That is the essence of persistence as generated by applet reloading.
There will be stability problems with the frame in some browsers, for it is constantly being destroyed and
recreated again. This is associated with the Abstract Windows Toolkit, and is not linked to persistence. The
said instability is present even when the applet is not being continually reloaded, and is caused by the fact
that the applet must interact with a peer, which does the actual work of constructing the frame. The said
instability is removed in three ways: the first is to adjust the order of frame operations so as to minimize
pressure on the peer. The second is to follow every frame operation with a call to pack(). This appears to
place a modal stop on further execution until the peer has completed its task. The third is to introduce delay
loops at critical points. For instance, one might define the method pause(200), in which pause(time) is
defined as follows:
void pause(int time){
long startTime = System.currentTimeMillis();
while(System.currentTimeMillis() < startTime + time){
//infinite loop: time slicing creates a pause
//to give the peer an opportunity to finish
}
Notice in the applet code that the frame name is defined in the abstract class Constants. This is absolutely
essential for stability. Notice also that the class Constants is never instantiated. You can see already, in this
example, that we have started to manipulate an abstract class.
Next ->
Lane Friesen
lanelise@dowco.com
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|