Digital Clock in Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Digital Clock in Java

  1. #1
    Join Date
    Oct 2004
    Posts
    10

    Digital Clock in Java

    I want to write a program that basically displays a digital clock. I dont want this to be an applet either, I have found tons of those on the web but its not what I am looking for. I just want some simple source code for a program that looks at my computers system clock and presents a digital display of the current time. Please post source code or any help you can offer.

  2. #2
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    In my opinion starting something then submitting it on the forum with get you a lot of help ;
    but droping what YOU WANT would get you nowhere near the solution to you problem;
    for the simple reason that ppl are volonteering here
    best regards

  3. #3
    Join Date
    Oct 2004
    Posts
    10
    Well I dont expect anyone to run out and write the program just for me. I was hoping for someone who has written a program like this already and could just offer the source. I will try to write it myself later if I cant get any help. Just trying to save myself sometime. I dont need to write the program to learn, I just need to use it.

  4. #4
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    Well i really didn't mean it to hurt you , i was trying to tell you what i think could get you quick help
    I'm deeply sorry i
    best regard

  5. #5
    Join Date
    Oct 2004
    Posts
    10
    Pervert17,

    Thanks for the apology. I know that you were just trying to help me and I snapped at you. I was wrong and I hope you didnt take my comments to heart. Now I am the one that should apologize. There is no excuse for my actions. I wish you the best in all your endeavors. May your heart be filled with happiness and your kitchen floors always be clean.

    All the best

  6. #6
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    Well
    now you got me red, i
    i do appreciat your ability to re-think about issues ;
    this atittude would make you the winner,
    i now dicided to work on your case
    just tell me weither you want it on applet or in Frame
    so i could get to work
    best regard

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Here is a nice fat clock for you. The Clockpanel does all the work

    IMPORTANT A: This bugger runs a thread and wont stop when you close the window, the stopClock method must be called or it will keeeeeeeep on. (check Krams answer on 'closing a window').

    IMPORTANT B: Don't use this method for animations.

    import javax.swing.*;
    import java.awt.*;
    class ClockPanel extends JPanel implements Runnable {
    Font fnt=new Font("monospace",Font.PLAIN,25);
    private boolean isTicking=false;

    private String getTimeString() {
    return new java.util.Date(System.currentTimeMillis()).toString();
    }
    public void startClock() {
    Thread t=new Thread (this);
    setTicking(true);
    t.start();
    }
    private synchronized void setTicking(boolean isTicking) {
    this.isTicking=isTicking;
    }
    private synchronized boolean getTicking() {
    return isTicking;
    }
    public void stopClock() {
    setTicking(false);
    }
    public void run() {
    while (getTicking()) {
    try {
    Thread.currentThread().sleep(1000);
    this.repaint();
    } catch (InterruptedException ie) { // does NOT happend when you clode the frame !!!!!
    return;
    }
    }
    }
    public void paint(Graphics g) {
    g.setColor(Color.YELLOW);
    g.fillRect(0,0,this.getSize().width,this.getSize().height);
    g.setFont(fnt);
    g.setColor(Color.BLUE);
    g.drawString(getTimeString(),20,30);
    }
    }
    public class DigiClock extends JFrame {

    ClockPanel theClock = new ClockPanel();
    public DigiClock() {
    this.setTitle("A nice clock");
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(theClock,BorderLayout.CENTER);
    theClock.startClock();
    }
    public static void main(String[] args) {
    DigiClock dc=new DigiClock();
    dc.setBounds(20,50,400,70);
    dc.setVisible(true);
    }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center