dominoclock/DominoClock.coffee

220 lines
5.7 KiB
CoffeeScript
Executable File

class DominoClock
constructor: ({@container}) ->
@container.append('<canvas class="canvas_itself"></canvas>')
@canvas = $('canvas', @container).get(0)
@canvas.width = @width = @container.width()
@canvas.height = @hight = @container.height()
@ctx = @canvas.getContext('2d')
if @height > 3 * @width
@square_d = @width
else
@square_d = @height / 3
tock: (element, time) ->
dot: (x, y) ->
@ctx.context.beginPath();
@ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
@ctx.clearRect(0, 0, @height, @width)
if time in [1, 5, 8, 9, 11]
dot startx + dotrad, starty + dotrad
if time in [2, 5, 6, 10, 11]
dot startx + squard - (2 * dotrad), starty + dotrad
if time in [3, 6, 7, 9, 11]
dot startx + squard - (2 * dotrad), starty + squared - (2 * dotrad)
if time in [4, 7, 8, 10, 11]
dot startx + dotrad, starty + squared - (2 * dotrad)
paint: ->
d = new Date()
hour = d.getHours() + 1
hour = if hour > 12 then hour - 12 else hour
min = d.getMinutes()
sec = d.getSeconds()
@tock(@elements.hour, @startx, @starty, hour)
@tock(@elements.mins, @startx, @starty + @square_d, (min / 5))
@tock(@elements.secs, @startx, @starty + (2 * @square_d), (sec / 5))
start: ->
@running = true
@run()
stop: ->
clearTimeout @waiting
@running = false
run: =>
if @running
@paint()
@waiting = setTimeout(120, @run)
$ ->
clock = new DominoClock($('container'))
public class DominoClock extends Applet implements Runnable {
int StartX, StartY, SquareD, DotRad;
Color bgColor, dominoColor, dotColor;
Thread clock = null;
String background;
public void init() {
try {
background = getParameter( "bgcolor" );
} catch (NullPointerException e) {};
if (( background == null ) ||
( background.charAt(0) != '#' ) ||
( background.length() != 7 )) {
Integer rgbValue = new Integer(0);
bgColor = new Color(rgbValue.intValue());
} else {
Integer rgbValue = new Integer(0);
rgbValue = Integer.valueOf( background.substring(1,7), 16 );
bgColor = new Color(rgbValue.intValue());
}
try {
background = getParameter( "dominocolor" );
} catch (NullPointerException e) {};
if (( background == null ) ||
( background.charAt(0) != '#' ) ||
( background.length() != 7 )) {
Integer rgbValue = new Integer(14331680);
dominoColor = new Color(rgbValue.intValue());
} else {
Integer rgbValue = new Integer(14331680);
rgbValue = Integer.valueOf( background.substring(1,7), 16 );
dominoColor = new Color(rgbValue.intValue());
}
try {
background = getParameter( "dotcolor" );
} catch (NullPointerException e) {};
if (( background == null ) ||
( background.charAt(0) != '#' ) ||
( background.length() != 7 )) {
Integer rgbValue = new Integer(3100495);
dotColor = new Color(rgbValue.intValue());
} else {
Integer rgbValue = new Integer(3100495);
rgbValue = Integer.valueOf( background.substring(1,7), 16 );
dotColor = new Color(rgbValue.intValue());
}
Dimension D = size();
if (D.height > (3 * D.width)) {
SquareD = D.width;
StartX = 0;
StartY = 0; // (D.height / 3) - SquareD;
}
else {
SquareD = (D.height / 3);
StartY = 0;
StartX = 0; //.width - (SquareD / 3);
}
DotRad = (int) (Math.sqrt(2 * SquareD * SquareD) / 6);
}
public void paint(Graphics g) {
int Hour, Min, Sec;
Date D = new Date();
Hour = D.getHours();
if (Hour > 12) Hour = Hour - 12;
Min = D.getMinutes();
Sec = D.getSeconds();
Tock(StartX, StartY, Hour, g);
Tock(StartX, StartY + SquareD, (Min / 5), g);
Tock(StartX, StartY + (2 * SquareD), (Sec / 5), g);
}
public void update(Graphics g) {
paint(g); // Overridden to prevent flicker.
}
public void start() {
clock = new Thread(this);
clock.start();
}
public void stop() {
clock.stop();
}
public void run() {
while(true) {
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {}
repaint();
}
}
Tock: (int time) ->
@ctx.clearRect(0, 0, @height, @width)
dot: (x, y) ->
@ctx.context.beginPath();
@ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = "#8ED6FF";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
g.fillRect(StartX, StartY, SquareD, SquareD);
g.setColor(dotColor);
if ((time == 1) || (time == 5) || (time == 8) || (time == 9) || (time == 11))
g.fillOval(StartX + DotRad, StartY + DotRad, DotRad, DotRad);
if ((time == 2) || (time == 5) || (time == 6) || (time == 10) || (time == 11))
g.fillOval(StartX + SquareD - (2 * DotRad), StartY + DotRad, DotRad, DotRad);
if ((time == 3) || (time == 6) || (time == 7) || (time == 9) || (time == 11))
g.fillOval(StartX + SquareD - (2 * DotRad), StartY + SquareD - (2 * DotRad), DotRad, DotRad);
if ((time == 4) || (time == 7) || (time == 8) || (time == 10) || (time == 11))
g.fillOval(StartX + DotRad, StartY + SquareD - (2 * DotRad), DotRad, DotRad);
g.drawLine(StartX, StartY + SquareD - 1, StartX + SquareD, StartY + SquareD - 1);
}
public static void main(String args[]) {
Frame f1 = new Frame("Domino Clock");
DominoClock s1 = new DominoClock();
f1.add("Center", s1);
f1.resize(300, 300);
f1.show();
s1.init();
s1.start();
}
}