1. Class Window
2. Source Code
NumberDisplay Class
PeriodDisplay Class
ClockDisplay Class
3. 12-Hour Clock
Output
3. 12-Hour Clock with GUI
Output

2. Source Code
NumberDisplay Class
/**
* NumberDisplay Class
*
* @author (Arino Jenynof)
* @version (20180929)
*/
public class NumberDisplay
{
private int limit; //Upper bound, exclusive
private int value; //Current value
public NumberDisplay(int limit)
{
this.limit = limit;
value = 0;
}
public int get_value()
{
return value;
}
public void set_value(int new_value)
{
if (new_value >= 0 && new_value < limit)
{
value = new_value;
}
}
public String get_display()
{
if (value < 10)
{
return "0" + value;
}
else
{
return "" + value;
}
}
public void increment()
{
value = (value + 1) % limit;
}
}
PeriodDisplay Class
/**
* PeriodDisplay Class
*
* @author (Arino Jenynof)
* @version (20180929)
*/
public class PeriodDisplay
{
private String period; //AM or PM
public PeriodDisplay()
{
period = "AM";
}
public PeriodDisplay(String period)
{
this.period = period;
}
public String get_period()
{
return period;
}
public void set_period(String period)
{
this.period = period;
}
}
ClockDisplay Class
/**
* ClockDisplay Class
*
* @author (Arino Jenynof)
* @version (20180929)
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private PeriodDisplay period;
private String display; //For displaying current time
/**
* Constructor without initial input, set the clock to 00:00 AM
*/
public ClockDisplay()
{
hours = new NumberDisplay(13); //12-Hour clock
minutes = new NumberDisplay(60);
period = new PeriodDisplay();
update_display();
}
/**
* Constructor with user input, set the clock according to user needs
*/
public ClockDisplay(int hours, int minutes, String period)
{
this.hours = new NumberDisplay(13);
this.minutes = new NumberDisplay(60);
this.period = new PeriodDisplay();
set_display(hours, minutes, period);
}
/**
* Update display string
*/
public void update_display()
{
display = hours.get_display() + ":" + minutes.get_display() + " " + period.get_period();;
}
public void set_display(int hours, int minutes, String period)
{
this.hours.set_value(hours);
this.minutes.set_value(minutes);
this.period.set_period(period);
update_display();
}
public String get_display()
{
return display;
}
/**
* Increment minute
*/
public void tick()
{
minutes.increment();
if (minutes.get_value() == 0)
{
hours.increment();
}
if (hours.get_value() == 12 && minutes.get_value() == 0)
{
if (period.get_period().equalsIgnoreCase("AM"))
{
period.set_period("PM");
}
else
{
period.set_period("AM");
}
}
update_display();
}
}
3. 12-Hour Clock
/**
* 12-Hour Clock Demonstration
*
* @author (Arino Jenynof)
* @version (20180929)
*/
public class Clock12Hour
{
public void demo()
{
System.out.println("12-Hour Clock Demonstration");
ClockDisplay clock = new ClockDisplay();
clock.set_display(11, 55, "AM");
System.out.println(clock.get_display());
int i = 10;
while (i > 0)
{
clock.tick();
System.out.println(clock.get_display());
i--;
}
}
}
Output

3. 12-Hour Clock with GUI
/**
* ClockGUI Class
* Displaying Clock with GUI
*
* @author (Arino Jenynof)
* @version (20180929)
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ClockGUI
{
private JFrame frame;
private JLabel label;
private ClockDisplay clock;
private boolean clockRunning = false;
private TimerThread timerThread;
public ClockGUI()
{
clock = new ClockDisplay();
makeFrame();
}
public ClockGUI(int hours, int minute, String period)
{
clock = new ClockDisplay(hours, minute, period);
makeFrame();
}
private void start()
{
clockRunning = true;
timerThread = new TimerThread();
timerThread.start();
}
private void stop()
{
clockRunning = false;
}
private void step()
{
clock.tick();
label.setText(clock.get_display());
}
private void showAbout()
{
JOptionPane.showMessageDialog(frame,
"Clock v1.0\n" +
"A simple interface for the 'Object First' clock display project",
"About Clock",
JOptionPane.INFORMATION_MESSAGE);
}
private void quit()
{
System.exit(0);
}
private void makeMenuBar(JFrame frame)
{
final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
menu = new JMenu("File");
menubar.add(menu);
item = new JMenuItem("About Clock. . . ");
item.addActionListener(e->showAbout());
menu.add(item);
menu.addSeparator();
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
item.addActionListener(e->quit());
menu.add(item);
}
private void makeFrame()
{
frame = new JFrame("Clock");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(1, 60, 1, 60));
makeMenuBar(frame);
contentPane.setLayout(new BorderLayout(50, 50));
label = new JLabel(clock.get_display(), SwingConstants.CENTER);
Font displayFont = label.getFont().deriveFont(96.0f);
label.setFont(displayFont);
contentPane.add(label, BorderLayout.CENTER);
JPanel toolbar = new JPanel();
toolbar.setLayout(new GridLayout(1, 0));
JButton startButton = new JButton("Start");
startButton.addActionListener(e->start());
toolbar.add(startButton);
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(e->stop());
toolbar.add(stopButton);
JButton stepButton = new JButton("Step");
stepButton.addActionListener(e->step());
toolbar.add(stepButton);
JPanel flow = new JPanel();
flow.add(toolbar);
contentPane.add(flow, BorderLayout.SOUTH);
frame.pack();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
public class TimerThread extends Thread
{
public void run()
{
while (clockRunning)
{
step();
pause();
}
}
private void pause()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException exc)
{
//NOP
}
}
}
}
Output

Comments
Post a Comment