NRP: 05111740000097
Kelas: PBO-A
Ini adalah tugas rumah saya membuat Digital Clock dengan menggunakan Java.
Berikut Formatted Source code saya:
A) NumberDisplay
public class NumberDisplay
{
private int limit;
private int value;
public NumberDisplay(int Maks)
{
limit = Maks;
value = 0;
}
public int getValue()
{
return value;
}
public void increment()
{
value = (value + 1) % limit;
}
public String getDisplayValue()
{
if(value >= 10)
{
return "" + value;
}
else
{
return "0" + value;
}
}
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit))
{
value = replacementValue;
}
}
}
B)Clock Display
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour,minute);
}
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0)
{
hours.increment();
}
updateDisplay();
}
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
public String getTime()
{
return displayString;
}
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue();
}
}
C) ClockGUI 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 clockOn = false;
private TimerThread timerThread;
public void Clock()
{
makeFrame();
clock = new ClockDisplay();
}
private void start()
{
clockOn = true;
timerThread = new TimerThread();
timerThread.start();
}
private void stop()
{
clockOn = false;
}
private void step()
{
clock.timeTick();
label.setText(clock.getTime());
}
private void showAbout()
{
JOptionPane.showMessageDialog (frame, "Clock Version 0.1\n" +
"Make a digital clock with java",
"About Clock",
JOptionPane.INFORMATION_MESSAGE);
}
private void quit()
{
System.exit(0);
}
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(12,12));
label = new JLabel("00:00", 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);
}
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);
}
class TimerThread extends Thread
{
public void run()
{
while(clockOn)
{
step();
pause();
}
}
private void pause()
{
try
{
Thread.sleep(900);
}
catch(InterruptedException exc)
{
}
}
}
}
D) Test Clock Display
/**
* Write a description of class Clock here.
*
* @author (Muhammad Naufal Refadi)
* @version (30-9-2018)
*/
public class TestClockDisplay
{
public void test()
{
ClockDisplay clock = new ClockDisplay();
clock.setTime(11,50);
System.out.println(clock.getTime());
clock.setTime(9,05);
System.out.println(clock.getTime());
clock.setTime(23,58);
System.out.println(clock.getTime());
clock.setTime(17,59);
System.out.println(clock.getTime());
clock.setTime(12,01);
System.out.println(clock.getTime());
}
}
Dan berikut adalah screenshot hasil outputnya
No comments:
Post a Comment