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

Defining the Clock Face

We can base our ClockFace class on JPanel. The clock face will need to be sized to fit within the applet so we will construct a ClockFace object with a given diameter. The basic geometry of the clock face is shown in the diagram.

image5-4

We can create the circular face as a filled circle, which will be an ellipse object of type Ellipse2D.Double with the major and minor axes the same dimension, diameter. We want the hour marks to fit just inside the face and the dimensions shown as a proportion of the diameter will suit. We can create all the hour marks around the face very easily from a single vertical line of the appropriate length at the 12 o'clock position. We just need to repeatedly rotate the axes by one twelfth of 2ϖ and redraw the line at a further eleven positions around the dial. We will define the line for the hour mark as an object of type Line2D.Double. Based on those ideas, here's the definition of the inner class:

// Class defining the static face of the clock
class ClockFace extends JPanel
{
  // Creates a clock face with the given diameter
  public ClockFace(int diameter)
  {
    this.diameter = diameter;
    face = new Ellipse2D.Double(); 
    hourMark = new Line2D.Double(0, -diameter*0.38,
                                 0, -diameter*0.48); 
    setOpaque(false); // Set panel transparent
  }

  public void paint(Graphics g)
  {
    Dimension size = getSize();
    face.setFrame((size.width-diameter)/2,  // Set the size
                  (size.height-diameter)/2, // of the face centered
                    diameter, diameter);  // and of the given diameter

    Graphics2D g2D = (Graphics2D)g;

    // Clear the panel
    g2D.setPaint(CLEAR);  // Transparent color
    g2D.fillRect(0,0,size.width,size.height); // Fill the background
    g2D.setPaint(Color.lightGray);  // Face color
    g2D.fill(face);  // Fill the clock face
    g2D.setPaint(Color.darkGray); // Face outline color
    g2D.setStroke(widePen); // Use wide pen
    g2D.draw(face); // Draw face outline

    // Move origin to center of face
    g2D.translate(size.width/2, size.height/2);

    // Paint hour marks
    for(int i = 0 ; i<12 ; i++)
    {
      if(i%3 == 0)
        g2D.setStroke(widePen);  // Wide pen each quarter position
      else 
        g2D.setStroke(narrowPen);  // otherwise narrow pen

      g2D.draw(hourMark);  // Draw the hour mark
      g2D.rotate(TWO_PI/12.0); // Rotate to next mark
    }
  }

  int diameter;  // Face diameter
  Ellipse2D.Double face; // The face
  Line2D.Double hourMark; // Mark for hours 
}

Note that the various shades of gray used here reflect the needs of printing in the book, rather than any somber character trait on my part. You may like to jazz the example up a bit with your own choice of colors.

The face of the clock will be a filled ellipse with major and minor axes equal. We create a default Ellipse2D.Double object for this in the constructor, because we will set up the dimensions of the ellipse based on the size of the panel in the paint() method. We do this by calling the setFrame() method for the ellipse where the first two arguments are the coordinates of the top-left of the enclosing rectangle, and the last two are the dimensions’ major and minor axes.

In the constructor, we make the hour mark object a vertical line of type Line2D.Double with start and end points such that it fits just within the diameter of the face, and leaves enough room centrally for the hands. We draw the hour marks in the paint() method by applying a transform that rotates the axes by 2ϖ/12 between drawing one mark and the next. The marks on quarters need to be a different thickness to the others and for this we use a couple of BasicStroke objects that defines lines with specific characteristics. Whenever you draw a shape – a line, an ellipse, or whatever, the characteristics of the line that is produced are determined by a Stroke object that is stored within the Graphics2D object. So far we have just been using the default setup, but you can define your own line types. We haven't met this before, so let's take a brief detour into how strokes work.

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.