|
Clock Canvas
java.awt.*;
class ClockCanvas extends Canvas
{
int minutes;
void paint(Graphics g)
{
g.drawOval(0,0,100,100);
double hourAngle = 2 * Math.PI*(minutes-3*60)/(12*60);
double minuteAngle = 2 * Math.PI * (minutes - 15) / 60;
g.drawLine(50,50,50+(int)(30*Math.cos(hourAngle)),50 + (int)(30*Math.sin(hourAngle)));
g.drawLine(50,50,50+(int)(45*Math.cos(minuteAngle)), 50 + (int)(45*minuteAngle)));
}
public void tick()
{
minutes++;
repaint();
}
void reset ( )
{
minutes = 0;
repaint();
}
}
'
********************************************************************
' Clock Testing
'
********************************************************************
import java.awt.*;
public class ClockTesting extends Frame
{
ClockCanvas clk; public ClockTesting()
{
setTitle("Testing the Panel and Canvas with a
Clock"); Panel p;
p = new Panel();
p.setLayout(new FlowLayout());
Button tick,reset;close;
tick = new Button ("Tick");
reset = new Button (°Reset");
close = new Button ('Close");
p.add(tick);
p.add(reset);
p.add(close);
add ("South", p ) ;
clk = new ClockCanvas();
add ("North", clk) ;
clk.reshape(0,0,300,300);
clk.show();
public boolean handleEvent(Event e)
{
if (e.id == Event.WINDOW~DESTFtOY)
{
System.exit(0);
return super.handleEvent(e);
}
public boolean action(Event e, Object arg)
{
if (arg. equals ("Tick") )
clk.tick();
if (arg.equals ("Reset") )
clk.reset();
if (arg.equals ("Close") )
System.exit(0);
else
return super.action(e,arg);
repaint();
return true;
}
public static void main(String arg[])
{
ClockTesting quartz;
quartz = new ClockTesting();
quartz.resize(400,400);
quartz.show();
}
}
Output
:
This code used to see the clock.
|