-
Poster
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)
{
for (int i = 0; i < 8; i++) {
if (bits.charAt(i) == '0' {
g.setColor(Color.cyan);
} else {
g.setColor(Color.blue);
}
g.fillRect(x, y, 30, 60);
x += 40;
}
}
public void update(Graphics g)
{
if (bg == null) // no backgound buffer
repaintClock(g);
else {
repaintClock(bg);
g.drawImage(backBuffer,0,0,this);
showStatus("Decimal time: " + decimalTime);
}
}
// The start() method is called when the page containing the applet
// first appears on the screen. The AppletWizard's initial implementation
// of this method starts execution of the applet's thread.
//--------------------------------------------------------------------------
public void start()
{
now = Calendar.getInstance();
newnow = Calendar.getInstance();
if (clockThread == null) {
clockThread = new Thread(this, "Binclock");
clockThread.start();
}
// TODO: Place additional applet start code here
}
// The stop() method is called when the page containing the applet is
// no longer on the screen. The AppletWizard's initial implementation of
// this method stops execution of the applet's thread.
//--------------------------------------------------------------------------
public void stop()
{
clockThread = null;
}
// TODO: Place additional applet code here
public void run() {
// loop terminates when clockThread is set to null in stop()
while (Thread.currentThread() == clockThread) {
repaint();
// loop until the time changes by one second
// the sleeps are here to reduce CPU usage
while (now.get(Calendar.SECOND) == newnow.get(Calendar.SECOND)) {
try {
clockThread.sleep(200);
} catch (Exception e) {
};
newnow = Calendar.getInstance();
}
}
}
public String calc_bits(int value) {
int bit, filter = 32;
StringBuffer bitString = new StringBuffer("00");
for (bit = 3; bit <= 8; bit++) {
if (value >= filter) {
bitString.append('1'
value -= filter;
}
else {
bitString.append('0'
}
filter = filter - (filter / 2);
}
return bitString.toString();
}
public String lpad(int value) {
String mystring = "00";
if (value <= 9) {
mystring = "0" + Integer.toString(value);
}
else {
mystring = Integer.toString(value);
}
return mystring;
}
public String to_octal(int value) {
String octal = (value / 8) + "" + (value % 8);
return octal;
}
public String to_alpha_hex(int value) {
if (value < 10)
return Integer.toString(value);
else if (value == 10)
return "A";
else if (value == 11)
return "B";
else if (value == 12)
return "C";
else if (value == 13)
return "D";
else if (value == 14)
return "E";
else if (value == 15)
return "F";
else
return "0";
}
public String to_hex(int value) {
String hex = to_alpha_hex(value / 16) + "" + to_alpha_hex(value % 16);
return hex;
}
}
-
-
†Problem child
internetworld
-
-
Poster
Originally posted by TheRealDave@20 June 2004 - 21:02
internetworld
Good idea
If a MOD could move this there.
Thanks for the heads up The
-
-
Forum Star
BT Rep: +2
-
-
Poster
Originally posted by Virtualbody1234@20 June 2004 - 21:14
There ya go JP.
Cheers VB.
Now everybody can have their own binary clock, it's fantastic.
-
-
Forum Star
BT Rep: +2
D'ya have a sample site of what it looks like?
-
-
Phantom Gander
Can you make that long story short?
-
-
Poster
Originally posted by Virtualbody1234@20 June 2004 - 21:22
D'ya have a sample site of what it looks like?
This is it and where the code comes from.
-
-
Poster
-
-
Poster
And one that's just graphics
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks