Binary Clock
import java.applet.Applet;
import java.awt.*;
import java.awt.image.MemoryImageSource;
import java.util.Calendar;
public class BinaryClock extends Applet implements Runnable
{
Thread clockThread = null;
Image backBuffer;
Graphics bg = null;
Image grayImage, redImage;
public static final int grayPixels[] = {
-1, -1, -1, -1, -1, -986896, -3158065, -4276546, -4342339,
-3223858, -1118482, -1, -1, -1, -1, -1, -1, -1, -1, -855310,
-5395027, -8816263, -9671572, -9934744, -9934744, -9671572,
-8882056, -5723992, -1118482, -1, -1, -1, -1, -1, -1644826,
-7960954, -10066330, -9605779, -9474193, -9408400, -9408400,
-9474193, -9605779, -10000537, -8289919, -1907998, -1, -1, -1,
-855310, -7960954, -9934744, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9868951,
-8289919, -1118482, -1, -1, -5395027, -10066330, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -10066330, -5921371, -1,
-986896, -8750470, -9605779, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9539986, -9079435, -1315861, -3092272, -9671572,
-9474193, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9803158, -3618616, -4276546, -9934744, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -10000537, -4802890,
-4342339, -9934744, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -10000537, -4868683, -3223858, -9671572,
-9474193, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9803158, -3750202, -1052689, -8882056, -9605779, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9539986, -9145228, -1447447,
-1, -5658199, -10066330, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -10000537, -6184543, -1, -1, -986896, -8289919,
-9868951, -9408400, -9408400, -9408400, -9408400, -9408400,
-9408400, -9408400, -9408400, -9803158, -8618884, -1315861,
-1, -1, -1, -1973791, -8355712, -10000537, -9539986, -9474193,
-9408400, -9408400, -9474193, -9539986, -10000537, -8618884,
-2302756, -1, -1, -1, -1, -1, -1184275, -5987164, -9079435,
-9803158, -10066330, -10066330, -9803158, -9145228, -6250336,
-1447447, -1, -1, -1, -1, -1, -1, -1, -1, -1381654, -3684409,
-4802890, -4868683, -3750202, -1579033, -1, -1, -1, -1, -1,
-16777216, -16777216, -16777216, -16777216
};
public static final int redPixels[] = {
-1, -1, -1, -1, -1, -5912, -18762, -25958, -26215, -19276,
-6683, -1, -1, -1, -1, -1, -1, -1, -1, -5141, -32897, -53200,
-58340, -59625, -59368, -57826, -53200, -33925, -6426, -1, -1,
-1, -1, -1, -9767, -48317, -60910, -57826, -56284, -54999,
-54228, -54485, -55513, -59111, -49088, -11823, -1, -1, -1,
-4884, -48317, -60653, -56541, -55770, -53971, -50887, -48317,
-47546, -49088, -52172, -57569, -49602, -6683, -1, -1, -32383,
-61167, -56798, -56284, -54228, -49859, -42920, -34182,
-30841, -36238, -45747, -51915, -58854, -35210, -1, -5655,
-53457, -58597, -56541, -55256, -52429, -45233, -31355,
-16706, -11823, -19276, -35724, -48574, -54999, -53971, -7968,
-18762, -58854, -57312, -56541, -54999, -50887, -41635,
-24416, -8739, -4884, -11052, -29556, -46261, -53714, -58083,
-22103, -25958, -60653, -57055, -56541, -54742, -51144,
-42663, -26986, -12080, -7711, -14393, -31612, -46518, -53457,
-59625, -29042, -26215, -60653, -57312, -56541, -55256,
-52429, -46261, -36752, -25444, -21075, -27243, -39836,
-49345, -54228, -59882, -29299, -19533, -59111, -57569,
-56798, -56027, -54228, -50630, -45747, -40607, -38551,
-41892, -47803, -52686, -55256, -58854, -22617, -6169, -53971,
-58340, -57055, -56541, -55513, -53971, -51915, -49602,
-49345, -50373, -52943, -54742, -57055, -54999, -8739, -1,
-34182, -61167, -57312, -57055, -56541, -56027, -54999,
-54485, -54228, -54742, -55513, -56541, -60396, -37266, -1,
-1, -6169, -50116, -60139, -57312, -57055, -56798, -56541,
-56541, -56284, -56541, -56541, -59625, -52172, -8225, -1, -1,
-1, -11823, -50630, -60910, -58340, -57312, -57055, -57055,
-57312, -57826, -60653, -52172, -13879, -1, -1, -1, -1, -1,
-6940, -35981, -55256, -59625, -61167, -61167, -59882, -55770,
-37780, -8482, -1, -1, -1, -1, -1, -1, -1, -1, -8225, -22103,
-29299, -29299, -22617, -9253, -1, -1, -1, -1, -1, -16777216,
-16777216, -16777216, -16777216
};
public String getAppletInfo()
{
return "BinaryClock\n";
}
public void init()
{
setSize(getSize().width, getSize().height);
try {
backBuffer = createImage(getSize().width, getSize().height);
bg = backBuffer.getGraphics();
}
catch (Exception e) {}
grayImage = createImage(new MemoryImageSource(16, 16, grayPixels, 0, 16));
redImage = createImage(new MemoryImageSource(16, 16, redPixels, 0, 16));
}
private void repaintClock(Graphics g)
{
Calendar now = Calendar.getInstance();
int hours = now.get(Calendar.HOUR_OF_DAY);
int mins = now.get(Calendar.MINUTE);
int secs = now.get(Calendar.SECOND);
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width, getSize().height);
draw6Bits(g, hours, 4, 4);
draw6Bits(g, mins, 4, 28);
draw6Bits(g, secs, 4, 52);
g.setColor(Color.blue);
g.drawString(padding2(hours) + ':' + padding2(mins) + ':' + padding2(secs), 40, 92);
}
private void draw6Bits(Graphics g, int value, int x, int y)
{
int filter = 32;
for (int bit = 0; bit < 6; bit++) {
if (value >= filter) {
g.drawImage(redImage, x, y, null);
value -= filter;
}
else {
g.drawImage(grayImage, x, y, null);
}
filter >>= 1;
x += 20;
}
}
public void update(Graphics g)
{
if (bg == null)
repaintClock(g);
else {
repaintClock(bg);
g.drawImage(backBuffer, 0, 0, null);
}
}
public void start()
{
if (clockThread == null) {
clockThread = new Thread(this);
clockThread.start();
}
}
public void stop()
{
clockThread = null;
}
public void run()
{
while (Thread.currentThread() == clockThread) {
repaint();
try {
Thread.sleep(200);
} catch (Exception e) {}
}
}
private String padding2(int value)
{
return (value < 10 ? "0" : "") + Integer.toString(value);
}
}
You can download the Java source code here.
