Java Clock
Results 1 to 5 of 5

Thread: Java Clock

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    590

    Post Java Clock

    Hey everyone,

    I'm just posting this clock/timer applet I created. It's nothing special, just a timer - I find it very usefull when I listen to the footy on the radio. If any Aussies here follow the AFL, hopefully you'll know what I mean. Well anyways, if you are learning java, I'm hoping this will be a good example for you. I find that example applets/applications are really good to learn from - you can't just read text books...too boring.

    Well, I'll post the source code here and hopefully my poor documentation will explain most of the things.

    (Note: It's java, even though I put it in PHP quotes - it just looked the best in them - I tried 'code' quotes, but it was all over the place.)
    PHP Code:
    //import the packages that will be used
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    //define the class Clock
    public class Clock extends JApplet implements ActionListener
    {
        
    //define all instance variables
        
    private Container pane;
        private 
    JPanel centersouth;
        private 
    JLabel hoursminutesseconds;
        private 
    JTextField hourValueminuteValuesecondValue;
        private 
    JButton startButtonstopButton;
        private 
    String theHourtheMinutetheSecond;
        private 
    Timer theTimer;

        
    //define the init method, which basically outlines what the applet looks like
        
    public void init()
        {
            
    //define the pane where everything will be added
            
    pane getContentPane();
            
    //define what layout the pane will use
            
    pane.setLayout(new BorderLayout());

            
    center = new JPanel();
            
    center.setLayout(new FlowLayout());
            
    pane.add(center"Center");
            
    south = new JPanel();
            
    south.setLayout(new FlowLayout());
            
    pane.add(south"South");

            
    //define the labels
            
    hours = new JLabel("Hours");
            
    minutes = new JLabel("Minutes");
            
    seconds = new JLabel("Seconds");

            
    //define the text fields - the actual timer
            
    hourValue = new JTextField("0"3);
            
    minuteValue = new JTextField("0"3);
            
    secondValue = new JTextField("0"3);

            
    //define the buttons to start and stop the timer
            
    startButton = new JButton("Start Timer");
            
    stopButton = new JButton("Stop Timer");

            
    //add the buttons to the action listener - so they fire an event when clicked
            
    startButton.addActionListener(this);
            
    stopButton.addActionListener(this);

            
    //add the buttons to the pane
            
    south.add(startButton);
            
    south.add(stopButton);

            
    //add all other objects to the pane
            
    center.add(hours);
            
    center.add(hourValue);
            
    center.add(minutes);
            
    center.add(minuteValue);
            
    center.add(seconds);
            
    center.add(secondValue);

            
    //create a timer that fires an event every second (1000 milliseconds)
            
    theTimer = new Timer(1000this);
        }

        
    //defines the method which is called when an event is fired
        
    public void actionPerformed(ActionEvent e)
        {
            
    //capture the source which fired the event
            
    Object event e.getSource();

            
    //define what to do when the start button is pushed
            
    if(event == startButton)
            {
                
    theTimer.start();
            }
            
    //define what to do when the stop button is pushed
            
    else if(event == stopButton)
            {
                
    theTimer.stop();
            }
            
    //define what to do when the timer fires the event (every 1000 milliseconds in this case)
            
    else if(event == theTimer)
            {
                
    addSecondValue();
            }
        }

        
    //define the method that increments the 'hours' section
        
    public void addHourValue()
        {
            
    theHour hourValue.getText();
            
    int theHourValue = (Integer.parseInt(theHour)) + 1;

            
    hourValue.setText(Integer.toString(theHourValue));
        }

        
    //define the method that increments the 'minutes' section
        
    public void addMinuteValue()
        {
            
    theMinute minuteValue.getText();
            
    String incrementedValue increment(theMinute);

            
    minuteValue.setText(incrementedValue);

            if(
    incrementedValue.equals("0"))
                
    addHourValue();
        }

        
    //define the method that increments the 'seconds' section
        
    public void addSecondValue()
        {
            
    theSecond secondValue.getText();
            
    String incrementedValue increment(theSecond);

            
    secondValue.setText(incrementedValue);

            if(
    incrementedValue.equals("0"))
                
    addMinuteValue();
        }

        
    //define the method that increments the seconds and minutes, reseting the value to '0' every 60 occurances
        
    public String increment(String value)
        {
            
    int theValue Integer.parseInt(value);

            if(
    theValue <= 58)
                
    theValue++;
            else
                
    theValue 0;

            return 
    Integer.toString(theValue);
        }

    And here is the ZIP file that contains the HTML file and the CLASS file so that you can view it (the JAVA file is there aswell - the source code). Just open it in any java-enabled browser and it should work fine. Any questions just reply and I'll be happy to help.

    EDIT: OK, so the ZIP file is attached in my post below (2 posts down).

    Cheers,
    Greg
    \"Do you know what people are most afraid of?
    What they don\'t understand.
    When we don\'t understand, we turn to our assumptions.\"
    -- William Forrester

  2. #2
    Banned
    Join Date
    Oct 2001
    Posts
    1,463
    Umm, Dude... Theres no ZIP file

  3. #3
    Senior Member
    Join Date
    Feb 2002
    Posts
    262
    ut oh were did the zip go?
    aislinn, Aria, BTBAM, chevelle, codeseven, Cky, dredg, evergreen terrace, from autumn to ashes,hopesfall, hxc, luti-kriss, nirvana, norma jean, shai hulud, this hero dies, tool, underoath, zao,

  4. #4
    Senior Member
    Join Date
    Dec 2001
    Posts
    590
    Yeah...I know. I actually didn't mean to post it yet. I clicked 'Preview Post' and it posted it...so I'm just trying to figure out whether this 'Preview Post' button is working or if it's a BUG!!??

    I edited twice, trying to add the zip...still not working. I'm not sure if it's just me or it's a bug with the site. Give us a few mins...

    EDIT: OK, the 'Preview Post' is working fine...I dunno, I swear I thought I hit it and it posted it...aw well, maybe I'm going crazy.

    Greg
    \"Do you know what people are most afraid of?
    What they don\'t understand.
    When we don\'t understand, we turn to our assumptions.\"
    -- William Forrester

  5. #5
    Senior Member
    Join Date
    Dec 2001
    Posts
    590
    OK, well, just using this app before, I had to reset each digit to "0" every quarter, so I figured I'd just chuck in a 'reset button'. So...I did and it's all sweet.

    I'm not gonna post it here, but was thinking, if you are really trying to learn java and are actually taking a look at this, add a reset button that resets each figure to "0". You gotta basically create another button object, add it to the pane, add the action listener and then define the event. If you want to be more fancy, disable the button when the timer is active and enable it when the timer has stoped - that only takes two lines of code - use the 'setEnabled(boolean variable)' method.

    And if you got any queries, just post em up.

    Greg
    \"Do you know what people are most afraid of?
    What they don\'t understand.
    When we don\'t understand, we turn to our assumptions.\"
    -- William Forrester

Posting Permissions

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