// Exercise for week 8 HND 1st year java course...
import java.awt.*;
import java.applet.*;
public class Clock extends Applet
{
private int x_middle; // to hold centre of wdith of applet window
private int y_middle; // to hold centre of depth of applet window
private int length; // the length of the second hand
private double degrees_to_radians; // used to hold a conversion constant
public void paint(Graphics g)
{
// middle of the canvas,
// can be found as follows=>
// x_middle = getSize().width / 2;
// y_middle = getSize().height/ 2;
// length = getSize().width / 2 - 1; //Choose a length for the pendulum
x_middle = 150;
y_middle = 150;
length = 149;
degrees_to_radians = 3.14159 / 180.0;
// sin and cos work in units
// called RADIANS which are different from degrees.
// Basically there are 2 times pi of them (or 6.28) in 360 degrees
// which is a full circle.
// Draw a big circle
g.drawOval(0,0,299,299);
// Step the second hand through 360 degrees in 5 degree steps
for (int angle = 0 ; angle < 360; angle = angle + 5 )
{
// Draw second hand
g.drawLine(x_middle , y_middle ,
x_middle+(int)(Math.cos((double)angle*degrees_to_radians)*length),
y_middle+(int)(Math.sin((double)angle*degrees_to_radians)*length));
// put the delay loop here
} // end of for loop
} // end of paint function
} // end of 'clock' class
|