+ Reply to Thread
Results 1 to 3 of 3

Thread: Tutorial: Simple digital watch...

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Tutorial: Simple digital watch...

    Hello again !
    Today I thought I would share one of my "best" works. It took along while and was quite hard to understand and how to use it; But I have fixed it. So here is my Digital watch.(Sorry I can't explain everything quite good, am right now 50% understanding what I have done to get the watch work lol...)
    *Side note* It won't show updating time, only the time you requested for.(I have still along way to go with boring java.)

    So lets begin...
    Code:
    public class showtime {
    
    	private float t, m, s;//We setup instructions
    	private boolean showsec = true;//allow to show our box of time
    
    	public void set (float h, float min, float sec) {
    		
    		if (h>=0 && h<24 && min>=0 && min<60 && sec>=0 && sec <60) {//Counter
    			t=h;//defenitions
    			m=min;//defenitions
    			s=sec;//defenitions
    		}
    		else
    			System.out.println ("Wrong hour");//Error message
    
    	}
    
    	public void setshowsec(boolean show) {//function
    		showsec = show;
    	}
    
    	public float readh() {//function out of our defenition
    		return t;
    	}
    
    	public float readMin() {//function out of our defenition
    		return m;
    	}
    
    	public float readSec() {//function out of our defenition
    		return s;
    
    	}
    
    	public String toString () {//Print out time in right order,
    		String time = t + ":" + m;
    		if (showsec)
    			time = t + ":" + m + ":" + s;
    		return time;
    	}
    
    	public void tick() {
    		s = s + 1;
    
    		if (s==60){
    			s = 0;
    			m = m + 1;
    		}
    		if (m==60){
    			m = 0;
    			t = t + 1;
    		}
    		if (t==24){
    			t = 0;
    
    		}
    
    	}
    
    }
    This code was made logically and took along time to get right construction. Now due to my ability not to be able to explain how this code works; but still I will try and do my best!

    Save it on your folder where you keep your java files or so...

    Next step lets fix it so it can sync to our local machines time and get it out. This thing we made was kind of a counter, that will check the time and print out it for us(correct me if am wrong!)

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class whatsthetime {
    	public static void main (String[] arg) {
    	}
    }
    
    class thetime extends JFrame implements ActionListener {//Our time catcher
    
    	private showtime tp = new showtime ();//reading our timer we made
    	private JLabel l;
    
    	public thetime () {//new function
    		javax.swing.Timer h = new javax.swing.Timer (1000, this); h.start ();
    		Calendar c = Calendar.getInstance ();//function of our watch
    		tp.set (c.get (Calendar.HOUR_OF_DAY),//what to show 
    			c.get (Calendar.MINUTE),//what to show
    			c.get (Calendar.SECOND));//what to show
    
    		l = new JLabel (tp.toString(), JLabel.CENTER);//Gui menu
    		add (l);
    		l.setOpaque(true);//so it can be seen
    
    		l.setBackground (Color.black);//background color
    		l.setForeground (Color.white);//foreground color
    		l.setFont (new Font("SanSerif", Font.BOLD, 24));//text size
    		setSize (200,75);//box size
    		setVisible (true);//if it should be visible
    		setDefaultCloseOperation (EXIT_ON_CLOSE);//exit on our demand
    	}
    
    	public void actionPerformed (ActionEvent e) {//The catcher
    		tp.tick();
    		l.setText (tp.toString());//the outputer
    	}
    }
    Well this code is our main function to get out the time, now we can use it freely.

    Last part, to print out the time "we have catched"...
    Code:
    import java.util.*;
    import javax.swing.*;
    
    public class time {
    	public static void main (String[] arg) {
    
    		showtime tp = new showtime ();//Will look for our main function
    		Calendar c = Calendar.getInstance ();//Look up 
    		tp.set (c.get (Calendar.HOUR_OF_DAY),//
    				c.get (Calendar.MINUTE),//
    				c.get (Calendar.SECOND));//
    	JOptionPane.showMessageDialog (null, "The time is " + tp.toString());
    	}//Printout our time
    }
    So here is the whole how to printout the time in java. Myself this was kinda fun and challening. I might say this is my last thing for a while with java. So nowdays I will more of my lovely SICP.

    P.S, Sorry if I couldn't explain it for you guys as good I have to
    Anyways enjoy!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Tutorial: Simple digital watch...

    Nice tutorial! I would +rep but I've already given it to you last, so I can't again.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Tutorial: Simple digital watch...

    Lol, Lovely and thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Tutorial simple dice game
    By Turk4n in forum Java Tutorials
    Replies: 26
    Last Post: 05-31-2011, 12:33 PM
  2. Simple Stop Watch Application
    By Howdy_McGee in forum C# Programming
    Replies: 8
    Last Post: 03-20-2011, 12:32 PM
  3. JavaScript:Tutorial, Digital Clock
    By TcM in forum JavaScript Tutorials
    Replies: 11
    Last Post: 06-04-2009, 11:30 PM
  4. Tutorial - A Simple Stropwatch!
    By travy92 in forum Visual Basic Tutorials
    Replies: 19
    Last Post: 05-04-2009, 02:02 AM
  5. Tutorial paint dots(simple version)
    By Turk4n in forum Java Tutorials
    Replies: 6
    Last Post: 06-25-2008, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts