PDA

View Full Version : Something For Your Website Chaps



J'Pol
06-20-2004, 07:51 PM
Why not have a binary clock, it would look really cool.


//******************************************************************************
// Binclock.java:      Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
import java.util.Calendar;

//==============================================================================
// Main Class for applet Binclock
//
//==============================================================================
public class Binclock extends Applet implements Runnable
{
        // global variables
        Thread clockThread = null;
        Calendar now, newnow;
        Image backBuffer;
        Graphics bg;
private Font font;
        private String decimalTime;
        private int x;
        private int y;

        // Binclock Class Constructor
        //--------------------------------------------------------------------------
        public Binclock()
        {
                // TODO: Add constructor code here
        }

        // APPLET INFO SUPPORT:
        // The getAppletInfo() method returns a string describing the applet's
        // author, copyright date, or miscellaneous information.
        //--------------------------------------------------------------------------
        public String getAppletInfo()
        {
                return "Name: Binclock\r\n" +
                      "Author: David Binard (mailto:[email protected])\r\n" +
                      "Date: 08/15/98\r\n" +
                      "Description: Binary Clock Animation\r\n" +
                      "Created with Microsoft Visual J++ Version 1.1\r\n" +
                      "Reactualized for Sun's JDK 1.1.7";
        }


        // The init() method is called by the AWT when an applet is first loaded or
        // reloaded.  Override this method to perform whatever initialization your
        // applet needs, such as initializing data structures, loading images or
        // fonts, creating frame windows, setting the layout manager, or adding UI
        // components.
        //--------------------------------------------------------------------------
        public void init()
        {
        // If you use a ResourceWizard-generated "control creator" class to
        // arrange controls in your applet, you may want to call its
        // CreateControls() method from within this method. Remove the following
        // call to resize() before adding the call to CreateControls();
        // CreateControls() does its own resizing.
        //----------------------------------------------------------------------


        setSize(getSize().width, getSize().height);
font = new Font("Helvetica", Font.PLAIN, 11);
        try {
                backBuffer = createImage(getSize().width, getSize().height);
                bg = backBuffer.getGraphics();
        }
        catch (Exception e) {
                bg = null;
        };

                // TODO: Place additional initialization code here
        }

        // Place additional applet clean up code here.  destroy() is called when
        // when you applet is terminating and being unloaded.
        //-------------------------------------------------------------------------
        public void destroy()
        {
                // TODO: Place applet cleanup code here
        }

        private void repaintClock(Graphics g)
        {
                now = Calendar.getInstance();
                int hours = now.get(Calendar.HOUR_OF_DAY);
                int mins = now.get(Calendar.MINUTE);
                int secs = now.get(Calendar.SECOND);
                decimalTime = lpad(hours) + ":" + lpad(mins) + ":" + lpad(secs);
                String hourBits = calc_bits(hours);
                String minBits = calc_bits(mins);
                String secBits = calc_bits(secs);
                // Clear Background
  g.setFont(font);
                g.setColor(Color.white);
                g.fillRect(0, 0, getSize().width, getSize().height);
                // Draw Clock
                g.setColor(Color.black);
                x = 45;
                y = 225;
                g.drawString("Hours", x + 80, y);
                g.drawString("Minutes", x + 135, y);
                g.drawString("Seconds", x + 192, y);
                y += 15;
                g.drawString("Decimal time", x, y);
                y += 15;
                g.drawString("Octal time", x, y);
                y += 15;
                g.drawString("Hex time", x, y);
                y += 15;
                g.drawString("Binary time", x, y);
                g.setColor(Color.blue);
                y -= 45;
                g.drawString(lpad(hours), x + 88, y);
                g.drawString(lpad(mins), x + 148, y);
                g.drawString(lpad(secs), x + 208, y);
                y += 15;
                g.drawString(to_octal(hours), x + 88, y);
                g.drawString(to_octal(mins), x + 148, y);
                g.drawString(to_octal(secs), x + 208, y);
                y += 15;
                g.drawString(to_hex(hours), x + 88, y);
                g.drawString(to_hex(mins), x + 148, y);
                g.drawString(to_hex(secs), x + 208, y);
                y += 15;
                g.drawString(hourBits, x + 70, y);
                g.drawString(minBits, x + 130, y);
                g.drawString(secBits, x + 190, y);
                x = 5;
                y = 5;
                drawBits(hourBits, g, x, y);
                y += 70;
                drawBits(minBits, g, x, y);
                y += 70;
                drawBits(secBits, g, x, y);
                y += 70;
        }

        private void drawBits(String bits, Graphics g, int x, int y)
        {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 8; i++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bits.charAt(i) == &#39;0&#39;) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.cyan);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.blue);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.fillRect(x, y, 30, 60);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += 40;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; public void update(Graphics g)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bg == null)&nbsp; // no backgound buffer
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaintClock(g);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaintClock(bg);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawImage(backBuffer,0,0,this);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showStatus("Decimal time: " + decimalTime);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }


&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The start() method is called when the page containing the applet
&nbsp; &nbsp; &nbsp; &nbsp; // first appears on the screen. The AppletWizard&#39;s initial implementation
&nbsp; &nbsp; &nbsp; &nbsp; // of this method starts execution of the applet&#39;s thread.
&nbsp; &nbsp; &nbsp; &nbsp; //--------------------------------------------------------------------------
&nbsp; &nbsp; &nbsp; &nbsp; public void start()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; now = Calendar.getInstance();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newnow = Calendar.getInstance();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (clockThread == null) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clockThread = new Thread(this, "Binclock");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clockThread.start();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: Place additional applet start code here
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // The stop() method is called when the page containing the applet is
&nbsp; &nbsp; &nbsp; &nbsp; // no longer on the screen. The AppletWizard&#39;s initial implementation of
&nbsp; &nbsp; &nbsp; &nbsp; // this method stops execution of the applet&#39;s thread.
&nbsp; &nbsp; &nbsp; &nbsp; //--------------------------------------------------------------------------
&nbsp; &nbsp; &nbsp; &nbsp; public void stop()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clockThread = null;
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; // TODO: Place additional applet code here

&nbsp; &nbsp; &nbsp; &nbsp; public void run() {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // loop terminates when clockThread is set to null in stop()
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (Thread.currentThread() == clockThread) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // loop until the time changes by one second
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the sleeps are here to reduce CPU usage
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (now.get(Calendar.SECOND) == newnow.get(Calendar.SECOND)) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clockThread.sleep(200);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newnow = Calendar.getInstance();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; public String calc_bits(int value) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int bit, filter = 32;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuffer bitString = new StringBuffer("00");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (bit = 3; bit <= 8; bit++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value >= filter) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitString.append(&#39;1&#39;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value -= filter;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitString.append(&#39;0&#39;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter = filter - (filter / 2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return bitString.toString();
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; public String lpad(int value) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String mystring = "00";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value <= 9) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mystring = "0" + Integer.toString(value);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mystring = Integer.toString(value);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mystring;
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; public String to_octal(int value) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String octal = (value / 8) + "" + (value % 8);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return octal;
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; public String to_alpha_hex(int value) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value < 10)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Integer.toString(value);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (value == 10)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "A";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (value == 11)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "B";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (value == 12)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "C";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (value == 13)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "D";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (value == 14)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "E";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (value == 15)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "F";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "0";
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; public String to_hex(int value) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String hex = to_alpha_hex(value / 16) + "" + to_alpha_hex(value % 16);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return hex;
&nbsp; &nbsp; &nbsp; &nbsp; }
}

TheRealDave
06-20-2004, 07:54 PM
internetworld :P

J'Pol
06-20-2004, 07:59 PM
Originally posted by TheRealDave@20 June 2004 - 21:02
internetworld :P
Good idea

If a MOD could move this there.

Thanks for the heads up The

Virtualbody1234
06-20-2004, 08:06 PM
There ya go JP.

J'Pol
06-20-2004, 08:10 PM
Originally posted by Virtualbody1234@20 June 2004 - 21:14
There ya go JP.
Cheers VB.

Now everybody can have their own binary clock, it&#39;s fantastic.

Virtualbody1234
06-20-2004, 08:14 PM
D&#39;ya have a sample site of what it looks like?

UcanRock2
06-20-2004, 08:18 PM
Can you make that long story short?

J'Pol
06-21-2004, 11:17 AM
Originally posted by Virtualbody1234@20 June 2004 - 21:22
D&#39;ya have a sample site of what it looks like?
This is it and where the code comes from. (http://www.california.com/~binard/java/Binclock.html)

J'Pol
06-21-2004, 11:18 AM
Here&#39;s one without graphics. (http://javascript.internet.com/clocks/binary-clock.html)

J'Pol
06-21-2004, 11:19 AM
http://www.anderbergfamily.net/ant/binclock/images/title.gif

And one that&#39;s just graphics