Skip to content

Instantly share code, notes, and snippets.

@liandrew
Created September 17, 2018 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liandrew/5e01835ca809e27633b09f08ae3e9862 to your computer and use it in GitHub Desktop.
Save liandrew/5e01835ca809e27633b09f08ae3e9862 to your computer and use it in GitHub Desktop.
/* clock.pde
Synchronizes an analog and digital clock with your machine's time.
Uses the observer pattern to synchronize clocks
*/
float angle = 0;
Clock clock;
Clock digital;
Publisher publisher;
void setup()
{
size(300, 300);
stroke(255);
publisher = new Publisher();
clock = new AnalogClock(publisher);
digital = new DigitalClock(publisher);
}
/* code in draw() function runs every frame
to do animation, use variables to control the things you draw.*/
void draw()
{
background(253, 237, 255);
publisher.setTime(hour(), minute(), second());
clock.draw();
digital.draw();
}
interface Observer {
void update(int hour, int minute, int second);
}
interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}
interface Clock {
int getMinute();
int getHour();
int getSecond();
void draw();
}
class AnalogClock implements Clock, Observer {
private int hour, minute, second;
private Subject publisher;
PImage clockBackground;
AnalogClock(Subject publisher){
this.publisher = publisher;
publisher.registerObserver(this);
clockBackground = loadImage("data/analog_bg.png");
}
AnalogClock(int hour, int minute, int second, Subject publisher){
this.hour = hour;
this.minute = minute;
this.second = second;
this.publisher = publisher;
publisher.registerObserver(this);
}
int getMinute(){
return this.minute;
}
void setMinute(int minute){
this.minute = minute;
}
int getHour(){
return this.hour;
}
void setHour(int hour){
this.hour = hour;
}
int getSecond(){
return this.second;
}
void setSecond(int second){
this.second = second;
}
void draw(){
image(clockBackground, 20,20);
tint(255, 126);
clockBackground.resize(140, 140);
fill(0);
noStroke();
ellipse(90, 90, 120, 120);
stroke(255);
float sec = map(this.second, 0, 60, 0, TWO_PI) - HALF_PI;
float min = map(this.minute, 0, 60, 0, TWO_PI) - HALF_PI;
float hr = map(this.hour % 12, 0, 12, 0, TWO_PI) - HALF_PI;
stroke(255,0,0);
strokeWeight(1);
line(90, 90, cos(sec)*58 + 90, sin(sec)*58 + 90);
stroke(255);
strokeWeight(4);
line(90, 90, cos(min)*50 + 90, sin(min)*50 + 90);
strokeWeight(4);
line(90, 90, cos(hr)*45 + 90, sin(hr)*45 + 90);
}
void update(int hour, int minute, int second){
setHour(hour);
setMinute(minute);
setSecond(second);
}
}
class DigitalClock implements Clock, Observer {
private int hour, minute, second;
private Subject publisher;
private PFont font;
DigitalClock(Subject publisher){
this.publisher = publisher;
publisher.registerObserver(this);
}
DigitalClock(int hour, int minute, int second, Subject publisher){
this.hour = hour;
this.minute = minute;
this.second = second;
this.publisher = publisher;
publisher.registerObserver(this);
}
int getMinute(){
return this.minute;
}
void setMinute(int minute){
this.minute = minute;
}
int getHour(){
return this.hour;
}
void setHour(int hour){
this.hour = hour;
}
int getSecond(){
return this.second;
}
void setSecond(int second){
this.second = second;
}
void draw(){
fill(0);
noStroke();
rect(150, 225, 100, 50, 7);
font = createFont("Myriad Set Pro", 17);
textFont(font);
textAlign(CENTER);
String t = nf(this.hour, 2) + " : " + nf(this.minute,2) + " : " + nf(this.second, 2);
fill(255);
text(t, 200, 255);
}
void update(int hour, int minute, int second){
setHour(hour);
setMinute(minute);
setSecond(second);
}
}
class Publisher implements Subject {
private ArrayList observers;
private int hour, minute, second;
Publisher() {
observers = new ArrayList();
}
void registerObserver(Observer o) {
observers.add(o);
}
void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer) observers.get(i);
observer.update(hour, minute, second);
}
}
void setTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
notifyObservers();
}
int getHour() {
return this.hour;
}
int getMinute() {
return this.minute;
}
int getSecond(){
return this.second;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment