advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

JavaBoutique : Articles :

An Illustration of these Techniques

Contents
Introduction
Intro pt 1
Intro pt 2
Intro pt 3
Intro pt 4
An Illustration of these Techniques
An Applet Example
An Active X Example
A Pseudo-Constructor Example
Pseudo-Constructor pt 2
Applet Persists Indefinitely; Breaks into JavaScript
Persistence
Conclusion

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

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs