Functions and Arrays
Digital Clocks, HTML Page Parameters - Con't
This advanced digital clock uses GIF digits to display the time. Also we implemented blinking dots, to make it even prettier. To tell which digit GIFs we have to draw, we pass a substring from the string, containing hour, minute or second to a little method which draws the correct digit in the right position for us. Always try to split your code into as many logical functions (methods) as possible!
|
//Sourcecode
import java.awt.*;
import java.applet.*;
//we need to import the util package for the time functions
import java.util.*;
public class Project20 extends Applet implements Runnable
{
Thread runner;
Image Buffer;
Image Digits[];
Graphics gBuffer;
int oldSec, ticker;
boolean dotFlash, flash;
public Image loadImage(String img)
{
Image image=getImage(getDocumentBase(),img);
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image,0);
try{tracker.waitForID(0);}
catch(InterruptedException e){}
return image;
}
public void init()
{
Digits=new Image[12];
//we load our digit images
for(int i=0;i<10;i++)
Digits[i]=loadImage("digits/"+i+".gif");
Digits[10]=loadImage("digits/dot_p.gif");
Digits[11]=loadImage("digits/dot_a.gif");
//create off-screen image we can draw to
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
}
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 50 ms here
try {runner.sleep(50);}
catch (Exception e) { }
repaint();
}
}
public void update(Graphics g)
{
paint(g);
}
public void drawDigit(int digit, int x)
{
gBuffer.drawImage(Digits[digit],x,5,this);
}
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();
if(flash)
{
ticker++;
if(ticker>9)
{
ticker=0;
flash=false;
}
}
if(sec!=oldSec)
{
dotFlash=true;
oldSec=sec;
flash=true;
}
else if(flash==false)
dotFlash=false;
String secStr, minStr, hourStr;
//copy the time integers to our string objects
//if values are less than 10 we add a 0
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;
//fill the background
gBuffer.setColor(new Color(105,0,0));
gBuffer.fillRect(0,0,size().width,size().height);
//draw a thin frame
gBuffer.setColor(new Color(255,105,40));
gBuffer.drawRect(0,0,size().width-1,size().height-1);
//we draw our digits from our method drawDigit
drawDigit(Integer.parseInt(hourStr.substring(0,1)),5);
drawDigit(Integer.parseInt(hourStr.substring(1,2)),35);
drawDigit(Integer.parseInt(minStr.substring(0,1)),72);
drawDigit(Integer.parseInt(minStr.substring(1,2)),102);
drawDigit(Integer.parseInt(secStr.substring(0,1)),139);
drawDigit(Integer.parseInt(secStr.substring(1,2)),169);
//the blinking dots
if(dotFlash)
{
gBuffer.drawImage(Digits[11],65,5,this);
gBuffer.drawImage(Digits[11],132,5,this);
}
else
{
gBuffer.drawImage(Digits[10],65,5,this);
gBuffer.drawImage(Digits[10],132,5,this);
}
//copy the buffer to the screen (no flickering!)
g.drawImage (Buffer,0,0, this);
}
}
|
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.
|