Topic: Javascript Stardate Clock |
|
---|---|
Author | Thread |
Obsessive-Compulsive (I) Inmate From: OZONE Asylum |
posted 05-05-2007 09:34
I run a webhosting/development service, and I have a client requesting a Stardate clock (in both a "Federation" and a "Klingon" format) on his site to show a future date in a Stardate format. However, I don't know Javascript (and i'm totaly guessing i'd have to use it) so how do I go about creating one. I started off with: code: import java.awt.*; import java.applet.*; import java.util.Calendar; import java.util.GregorianCalendar; import java.lang.Math; import java.lang.Integer; /** Applet displays future 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 future 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"); } }
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 05-05-2007 13:39
you learn the difference between java and javascript. |
Paranoid (IV) Inmate From: Norway |
posted 05-05-2007 14:38
Also, image swapping is a bad idea. It can generate some reflow if the browser's layout engine is not really well optimized, not to mention the hassle of having to preload the images to avoid further useless connections. |
Paranoid (IV) Inmate From: Norway |
posted 05-05-2007 19:44 |
Obsessive-Compulsive (I) Inmate From: OZONE Asylum |
posted 05-05-2007 21:24
quote:
|
Paranoid (IV) Inmate From: Norway |
posted 05-05-2007 22:46
You have no clue about what you're doing, do you ? |
Nervous Wreck (II) Inmate From: OZONE Asylum |
posted 05-06-2007 03:28
No I don't. I need *serious* help here. If you would be so kind please, as to give me the code, (I tryed the one you posted here I got a script error) I would be gratefull. |
Paranoid (IV) Inmate From: Norway |
posted 05-06-2007 03:59
hum, I didn't post any code sample in this thread. |
Nervous Wreck (II) Inmate From: OZONE Asylum |
posted 05-07-2007 01:54
After spending 3 hours on Google, I found zip that would help. I looked at the 3 links you advised but did not understand a lot of it. I did not provide a link because I wont upload it untill it's done. All I get is the actual code showing. I upload all the images into a temp directory and link it all to a webpage thats on a random site for testing, run from work. No one would see it untill it's completed and moved to it's new home. Youu would get a 404 dissalowing page. |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 05-07-2007 16:18
there is no '404 disallowing' page. 404 means document not found. |
Paranoid (IV) Inmate From: Norway |
posted 05-07-2007 19:07 |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 05-07-2007 20:14
but captain, the future of earth depends on it! |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 05-07-2007 20:20
here is a tutorial that shows how to set up a stardate using a java applet. |