Reviews : Java Books :
Beginning Java 2- JDK 1.3 Version : Images and Animation

Buy this book
Title: Beginning Java 2- JDK 1.3 Version
ISBN: 1861003668
US Price: $ 49.99
Canadian Price:
C$ 74.95
UK Price: £ 35.99
Publication Date: March 2000
Pages: 1230
© Wrox Press Limited, US and UK.

Beginning Java 2- JDK 1.3 Version
Images and Animation

Initializing the Applet

In the init() method we will need to create the ClockFace object and the Hands object that will make up the clock. We just have to decide how the hands are going to overlay the clock face.

Back in Chapter 12 we saw how a JFrame object had several window panes, including a content pane, to which you typically add the components you want to display, plus a glass pane that overlays the content pane. An object of type JApplet has exactly the same pane structure. We can add an object of type ClockFace to the content pane for the applet, and make the Hands object the glass pane. As long as it's transparent, the content pane with its ClockFace component will be visible underneath the glass pane – which will be our Hands object. In fact in general, a glass pane can be any object of a class that has as a superclass. To replace the default glass pane with your component, you just call the setGlassPane() method for the applet object with a reference to your component as the argument. Here's the code for the init() method that will set our clock up like that:

public void init()
{
  Dimension size = getSize(); // Get the applet size

  // Create the clockface to fit within the applet
  int clockDiameter = Math.min(size.width, size.height)*9/10;
  clock = new ClockFace(clockDiameter);
  getContentPane().add(clock);  // Add clockface to content pane

  hands = new Hands(clockDiameter); // Create the hands panel
  setGlassPane(hands);  // Make the hands the glass pane                  
  hands.setVisible(true);  // Set glass pane visible
}

We get the size of the applet by calling getSize(), and use that to decide the diameter of the clock. Ninety percent of the smaller of the width and height of the applet is a suitable choice to make it fit comfortably. Once we have created the ClockFace object, we just add it to the content pane for the applet. We then create the Hands object and pass it to setGlassPane() to make it the glass pane for the applet. Note that we must call setVisible() for the glass pane because it will be set as invisible by default.

That's all that's necessary to create the visual appearance of the clock. We just need to set it going somewhere, and that's the job of the start() method for the applet object.

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.