TUGAS PBODIGITAL CLOCK
Pada kesempatan kali ini, saya mencoba membuat digital clock menggunakan Java dan GUI dengan awt dan swing. Pada kali ini ada 4 class yang dibuat, yakni
- number : untuk digit yang tertera pada jam digital
- display : untuk menampung dan fungsi fungsi sederhana jam
- tampil : untuk menampilkan jam digital secara bukan GUI dengan format 24 jam
- gui : menampilkan jam digital secara gui dengan button dan panel menggunakan JFrame
Berikut diagram class:
/**
* Write a description of class Numbers here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class number
{
private int limit;
private int time;
public number(int input)
{
limit = input;
time = 0;
}
public int gettime(){
return time;
}
public void settime(int input){
if(input>=0 && input<limit) time=input;
}
public String tampil(){
if(time<10){
return "0"+time;
}
else{
return "" +time;
}
}
public void waktu(){
time = (time+1)%limit;
}
}
2. display
/**
* Write a description of class clockdisplay here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class display
{
private number jam,menit,detik;
private String tampil;
public display(){
jam = new number(24);
menit = new number(60);
detik = new number(60);
update();
}
public display(int j, int m, int d){
jam = new number(24);
menit = new number(60);
detik = new number(60);
setwaktu(j,m,d);
update();
}
public void setwaktu(int j, int m, int d){
jam.settime(j);
menit.settime(m);
detik.settime(d);
update();
}
public void berdetik(){
detik.waktu();
if(detik.gettime()==0){
menit.waktu();
if(menit.gettime()==0)jam.waktu();
}
update();
}
public void update(){
String tempj,tempm,tempd;
if(jam.gettime()<10) tempj="0"+jam.gettime();
else tempj = tempj=""+jam.gettime();
if(menit.gettime()<10) tempm="0"+menit.gettime();
else tempm=""+menit.gettime();
if(detik.gettime()<10) tempd="0"+detik.gettime();
else tempd=""+detik.gettime();
tampil = tempj+":"+tempm+":"+tempd;
}
public void clock(){
System.out.println(tampil);
}
public String gettampil(){
return tampil;
}
}
3.tampil
/**
* Write a description of class tampil here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class tampil
{
public static void disp() throws InterruptedException{
display waktu = new display(22,58,57);
while(true){
waktu.clock();
waktu.berdetik();
Thread.sleep(1000);
System.out.print('\u000C');
}
}
}
Dan berikut ketika diberi GUI
berikut kelas gui
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Scanner;
public class gui
{
private JFrame frame;
private JLabel label;
private display tampil;
private boolean clockRunning = false; //jam digital belum berdetik
private TimerThread timerThread;
public int sHour, sMinute, sSecond;
public void Clock()
{
makeFrame();
tampil = new display(22,58,57);
}
private void start()
{
clockRunning = true; //jam digital berdetik
timerThread = new TimerThread();
timerThread.start();
}
private void stop()
{
clockRunning = false;
}
private void step()
{
tampil.berdetik();
label.setText(tampil.gettampil());
}
private void showAbout()
{
JOptionPane.showMessageDialog (frame, "Jam Digital Sederhana","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: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(clockRunning)
{
step();
pause();
}
}
private void pause()
{
try
{
Thread.sleep(900);
}
catch(InterruptedException exc)
{
}
}
}
}
Tidak ada komentar:
Posting Komentar