Skip to content

Instantly share code, notes, and snippets.

@luizfonseca
Created December 26, 2011 20:00
Show Gist options
  • Save luizfonseca/1522014 to your computer and use it in GitHub Desktop.
Save luizfonseca/1522014 to your computer and use it in GitHub Desktop.
Another version of the Clock, still using the java.util.Observable
// Package :V
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import java.util.Observable;
import javax.swing.Timer;
public class Clock extends Observable implements ActionListener {
public static Timer timer;
public Clock(){
// On new instances of clock, start the Clock.
this.startClock();
}
public String getTime(){
String time = "";
// Better lib to handle this case
GregorianCalendar calendario = new GregorianCalendar();
int h = calendario.get(GregorianCalendar.HOUR_OF_DAY);
int m = calendario.get(GregorianCalendar.MINUTE);
int s = calendario.get(GregorianCalendar.SECOND);
time += ((h < 10) ? "0" : "") + h + ":";
time += ((m < 10) ? "0" : "") + m + ":";
time += ((s < 10) ? "0" : "") + s;
return time;
}
public void actionPerformed(ActionEvent e) {
// Everytime the timer changes, we are performing an action and communicatin' with the Observers
setChanged();
notifyObservers(this.getTime());
}
public void startClock() {
// Check if we didn't start the clock, if not, then start it.
if (timer == null) {
timer = new javax.swing.Timer(1000, this);
timer.setInitialDelay(0);
timer.start();
}
}
}
// Package :V
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;
import javax.swing.*;
public class Form implements Observer {
private Clock clock = new Clock();
private JLabel label = new JLabel();
private JButton button = new JButton("Stop/Restart");
private static JFrame frame = new JFrame("My personal Clock...");
public Form(){
this.mountFrameWindow();
// This is an Observer for Clock
clock.addObserver(this);
}
/*
* Mounting the JFrame/Form
*/
public void mountFrameWindow(){
JPanel panel = new JPanel();
panel.add(label);
panel.add(button);
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
label.setFont(new Font("Verdana", Font.BOLD, 30));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setBounds(200, 200, 600, 200);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if (Clock.timer.isRunning()){
Clock.timer.stop();
} else {
Clock.timer.restart();
}
}
});
label.setText(clock.getTime());
}
/*
* Implementation of the observer pattern, we're listening to changes on the Object.(non-Javadoc)
* @see java.util.Observer#update(java.util.Observable, java.lang.Object)
*/
@Override
public void update(Observable argument, Object value) {
// Debugging
System.out.println("Current time!: " + value);
label.setText((String)value);
}
}
// Package.blabla
// This file runs everything
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
new Form();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment