/** * simple_clock_v3 *
* This version of simple_clock adds tick and tock sounds for fun * using the Ess library. This also brings in the need to sign the applet * so the external library can be used. *
* Demonstrate the use of trigonometric functions along with map() and * norm() for drawing a simple analog clock. Basic trig formulas from * "Foundation ActionScript 3.0 Animation" by Keith Peters (Friends * of Ed, 2007) a good resource for basic animation techniques from the * ground up. * * @author david tames, http://kino-eye.com * */ /* Clock.wav by Pogotron, from freesound.org, icensed under a Creative Commons Sampling Plus 1.0 License: http://www.freesound.org/samplesViewSingle.php?id=60811 Copyright 2010 by David Tames, some rights reserved. This code is released under the terms of a Creative Commons Share-Alike Attribution License. */ // --------- libraries --------- import krister.Ess.*; // --------- parameters --------- boolean sound = true; color handColor = color(245, 245, 200); color textColor = color(200, 200, 250); color tickColor = color(164, 164, 228); color backgroundColor = color(44, 44, 64); int secondHandWeight = 2; int minuteHandWeight = 4; int hourHandWeight = 10; int minuteTickWeight = 2; int hourTickWeight = 7; float minuteHandLength = 130; float secondHandLength = 150; float hourHandLength = 80; float clockCenterX = 200; float clockCenterY = 200; // NO NO BAD BOY int screenSizeX = 400; // NO NO BAD BOY int screenSizeY = 430; // --------- global variables --------- // time variables, current ss, mm, and hh as radians float ss_rad; float mm_rad; float hh_rad; // endpoints of the clock hands float X2; float Y2; // tick positions float tickX; float tickY; int ss; // the current second int hh; // the current hours int mm; // the current minutes int last_ss; // the previous second boolean tock = false; // to tock or to tick, that is the question // text stuff PFont fontNumbers; String hh_sp; String mm_sp; String ss_sp; // Ess objects AudioChannel tickSound; AudioChannel tockSound; // --------- setup --------- void setup() { // NO NO BAD BOY size(, screenSizeY); size(400, 430); smooth(); fontNumbers = loadFont("ChollaUnicase-48.vlw"); textFont(fontNumbers, 48); if (sound) { Ess.start(this); last_ss = second(); tickSound=new AudioChannel(); tickSound.loadSound("tick.wav"); tockSound=new AudioChannel(); tockSound.loadSound("tock.wav"); } } // --------- draw --------- void draw() { background(backgroundColor); fill(handColor); stroke(handColor); ss = second(); mm = minute(); hh = hour(); if (sound) { if (ss != last_ss) { if (tock == false) { tickSound.play(1); tock = true; // println("tick"); last_ss = ss; } else { tockSound.play(1); tock = false; // println("tock"); last_ss = ss; } } } // What time is it? // 0 radians is at the 3 o'clock position, therefore we subtract // HALF_PI so the angles start at the top of clock ss_rad = map(ss, 0, 60, 0, TWO_PI) - HALF_PI; mm_rad = map(mm + norm(ss, 0, 60), 0, 60, 0, TWO_PI) - HALF_PI; hh_rad = map(hh + norm(mm, 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI; // println ("hh:mm:ss: " + hh_rad + ":" + mm_rad + ":" + ss_rad); // print digital time fill(textColor); if (hh < 10) { hh_sp = "0"; } else { hh_sp = ""; } if (mm < 10) { mm_sp = ":0"; } else { mm_sp = ":"; } if (ss < 10) { ss_sp = ":0"; } else { ss_sp = ":"; } text(hh_sp + hh + mm_sp + mm + ss_sp + ss, clockCenterX - hourHandLength -2, clockCenterY + secondHandLength + 50 ); // has the seconds incremented? do a tick or a tock stroke(handColor); // Draw the second hand strokeWeight(secondHandWeight); X2 = cos (ss_rad) * secondHandLength + clockCenterX; Y2 = sin (ss_rad) * secondHandLength + clockCenterY; line (clockCenterX, clockCenterY, X2, Y2); // Draw the minute hand strokeWeight(minuteHandWeight); X2 = cos (mm_rad) * minuteHandLength + clockCenterX; Y2 = sin (mm_rad) * minuteHandLength + clockCenterY; line (clockCenterX, clockCenterY, X2, Y2); // Draw the hour hand strokeWeight(hourHandWeight); X2 = cos (hh_rad) * hourHandLength + clockCenterX; Y2 = sin (hh_rad) * hourHandLength + clockCenterY; line (clockCenterX, clockCenterY, X2, Y2); stroke(tickColor); // Draw the seconds ticks strokeWeight(minuteTickWeight); for (int angle = 0; angle < 360; angle+=6) { tickX = clockCenterX + (cos(radians(angle)) * secondHandLength); tickY = clockCenterY + (sin(radians(angle)) * secondHandLength); point(tickX, tickY); } // Draw the hour ticks strokeWeight(hourTickWeight); for (int angle = 0; angle < 360; angle+=30) { tickX = clockCenterX + (cos(radians(angle)) * secondHandLength); tickY = clockCenterY + (sin(radians(angle)) * secondHandLength); point(tickX, tickY); } }