Showing posts with label digital clock. Show all posts
Showing posts with label digital clock. Show all posts

Saturday, May 31, 2014

Digital Clock in Java


One of the fascinating invention by man that amaze me is the clock or watch this object help us knows what is the exact time. It helps us in many ways in our day to day lives for instances what time we go to work and school, what time we to go sleep, what time we talk our medicine so that we will healed for our sickness. As a programmer there are many things that I have learned not only to solve programming problems but also mimic the real things that I can find in my surroundings.

In this article I will show you how create your own Digital Clock using Java as my programming language. I am using Swing Framework to create this program. There are many Java libraries that I used to create the graphical user interface of our Digital Clock I am very glad that Java is enriched with many build in library to do the graphical interface much easier that cannot be found in some programming language just like in C or C++.

Here is the library that I used to create our graphical user interface, time and calendar functions in Java import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.util.Calendar and import java.util.* all this library file is a part of Java programming language we must able to study each one of them to know more the different library functions which enhance and add functionality in our program.

I hope you find my sample program Digital Clock in Java useful in your learning in Java programming language. One of the things that I learned in Java know first what kind of problem you want to solve and select the appropriate library and frameworks that suited in your needs why because Java is a very big and complex language you may be lost if you are not aware of it.


Sample Output Of Our Program


The TextPad text editor that I used in creating our program

Program Listing

// clock.java
// Program code and Design By: Mr. Jake Rodriguez Pomperada,MAED-IT
// Tool : Java, TextPad Text Editor
// Date : May 31, 2014
// A Simple Digital Clock in Java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Calendar;
import java.util.*;

public class clock {

    public static void main(String[] args) {
        JFrame clock = new TextClockWindow();
        clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clock.setVisible(true);
    }
}


class TextClockWindow extends JFrame {

    private JTextField timeField;

    public TextClockWindow() {

        timeField = new JTextField(10);
        timeField.setFont(new Font("sansserif", Font.PLAIN, 48));

        Container content = this.getContentPane();
        content.setLayout(new FlowLayout());
        content.add(timeField);

        this.setTitle("DIGITAL CLOCK By: Jake R. Pomperada, MAED-IT");
        this.pack();


        javax.swing.Timer t = new javax.swing.Timer(1000,
              new ActionListener() {
                  public void actionPerformed(ActionEvent e) {

                  Calendar calendar = new GregorianCalendar();
String am_pm;


                     Calendar now = Calendar.getInstance();
                      int h = now.get(Calendar.HOUR_OF_DAY);
                      int m = now.get(Calendar.MINUTE);
                      int s = now.get(Calendar.SECOND);


if( calendar.get( Calendar.AM_PM ) == 0 ){
           am_pm = "AM";

        }
        else{
            am_pm = "PM";

        }   // Code to Determine whether the time is AM or PM

timeField.setText("" + h + ":" + m + ":" + s + " " + am_pm);
                  timeField.setHorizontalAlignment(JTextField.CENTER);
               // Center the text
            timeField.getCaret().setVisible(false);
               // Hide the Cursor in JTextField
         }
              });
        t.start();
    }
} // End of Code













Friday, May 30, 2014

Digital Clock in Visual Basic 6


Computers plays an important role in our day to day lives it can be used to process information that is useful in making good decisions whether in business or in our personal decisions. In this article I will show you how to make a simple Digital Clock using Microsoft Visual Basic 6.

This program will display the current time in your computer in digital format. Making this program is very easy because in Visual Basic 6 there are many controls that makes computer programming more easier to implement. I'm using a timer control in this program and label control to display the current time from our computer. 

If you wonder how the computer know the exact time very simple our computer is equip of a CMOS chip. CMOS means complimentary metal oxide semiconductor that stores the computer related instructions including the date and time of our computer. Even the computer is off but still the time and date is updated because it have a battery that connected in the computer motherboard.

I hope you find my program useful in learning how to  a programming using Microsoft Visual Basic 6.

Thank you very much until the next article.



Sample Output of Our Program


Program Listing

Private Sub Form_Load()
Label1.Caption = Format(Time, "h:mm:ss AM/PM")
Label1.Width = Me.Width
Label1.Height = Me.Height
Label1.Top = Me.Top
Label1.Left = Me.Left
End Sub

Private Sub Timer1_Timer()
Dim s As String
If InStr(Label1.Caption, ":") > 0 Then
    s = Replace(Label1.Caption, ":", " ", , , vbTextCompare)
    Label1.Caption = s
Else
   Label1.Caption = Format(Time, "h:mm:ss AM/PM")
End If
End Sub