Full Version : Simple Clock Code
llamatronics >>Java >>Simple Clock Code


<< Prev | Next >>

Scott- 03-20-2006
Name: Clock Code
Copyright: Me
Preview: Here
(very top of board)

CODE
<center>
<center><b>Clock created in Java and copyright to Scott Clarke</b></center>
<br>
</br>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)
}

function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}

</script>
<b>
<center>The time is:
<body onload="startTime()">
<div id="txt"></div>
</b>
<br>
</br>


</center>

// do not remove copyright
// clock runs on your system clock so updates are automatic


I got bored in lesson so i tried a bit of java and here is a fully working clock, where the time is taken from your system clock.

Blazeix- 03-20-2006
This is a nice clock code, but you posted it in the wrong subforum. This code is JavaScript, while this is the java subforum. This should go in the web programming subforum.

BTW: The site that you linked to also said it was Java! Maybe you should notify them.

dreamer- 03-21-2006
ye

java and js are diff

but who cares?


nice code

Scott- 03-23-2006
i only just started so i presumed that they where just mutations of each other, thanks for the heads up *thumbs up*

Blazeix- 03-23-2006
They are actually quite different, JavaScript is a smaller language, and needs a browser to interpret it at runtime. Java is larger and more complicated, can stand alone or be incorporated into a browser, and needs a compiler to turn it into 'bytecode' for the java virtual machine to interpret. RuneScape, a popular online game, was programmed in java.

Here is the source code of a simple java clock that does the same thing that your javaScript clock does. You can see that while there are some similarities, it is actually quite different.

CODE

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


public class TextClock {

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


class TextClockWindow extends JFrame {

   private JTextField timeField;  // set by timer listener


   public TextClockWindow() {
       // Build the GUI - only one panel
       timeField = new JTextField(6);
       timeField.setFont(new Font("sansserif", Font.PLAIN, 48));

       Container content = this.getContentPane();
       content.setLayout(new FlowLayout());
       content.add(timeField);
       
       this.setTitle("Text Clock");
       this.pack();

       // Create a 1-second timer and action listener for it.
       // Specify package because there are two Timer classes
       javax.swing.Timer t = new javax.swing.Timer(1000,
             new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     Calendar now = Calendar.getInstance();
                     int h = now.get(Calendar.HOUR_OF_DAY);
                     int m = now.get(Calendar.MINUTE);
                     int s = now.get(Calendar.SECOND);
                     timeField.setText("" + h + ":" + m + ":" + s);
                 }
             });
       t.start();  // Start the timer
   }
}

Llama_in_the_herd- 03-29-2006
That looks awsome! llamatronics/crash.gif

Free Forum Hosting by Forumer.comTM!