Tutorials : Java by Example : Section 4 :
Section Four Contents
Loops, Advanced Color Functions
Random Colors and Arrays
Digital Clocks, HTML Page Parameters

Functions and Arrays

Digital Clocks, HTML Page Parameters - Con't

In the following applet code we introduce parameters, which pass values to your applet from the embedding HTML page. So you can change to behavior, colors etc. of your applet before it is started to custimize it at the user's will. In this applet we make the background color and the foreground color customizable (pass the colors as hexadecimal values, like in HTML code). Additionally you can change the font size (so you can adjust the font to the size of the applet) and whether you prefer 12 hour (U.S.) format or military (European) format. The four applets you can see here use all the same class file, but different parameters. See the sourcecode of this HTML page, to see the details of how this is done.

//Sourcecode

import java.awt.*;
import java.applet.*;
//we need to import the util package for the time functions
import java.util.*;

public class Project19 extends Applet implements Runnable
{
      Thread runner;
      Image Buffer;
      Graphics gBuffer;
      Color bgColor, fgColor;
      int fontSize;
      int format24;

      public void init()
      {
            //create off-screen image we can draw to
            Buffer=createImage(size().width,size().height);
            gBuffer=Buffer.getGraphics();

            //Interprete the parameters from the HTML page

            //read the fontSize parameter
            String clockSizeStr=getParameter("font_size");
            //we have to convert the String to an integer, base is 10 (decimal)
            fontSize=Integer.parseInt(clockSizeStr, 10);

            format24=Integer.parseInt(getParameter("format24"), 10);

            String redStr, greenStr, blueStr;
            int redInt, greenInt, blueInt;

            //read the background color parameter
            String bgColorStr=getParameter("bg_color");
            redStr=bgColorStr.substring(0,2);
            greenStr=bgColorStr.substring(2,4);
            blueStr=bgColorStr.substring(4);

            //during conversion, we convert from hexadecimal (base 16)!
            redInt=Integer.parseInt(redStr, 16);
            greenInt=Integer.parseInt(greenStr, 16);
            blueInt=Integer.parseInt(blueStr, 16);

            bgColor=(new Color(redInt, greenInt, blueInt));

            //read the foreground color parameter
            String fgColorStr=getParameter("fg_color");
            redStr=fgColorStr.substring(0,2);
            greenStr=fgColorStr.substring(2,4);
            blueStr=fgColorStr.substring(4);

            redInt=Integer.parseInt(redStr, 16);
            greenInt=Integer.parseInt(greenStr, 16);
            blueInt=Integer.parseInt(blueStr, 16);

            fgColor=(new Color(redInt, greenInt, blueInt));
      }

      public void start()
      {
            if (runner == null)
            {
                 runner = new Thread (this);
                 runner.start();
            }
      }

      public void stop()
      {
           if (runner != null)
           {
                runner.stop();
                runner = null;
           }
      }

      public void run()
      {
           while(true)
           {
                //halt the thread for 100 ms here
                try {runner.sleep(100);}
                catch (Exception e) { }

                repaint();
           }
      }

      public void update(Graphics g)
      {
           paint(g);
      }

      public void paint (Graphics g)
      {
           //create a new time object
           Date today=new Date();

           //get the seconds, minutes and hours
           int sec=today.getSeconds();
           int min=today.getMinutes();
           int hour=today.getHours();

           String secStr, minStr, hourStr;

           //copy the time integers to our string objects
           //if values are less than 10 we add a 0

           //12 hour US time format with AM and PM
           boolean am;

           if(hour<12)
                         am=true;
           else
                         am=false;

           if(format24==0&&hour>12)
                         hour-=12;

           if(hour<10)
                         hourStr="0"+hour;
           else
                         hourStr=""+hour;

           if(min<10)
                         minStr="0"+min;
           else
                         minStr=""+min;

           if(sec<10)
                         secStr="0"+sec;
           else
                         secStr=""+sec;

           //repaint the background in the background color
           gBuffer.setColor(bgColor);
           gBuffer.fillRect(0,0,size().width,size().height);

           //draw a thin frame in the foreground color
           gBuffer.setColor(fgColor);
           gBuffer.drawRect(0,0,size().width-1,size().height-1);

           String outputString=hourStr+":"+minStr+":"+secStr;

           if(format24==0)
           {
                         if(am==true)
                                     outputString+=" AM";
                         else
                                     outputString+=" PM";
           }

           Font clockFont=new Font("Helvetica", Font.BOLD, fontSize);

           //measure the size of the string to center it
           FontMetrics fm = gBuffer.getFontMetrics(clockFont);
           int stringWidth=fm.stringWidth(outputString);
           int stringHeight=gBuffer.getFontMetrics().getAscent()-fontSize/4;

           //set the font and the foreground color
           gBuffer.setFont(clockFont);
           gBuffer.setColor(fgColor);

           //center the string inside the applet
           gBuffer.drawString(outputString,
                 (size().width-stringWidth)/2, (size().height+stringHeight)/2);

           //copy the buffer to the screen (no flicker!)
           g.drawImage (Buffer,0,0, this);
        }
}

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.