stardate


import java.awt.*;
import java.applet.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.lang.Math;
import java.lang.Integer;

/**
Copyright by Konstantin Priblouda 1998
This is a free software covered by GNU GPL version 2 or later
Please read this license before using this software
at http://www.gnu.org
Applet displays current stardate
*/

public class stardate extends Applet implements Runnable {


 Image buffer;

 Graphics doublebuffer;
 int height, width;
 String beforePoint,afterPoint;
 boolean preWarp = false;
 Thread mythread;
 Image glyphs[],beforeWarp,point,Background;
 /*
   specify interval between updates ( roughly )
   do not trash target system */
 static final int RRATE = 1000;
 /*
    gregorian date of descent of warp technilogy -
    zero point in stardate calculation
 */
 GregorianCalendar warpStart= new GregorianCalendar(2323,0,1);

 public void init () {

  showStatus("loading glyphs...");

  // load images
  point = this.getImage(this.getCodeBase(),"stardateP.gif");
  beforeWarp = this.getImage(this.getCodeBase(),"stardateBW.gif");
  Background= this.getImage(this.getCodeBase(),"stardateBG.gif");


  glyphs= new Image[10];
  glyphs[0] = this.getImage(this.getCodeBase(),"stardate0.gif");
  glyphs[1] = this.getImage(this.getCodeBase(),"stardate1.gif");
  glyphs[2] = this.getImage(this.getCodeBase(),"stardate2.gif");
  glyphs[3] = this.getImage(this.getCodeBase(),"stardate3.gif");
  glyphs[4] = this.getImage(this.getCodeBase(),"stardate4.gif");
  glyphs[5] = this.getImage(this.getCodeBase(),"stardate5.gif");
  glyphs[6] = this.getImage(this.getCodeBase(),"stardate6.gif");
  glyphs[7] = this.getImage(this.getCodeBase(),"stardate7.gif");
  glyphs[8] = this.getImage(this.getCodeBase(),"stardate8.gif");
  glyphs[9] = this.getImage(this.getCodeBase(),"stardate9.gif");

  /* used deprecated API to be polite to pre 1.1.6 java */
  width = bounds().width;
  height= bounds().height;

  buffer = createImage(width,height);
  doublebuffer = buffer.getGraphics();

  showStatus("done...");
 }

 public void paint (Graphics g) {


  computeStardate();

  // draw rectangle filled white
  //doublebuffer.setColor(Color.white);
  //doublebuffer.fillRect(0,0,width,height);

  // draw background image
  doublebuffer.drawImage(Background,0,0,this);

  int i,index;
  int cumulativeWidth = 0;
  for(i=0; i < beforePoint.length(); i++) {
     index = Character.digit(beforePoint.charAt(i),10);
     doublebuffer.drawImage(glyphs[index],cumulativeWidth,0,this);
     cumulativeWidth = cumulativeWidth + glyphs[index].getWidth(this);
  }
  doublebuffer.drawImage(point,cumulativeWidth,0,this);
  cumulativeWidth = cumulativeWidth + point.getWidth(this);
  // draw after decimal point
  for(i=0; i < afterPoint.length(); i++) {
     index = Character.digit(afterPoint.charAt(i),10);
     doublebuffer.drawImage(glyphs[index],cumulativeWidth,0,this);
     cumulativeWidth = cumulativeWidth + glyphs[index].getWidth(this);
  }
  if(preWarp) {
     doublebuffer.drawImage(beforeWarp,cumulativeWidth,0,this);
  }


  g.drawImage(buffer,0,0,this);

 }

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

/**
	compute current stardate
  	mybe just subclass GregorianCalendar ?
*/
  public void computeStardate () {
  int YY;
  double T,DDD;
  GregorianCalendar current = new GregorianCalendar();

  YY = current.get(Calendar.YEAR) - warpStart.get(Calendar.YEAR);

  if(current.before(warpStart)) {
	preWarp= true;
	YY = Math.abs(YY);
  }

  if(current.isLeapYear(current.get(Calendar.YEAR))) {
	DDD = (current.get(Calendar.DAY_OF_YEAR)-1)/366.0;
  }
  else {
	DDD = (current.get(Calendar.DAY_OF_YEAR)-1)/365.0;
  }
  T = (current.get(Calendar.HOUR_OF_DAY) * 3600.0 +
		         current.get(Calendar.MINUTE)*60.0 +
			 current.get(Calendar.SECOND)) / 86400.0;
  /* now we compose our strings - chop first 2 chars start of year fraction*/
  beforePoint = Integer.toString(YY) + Double.toString(DDD).substring(2);
  /* trim string to 6 chars */
  if(beforePoint.length() > 6)
	beforePoint = beforePoint.substring(0,6);
  /* the same - also specify desired precision */
  afterPoint =  Double.toString(T).substring(2);
  if(afterPoint.length() > 5)
	afterPoint = afterPoint.substring(0,5);

}




 public void start () {


 showStatus("Stardate syncronized...");
  mythread = new Thread(this);
   if (mythread != null) {
    mythread.start();
   }
 }

 public void run () {
  while (true) {
   repaint();
    try {
     Thread.sleep(RRATE);
    } catch (Exception exc) {};
   }
 }

 public void stop () {

  showStatus("Quitting applet");
   if (mythread != null) {
    mythread.stop();
    mythread = null;
    }
  }

 public void destroy () {
  showStatus("Freeing resources");
 }

}


Back to the stardate applet page.

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.