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

Here's the code for the Hands inner class to the NewClock class:

// Class defining the hands on the clock
class Hands extends JPanel
{
  public Hands(int clockDiameter)
  {
    center = new Ellipse2D.Double(-3,-3,6,6);  // Central boss
    hourHand = new Line2D.Double(0,6,0,-clockDiameter*0.25);
    minuteHand = new Line2D.Double(0,8,0,-clockDiameter*0.3);
    secondHand = new Line2D.Double(0,14,0,-clockDiameter*0.35);
    setOpaque(false);  // Set transparent
    }

    // Paint the hands
    public void paint(Graphics g)
    {
      // Get hand angles for the current time
      double secondAngle = seconds*TWO_PI/60;
      double minuteAngle = (secondAngle+minutes*TWO_PI)/60;
      double hourAngle  = (minuteAngle+hours*TWO_PI)/12;

      Dimension size = getSize();
      Graphics2D g2D = (Graphics2D)g;
      g2D.setPaint(CLEAR); // Transparent color
      g2D.fillRect(0,0,size.width,size.height);  // Fill to erase

      g2D.setPaint(Color.darkGray);  // Hands color
      g2D.translate(size.width/2, size.height/2);// Origin to center
      AffineTransform transform = g2D.getTransform();  // Save this xform

      // Draw hour hand
      g2D.setStroke(widePen);  // Use wide pen
      g2D.rotate(hourAngle); // Rotate to hour position
      g2D.draw(hourHand);  // and draw hand

      // Draw minute hand
      g2D.setTransform(transform); // Reset transform
      g2D.rotate(minuteAngle); // Rotate to minute position
      g2D.draw(minuteHand);  // and draw hand

      // Draw second hand
      g2D.setStroke(narrowPen);  // Use narrow pen
      g2D.setTransform(transform); // Reset transform
      g2D.rotate(secondAngle); // Rotate to second position
      g2D.draw(secondHand);  // and draw hand

      g2D.setPaint(Color.white); // Center color
      g2D.draw(center);  // Draw center
    }

    Line2D.Double hourHand;
    Line2D.Double minuteHand;
    Line2D.Double secondHand;
    Ellipse2D.Double center;
    final Color CLEAR = new Color(0,0,0,0);
}

The constructor is quite straightforward. The central boss holding the hands on is the Ellipse2D.Double object, center. This is a circle with a diameter of 6. After defining the lines representing the hands, we call setOpaque() for the panel to make the panel transparent.

The paint() method looks like a lot of code, but after calculating the angular position for each hand, it is just a series of separate drawing operations. The position of each hand is basically a proportion of 2ϖ. Each second or minute contributes one sixtieth of 2ϖ to the position of the corresponding hand and each hour is one twelfth. To the angular position of the minute hand we add the contribution that the angle of the second hand represents as a fraction of a minute, and for the hour hand we add the contribution of the minute hand. After getting the size of the panel, we fill the entire panel with the color CLEAR. This is to erase the previous instance of the hands drawn on the panel. This Color object is defined as a constant member of the class. The first three arguments to the Color constructor define the red, green, and blue color components of the color to be zero. The fourth argument defines something called the alpha component for the color as zero, which defines this color as completely transparent. We will go into the significance of the alpha component in more detail later in this chapter. We'll just use it for now. We want the color to be transparent because the Hands panel will be displayed on top of the ClockFace panel in the applet, and we want the face to be visible.

After moving the origin to the center of the panel we save the current transform. This will enable us to restore this position after rotating the axes to position each hand. Each hand is drawn in essentially the same way. The Stroke for the hand is set, then the axes are rotated to the required angle, and finally we draw the line representing the hand. The last step after drawing the three hands is to draw the central boss in white.

Now we have the inner classes for the face and hands defined, we are ready to complete the applet.

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.