How to create the digital clock in java swing application ?

I want to create the running digital clock in my java swing application. Can someone throw some light on this how to do this ? Or If someone has done it then can someone pl. paste the code ?
Thanks.

hi prah_Rich,
I have created a digital clock you can use. You will most likely have to change some things to use it in another app although that shouldn't be too hard. A least it can give you some ideas on how to create one of your own. There are three classes.One that creates the numbers. a gui class and frame class.
cheers:)
Hex45
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
public class DigitalClock extends Panel{
          BasicStroke stroke = new BasicStroke(4,BasicStroke.CAP_ROUND,
                                           BasicStroke.JOIN_BEVEL);
          String hour1, hour2;
          String minute1, minute2;
          String second1, second2;
          String mill1, mill2, mill3;
          int hr1, hr2;
          int min1, min2;
          int sec1, sec2;
          int mll1, mll2,mll3;       
    public void update(Graphics g){
         paint(g);
     public void paint(Graphics g){
          Graphics2D g2D = (Graphics2D)g;
          DigitalNumber num = new DigitalNumber(10,10,20,Color.cyan,Color.black);     
          GregorianCalendar c = new GregorianCalendar();
          String hour = String.valueOf(c.get(Calendar.HOUR));
          String minute = String.valueOf(c.get(Calendar.MINUTE));
          String second = String.valueOf(c.get(Calendar.SECOND));
          String milliSecond = String.valueOf(c.get(Calendar.MILLISECOND));
          if(hour.length()==2){
               hour1 = hour.substring(0,1);
               hour2 = hour.substring(1,2);
          }else{
               hour1 = "0";
               hour2 = hour.substring(0,1);
          if(minute.length()==2){
               minute1 = minute.substring(0,1);
               minute2 = minute.substring(1,2);
          }else{
               minute1 = "0";
               minute2 = minute.substring(0,1);
          if(second.length()==2){
               second1 = second.substring(0,1);
               second2 = second.substring(1,2);
          }else{
               second1 = "0";
               second2 = second.substring(0,1);
          if(milliSecond.length()==3){
               mill1 = milliSecond.substring(0,1);
               mill2 = milliSecond.substring(1,2);
               mill3 = milliSecond.substring(2,3);
          }else if(milliSecond.length()==2){
               mill1 = "0";
               mill2 = milliSecond.substring(0,1);
               mill3 = milliSecond.substring(1,2);
          }else{
               mill1 = "0";
               mill2 = "0";
               mill3 = milliSecond.substring(0,1);
          hr1  = Integer.parseInt(hour1);     
          hr2  = Integer.parseInt(hour2);
          min1 = Integer.parseInt(minute1);
          min2 = Integer.parseInt(minute2);
          sec1 = Integer.parseInt(second1);
          sec2 = Integer.parseInt(second2);
          mll1 = Integer.parseInt(mill1);
          mll2 = Integer.parseInt(mill2);
          g2D.setStroke(stroke);
          g2D.setPaint(Color.cyan);
          num.setSpacing(true,8);
          num.setSpacing(true,8);
          if(hr1==0&hr2==0){
               num.drawNumber(1,g2D);
               num.setLocation(40,10);
               num.drawNumber(2,g2D);
          else{
               if(!(hr1 == 0)){     
                    num.drawNumber(hr1,g2D);
               num.setLocation(40,10);
               num.drawNumber(hr2,g2D);
          num.setLocation(70,10);
          num.drawNumber(DigitalNumber.DOTS,g2D);
          num.setLocation(100,10);
          num.drawNumber(min1,g2D);
          num.setLocation(130,10);
          num.drawNumber(min2,g2D);
          num.setLocation(160,10);
          num.drawNumber(DigitalNumber.DOTS,g2D);
          num.setLocation(190,10);
          num.drawNumber(sec1,g2D);
          num.setLocation(220,10);
          num.drawNumber(sec2,g2D);
          /*num.setLocation(250,10);
          num.drawNumber(DigitalNumber.DOTS,g2D);
          num.setLocation(280,10);
          num.drawNumber(mll1,g2D);
          num.setLocation(310,10);
          num.drawNumber(mll2,g2D);
          g2D.setPaint(Color.cyan);
          if((c.get(Calendar.AM_PM))==Calendar.AM){               
               g2D.drawString("AM",260,20);
          }else{
               g2D.drawString("PM",260,20);
     String dayOfweek = "";     
     switch(c.get(Calendar.DAY_OF_WEEK)){
          case(Calendar.SUNDAY):
               dayOfweek = "Sunday, ";
               break;
          case(Calendar.MONDAY):
               dayOfweek = "Monday, ";
               break;
          case(Calendar.TUESDAY):
               dayOfweek = "Tuesday, ";
               break;
          case(Calendar.WEDNESDAY):
               dayOfweek = "Wednesday, ";
               break;
          case(Calendar.THURSDAY):
               dayOfweek = "Thursday, ";
               break;
          case(Calendar.FRIDAY):
               dayOfweek = "Friday, ";
               break;
          case(Calendar.SATURDAY):
               dayOfweek = "Saturday, ";
               break;
     String month = "";     
     switch(c.get(Calendar.MONTH)){
          case(Calendar.JANUARY):
               month = "January ";
               break;
          case(Calendar.FEBRUARY):
               month = "February ";
               break;
          case(Calendar.MARCH):
               month = "March ";
               break;
          case(Calendar.APRIL):
               month = "April ";
               break;
          case(Calendar.MAY):
               month = "May ";
               break;
          case(Calendar.JUNE):
               month = "June ";
               break;
          case(Calendar.JULY):
               month = "July ";
               break;
          case(Calendar.AUGUST):
               month = "August ";
               break;
          case(Calendar.SEPTEMBER):
               month = "September ";
               break;
          case(Calendar.OCTOBER):
               month = "October ";
               break;
          case(Calendar.NOVEMBER):
               month = "November ";
               break;
          case(Calendar.DECEMBER):
               month = "December ";
               break;
     int day = c.get(Calendar.DAY_OF_MONTH);
     int year = c.get(Calendar.YEAR);
     Font font = new Font("serif",Font.PLAIN,24);
     g2D.setFont(font);
     g2D.drawString(dayOfweek+month+day+", "+year,10,80);
     public static void main(String args[]){
          AppFrame aframe = new AppFrame("Digital Clock");
          Container cpane = aframe.getContentPane();
          final DigitalClock dc = new DigitalClock();
          dc.setBackground(Color.black);
          cpane.add(dc,BorderLayout.CENTER);
          aframe.setSize(310,120);
          aframe.setVisible(true);
          class Task extends TimerTask {
             public void run() {
                  dc.repaint();
          java.util.Timer timer = new java.util.Timer();
         timer.schedule(new Task(),0L,250L);
class DigitalNumber {
     private float x=0;
     private float y=0;
     private float size=5;
     private int number;
     private Shape s;
     private float space = 0;
     public static final int DOTS = 10;
     private Color on,off;
     DigitalNumber(){          
          this(0f,0f,5f,Color.cyan,Color.black);          
     DigitalNumber(float x,float y, float size,Color on,Color off){
          this.x = x;
          this.y = y;
          this.size = size;
          this.on = on;
          this.off = off;
     public void drawNumber(int number,Graphics2D g){
          int flag = 0;
          switch(number){
               case(0):          
                    flag = 125;
                    break;
               case(1):
                    flag = 96;
                    break;
               case(2):
                    flag = 55;
                    break;
               case(3):
                    flag = 103;
                    break;
               case(4):
                    flag = 106;
                    break;
               case(5):
                    flag = 79;
                    break;
               case(6):
                    flag = 94;
                    break;
               case(7):
                    flag = 97;
                    break;
               case(8):
                    flag = 127;
                    break;
               case(9):
                    flag = 107;
                    break;
               case(DOTS):
                    GeneralPath path = new GeneralPath();
                    path.moveTo(x+(size/2),y+(size/2)-1);
                    path.lineTo(x+(size/2),y+(size/2)+1);
                    path.moveTo(x+(size/2),y+(size/2)+size-1);
                    path.lineTo(x+(size/2),y+(size/2)+size+1);
                    g.setPaint(on);
                    g.draw(path);     
                    return;
          //Top          
          if((flag & 1) == 1){
               g.setPaint(on);
          }else{
               g.setPaint(off);
          GeneralPath Top = new GeneralPath();
          Top.moveTo(x + space, y);
          Top.lineTo(x + size - space, y);
          g.draw(Top);
          //Middle
          if((flag & 2) == 2){
               g.setPaint(on);
          }else{
               g.setPaint(off);
          GeneralPath Middle = new GeneralPath();
          Middle.moveTo(x + space, y + size); 
          Middle.lineTo(x + size - space,y + size);     
          g.draw(Middle);
          //Bottom
          if((flag & 4) == 4){
               g.setPaint(on);
          }else{
               g.setPaint(off);
          GeneralPath Bottom = new GeneralPath();
          Bottom.moveTo(x + space, y + (size * 2));  
          Bottom.lineTo(x + size - space, y + (size * 2));
          g.draw(Bottom);
          //TopLeft
          if((flag & 8) == 8){
               g.setPaint(on);
          }else{
               g.setPaint(off);
          GeneralPath TopLeft = new GeneralPath();     
          TopLeft.moveTo(x, y + space);
          TopLeft.lineTo(x, y + size - space);          
          g.draw(TopLeft);
          //BottomLeft
          if((flag & 16) == 16){
               g.setPaint(on);
          }else{
               g.setPaint(off);
          GeneralPath BottomLeft = new GeneralPath();     
          BottomLeft.moveTo(x, y + size + space);
          BottomLeft.lineTo(x, y + (size * 2) - space);
          g.draw(BottomLeft);
          //TopRight
          if((flag & 32) == 32){
               g.setPaint(on);
          }else{
               g.setPaint(off);
          GeneralPath TopRight = new GeneralPath();     
          TopRight.moveTo(x + size, y + space);
          TopRight.lineTo(x + size, y + size - space);
          g.draw(TopRight);
          //BottomRight
          if((flag & 64) == 64){
               g.setPaint(on);
          }else{
               g.setPaint(off);
          GeneralPath BottomRight = new GeneralPath();     
          BottomRight.moveTo(x + size, y + size + space);
          BottomRight.lineTo(x + size, y + (size * 2) - space);
          g.draw(BottomRight);
     public void setSpacing(boolean spacingOn){
          if(spacingOn == false){
               space = 0;
          else{
               this.setSpacing(spacingOn,5f);
     public void setSpacing(boolean spacingOn,float gap){
          if(gap<2){
               gap = 2;
          if(spacingOn == true){
               space = size/gap;
     public void setLocation(float x,float y){
          this.x = x;
          this.y = y;
     public void setSize(float size){
          this.size = size;
class AppFrame extends JFrame{
     AppFrame(){
          this("Demo Frame");
     AppFrame(String title){
          super(title);
          setSize(500,500);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Similar Messages

  • How to create the exe file for java project.

    How to create the exe file for java project.
    am done the project in java swing , i like to create the project in exe format, so any one help for me,
    send the procedure for that.
    thanking u.

    How to create the exe file for java project.Have you ever heard of google? I pasted your exact "question" into a google search:
    http://www.google.com/search?q=How+to+create+the+exe+file+for+java+project.
    and got several useful links.
    Better search terms might yield even better results.
    Sheesh.

  • How to output the digital clock and synchronization signal from the NI USB-6211

    Hello,
    I need to connect the NI USB-6211 to control a digital to analog convertor chip (AD5541). However, this chip requires three input signals :1) Clock input, 2) Logic input or a synchronization signal  and 3) Signal Serial Data input (CS, SCLK, DIN).
    how to output the digital clock and the synchronization signal from the NI USB-6211?

    Hi SaberSaber,
    You should be able to use the counters to generate a pulse train that could be used for clock and synch purposes.  
    Hope this helps.  Let us know if you have more questions.  
    Dave C.
    Applications Engineer
    National Instruments

  • How to create the exe files for java application

    How to create the exe file for java application?
    got any software to do that?
    Thanks

    In terms of converting java applications into exe files, there are 3 schools of thought:
    1) Instead of converting it to an exe, convert it to a jar file. Jar files are more portable than exe files because they can be double-clicked on any operating system. The caveat is that a Java interpreter must be installed on the target computer for the double-clicking to work.
    http://developer.java.sun.com/developer/Books/javaprogramming/JAR/
    2) Create an exe launcher that, when double-clicked, attempts to find a Java interpreter on the local computer and launches the Java application. The exe file is still double-clickable but whether your java application runs depends on whether a Java interpretor is installed on the target computer.
    http://www.sureshotsoftware.com/exej/
    http://www.objects.com.au/products/jstart/index.jsp
    http://www.duckware.com/products/javatools.html
    http://www.ucware.com/jexec/
    http://www.rolemaker.dk/nonRoleMaker/javalauncher/
    http://www.jelude.cjb.net/
    http://thor.prohosting.com/~dfolly/java/index.html
    3) Create an exe launcher that bundles a Java interpretor. Same as above but when the exe file is double-clicked, it searches for a Java interpreter and if one is not found, it installs one on the target computer. The caveat is that the exe file would have an overhead of 10 MB in size for the interpreter.
    http://www.excelsior-usa.com/jet.html (evaluation version available)
    4) Convert the Java application into a native exe file. The caveat is that if you use Swing for your GUI, it won't be converted. Also this option is still somewhat experimental.
    Can't think of any free options right now.

  • How to get the system time in a Swing application ?

    I know how to do it in JavaScript, but not java. Please help me, and also how to get the system time in an Applet. Thanks !!!

    Check this link, I hope it helps
    http://202.71.136.142:8080/globalleafs/Swing/View.jsp?slno=22&tbl=0

  • How to create the setup file in java

    hi
    am new to java...
    is it possible to create setup file in java like in .net..
    if yes how?

    i mean our application can be installed in to any other system..
    using .net we can easily generate setup fileThe other people understood what you meant.
    And they suggested that you use one of the free
    installer generator, such as
    http://www.java-source.net/open-source/installer-generators

  • How to create the ini file for java programms

    hi all,
    I have one problem that is ...
    In my project I am using the JDBC connection ...
    in this I am connecting different connections (means differenet usernames and passwords)...
    for this evrey time I have to change the username and password in the
    hardcode ...
    I think that some ini file are there in java ...
    So if anyone knows about use of ini files in java please
    send me..
    thanks........

    In java they are called properties files.
    Look at java.util.Properties which provides methods for reading, writing, etc.

  • I want to  implement the Window autoresize in java swing application

    Hai...
    i want implement the window autoresize functionality in my java application. Same time i want to minimize and maximize my won window. and my java application develop one more frame at time disply in single window so i want disply the thaminal view formate in all frame
    Thanx to Advance.
    ARjun...

    ????????

  • HOw to create a Batch file for java application and whats the use of this ?

    HI,
    How to create a Batch file for java application ?
    And whats the use of creating batch file ?
    Thanks in advance

    First of all, you're OT.
    Second, you can find this everywhere in the net.
    If you got a manifest declaring main class (an classpath if needed), just create a file named whatever.bat, within same directory of jar file, containing:
    javaw -jar ./WhateverTheNameOfYourJarIs.jar %*By the way, assuming a Windows OS, you can just double click the jar file (no batch is needed).
    Otherwise use:
    javaw -cp listOfJarsAndDirectoriesSeparedBySemiColon country/company/application/package/className %*Where 'country/company/application/package/' just stands for a package path using '/' as separator instead of '.'
    Don't specify the .class extension.
    Javaw only works on Windows (you asked for batch, I assumed .BAT, no .sh), in Linux please use java.exe (path may be needed, Windows doesn't need it 'cause java's executables are copied to system32 folder in order to be always available, see PATH environment variable if you don't know what I'm talking about) and use ':' as classpath (cp) separator.
    The '%***' tail is there in order to pass all parameters, it only works on Windows, refer to your shell docs for other OSs (something like $* may work).
    This way you have a command you can call to launch your code (instead of opening NetBeans just to see your app working). You could schedule tasks on it or just call it in any command prompt (hope you know what it is 'cause there have been people in this very same forum with no clue about it, if not just hold the 'Windows button' and press 'R', then type 'cmd' and run it).
    Finally add dukes and give 'hem away.
    Bye.

  • How to create the namespace in java dictionory perspective?

    hi buddies,
          when i try to create the table in javadictinory pers
    pective.  Its is asking me to enter the name in the format of <NAMESPACE>_<SUFFIX>
      HOW TO CREATE THE NAMESPACE
         PLZ HELP ME
    HARI

    Hi,
    Go to Window->Preferences->Dictionary ->Nameserver Prefix
    and give the Prefix .
    Regards, Anilkumar

  • How to create a digital signature formy Applet?

    who can tell me how to create a digital signature for my applet? i want details of the process, because i know nothing about it. Thank you very much!!!

    http://java.sun.com/docs/books/tutorial/security1.2/apisign/gensig.html

  • How to create a "Explorer" like in java

    i'm just wonderin if there's a way on how to create "Windows Explorer" like in Java wherein you can view all your directories with yellow folder icon and also you can view all your files and drives....
    And when you double click the yellow folder icon it will then expand and will show all the subfolders and files ...
    How????
    Thanks!

    Yes, there is a way. Look up JTree in the API.-can you post some code.... i cant figure out on how to use JTree...
    i'm still confused on where will i get the entire directories of my drives...
    thanks

  • How to create a Batch file for java application  ?

    HI,
    How to create a Batch file for java application ?
    And whats the use of creating batch file ?
    Thanks in advance

    [http://static.springsource.org/spring-batch/]
    Assuming you want to develop a batch application rather than actually just create a .bat file ..

  • How to create the SWF file which was included surrogate pairs.

    I would like to make sure how to create the SWF file which are included surrogate pairs.
    The error of outside the scope of unicode occurred when I compiled(mxmlc) the as file which was set surrogate pairs to 'Unicoderange'.
    Ex, I set the '  '(UTF-16 is '20B9F') to 'Unicoderange'.
    However, I cannot add the surrogate pairs.
    The error of outside the scope of Unicode occurred when I compiled the AS file.
    The reason of error was that the Unicode(UTF-16) of surrogate pairs is invalid.
    Then, I read the following pages.
    These pages are written that unicode-range supported surrogate pairs.
    http://livedocs.adobe.com/flex/3/html/help.html?content=fonts_07.html
    http://www.w3.org/TR/1998/REC-CSS2-19980512/fonts.html#descdef-unicode -range
    So please let me know how to create the SWF file which are included surrogate pairs.
    Environment
    - Flex3.5a
    - TTC: meiryo(ver6.02), msgothic(ver2.50), mspgothic(ver2.50)
    Thanks,
    Takeshi Ishihara.

    I've been able to create project jar files in Eclipse by selecting Export from the File menu, then choose Java - Jar file - and provide the required information, name, destination folder etc. But when I do this, I do not export other jars on the build path this way nor do I expect to do this. I am only concerned with creating a jar file of my own work. A jar file does not "contain" other jar files. If the jar file contains the main method, and requires other jars for it's execution, then you may wish to include a reference to required jars on the class path entry of the Manifest file of the jar file you create. I'm not sure how to create a Manifest file in eclipse though..

  • How to create the log file in remote system using log4j.

    Hi,
    How to create the log file in remote system using log4j. please give me a sample code or related links.The below example i used for create the log file in remote system but it return the below exception.Is there any authandication parameter for accessing the remote path? Please help.
    public class Logging
    Logger log=null;
    FileAppender fileapp=null;
    public Logging(String classname)
    try
    log = Logger.getLogger(classname);
    String path=" [\\192.168.0.14\\c$\\LOG\\d9\\May_08_2008_log.txt|file://\\192.168.0.14\\c$\\LOG\\d9\\May_08_2008_log.txt]";
    fileapp = new FileAppender(new PatternLayout("%r [%t] %-5p %c %x - %m%n"),path, true);
    log.addAppender(fileapp);
    log.info("Logger initilized");
    }catch(Exception ex)
    ex.printStackTrace();
    java.io.FileNotFoundException: \\192.168.0.14\c$\LOG\d9\May_08_2008_log.txt (The network path was not found)
    at java.io.FileOutputStream.openAppend(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:290)
    at org.apache.log4j.FileAppender.<init>(FileAppender.java:109)
    at annwyn.logger.BioCapLogger.<init>(Logging.java:23)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Please help.
    Thanks in advance.
    Saravanan.K

    Sorry path is missing for the above request.
    path="\\192.168.0.14\c$\LOG\d9\May_08_2008_log.txt ";
    please help.
    Saravanan.K

Maybe you are looking for

  • GR/IRN REPORT urgent

    Hi guru's this is very urgent issue... i need GR/IR accounts for one particular vendor for this i use  Tcode fbl3n... i gave GR/IR no. i was dowloading this file sort it vendor wise. here WE , RE  two document type  is there which one i have to take

  • I double clicked on something and now the Firefox Menu button has disappeared. How do I retrieve it?

    I have been looking for information on how to save passwords. After I clicked on the Menu button, I double-clicked on an item in the list. Now the Menu button is gone. The name of the tab is at the top and under it the File, Edit... bar. I tried relo

  • ACS 5.4 backup status in syslog

    Have raised a TAC for this but thought I'd post here too. We are running ACS v5.4 Patch 1. We have noticed that ACS will not produce syslog messages about scheduled backups (success or failure) (1)    From the GUI, under "System Administration >  Con

  • Contract not what was agreed via email and phone

    Hi all, new to this forum so I'm hoping people here will be friendly and helpful . I recently (Oct '10) changed my phone back to BT and got a package deal along with my existing BB and Vision services. Spoke to a helpful guy called Tim Chappell who o

  • Chinese character don't work

    hey guy, can anyone tell me how to solve this problem, ...i been using the chinese character to type in corel draw and skype and etc... it's work fine but it doesn't show up the charater when i type since i made the 1st save of my document....i encou