PDA

View Full Version : (Not GScript) A simple clock


godofwarares
03-05-2007, 04:28 PM
I made this out of pure free time. It's Java so it won't work on Graal -- Sorry guys. You might be able to get some good ideas / good formulas off of this however.

Alert: This is an applet. You can use it on a webpage by compiling it and using this code:

<APPLET CODE=Clock.class WIDTH=100 HEIGHT=100>
</APPLET>




public class Clock extends javax.swing.JApplet
{
public int clockX;
public int clockY;
public int clockW;
public int clockR;
public double sa;
public double sda;
public double mda;
public double hda;
public int sr;
public int mr;
public int hr;
public int s;
public int m;
public int h;
public int secondX;
public int secondY;
public int minuteX;
public int minuteY;
public int hourX;
public int hourY;

public void init()
{
// Start the timer; Variables here don't matter
// since they're in the Paint method.

javax.swing.Timer clock = new javax.swing.Timer(500, updateClock);
clock.start();
}

public void paint(java.awt.Graphics g)
{
clockX = (getWidth()/2);
clockY = (getHeight()/2);

clockW = (getWidth()-1);
clockR = ((getWidth()-1) / 2);

sa = (Math.PI / 2);
sda = (Math.PI / 30);
mda = (Math.PI / 30);
hda = (Math.PI / 6);

sr = clockR;
mr = (int) (0.9 * clockR);
hr = (int) (0.7 * clockR);

g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(new java.awt.Color(255, 255, 255));

java.util.Date d = new java.util.Date();

s = d.getSeconds();
m = d.getMinutes();
h = d.getHours();

secondX = (int) ((getWidth() / 2) + Math.cos((s * sda) - sa) * sr);
secondY = (int) ((getWidth() / 2) + Math.sin((s * sda) - sa) * sr);

minuteX = (int) ((getWidth() / 2) + Math.cos((m * mda) - sa) * mr);
minuteY = (int) ((getWidth() / 2) + Math.sin((m * mda) - sa) * mr);

hourX = (int) ((getWidth() / 2) + Math.cos((h * hda) - sa) * hr);
hourY = (int) ((getWidth() / 2) + Math.sin((h * hda) - sa) * hr);

g.drawLine( (getWidth() / 2), (getHeight() / 2), secondX, secondY );
g.drawLine( (getWidth() / 2), (getHeight() / 2), minuteX, minuteY );
g.drawLine( (getWidth() / 2), (getHeight() / 2), hourX, hourY );

g.drawOval(0, 0, (getWidth()-1), getHeight()-1);

String stime = (h % 12 + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s) + " " + (h > 11 ? "PM" : "AM"));
showStatus("The time is now: " + stime + ".");
}

java.awt.event.ActionListener updateClock = new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
repaint();
}
};
}


Just in case nobody has compilers, Forum PM me and i'll give you the class file.

Enjoy.



Note: I KNOW THIS ISN'T A JAVA FORUM.
I just thought it would help someone >_>

Zeta0
03-05-2007, 04:32 PM
gk hik repped

Skyld
03-05-2007, 05:58 PM
I'll let the thread stay since the formulae might help some people, but ideally you shouldn't post code in different languages here.

godofwarares
03-05-2007, 07:09 PM
Alright