NRP : 05111740000077
1. NumberDisplay
/**
* Write a description of class NumberDisplay here.
*
* @author (Nadia Hasna L)
* @version (20181001)
*/
public class NumberDisplay
{
private int limit;
private int fix;
public NumberDisplay(int Maksimal)
{
limit = Maksimal;
fix = 0;
}
public int Value_Now()
{
return fix;
}
public void increment()
{
fix = (fix + 1) % limit;
}
public String getDisplayValue()
{
if(fix < 10)
{
return "0" + fix;
}
else
{
return "" + fix;
}
}
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit))
{
fix = replacementValue;
}
}
}
2. TestClockDispaly
/**
* Write a description of class TestClockDisplay here.
*
* @author (Nadia Hasna L)
* @version (20181001)
*/
public class TestClockDisplay
{
public TestClockDisplay()
{
}
public void test()
{
ClockDisplay cl = new ClockDisplay();
cl.setTime(22,37);
System.out.println(cl.getTime());
cl.setTime(5,59);
System.out.println(cl.getTime());
cl.setTime(14,00);
System.out.println(cl.getTime());
cl.setTime(00,17);
System.out.println(cl.getTime());
}
}
3. ClockDisplay
/**
* Write a description of class ClockDisplay here.
*
* @author (Nadia Hasna L)
* @version (20181001)
*/
public class ClockDisplay
{
private NumberDisplay hour;
private NumberDisplay minute;
private String displayString;
public ClockDisplay()
{
hour = new NumberDisplay(24);
minute = new NumberDisplay(60);
updateDisplay();
}
public ClockDisplay(int jam, int menit)
{
hour = new NumberDisplay(24);
minute = new NumberDisplay(60);
setTime(jam,menit);
}
public void timeTick()
{
minute.increment();
if(minute.Value_Now() == 0)
{
hour.increment();
}
updateDisplay();
}
public void setTime(int jam, int menit)
{
hour.setValue(jam);
minute.setValue(menit);
updateDisplay();
}
public String getTime()
{
return displayString;
}
private void updateDisplay()
{
displayString = hour.getDisplayValue() + ":" + minute.getDisplayValue();
}
}
4. ClockGUI
/**
* Write a description of class ClockGUI here.
*
* @author (Nadia Hasna L)
* @version (20181001)
*/
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" +
"Membuat jam digital simpel dengan 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)
{
}
}
}
}
Hasil:
Tidak ada komentar:
Posting Komentar