CS 100: Lecture L11
March 2
Template for programs with User-Defined Graphics methods:
import java.awt.*;
public class ShowClock extends Frame
{
// Put methods here that operate on the ShowClock object:
// This method is called by the system whenever the window needs to be
// redrawn. g "does the drawing"
public void paint(Graphics g)
{
}
}
public class L11
{
public static void main(String args[])
{
// Create a ShowClock object. This is just a window, border, and title bar.
ShowClock d = new ShowClock();
d.resize(800,600);
d.move(0,75);
d.setTitle("Time");
d.show();
d.toFront();
}
}
The ShowClock Example
import java.awt.*;
public class ShowClock extends Frame
{
// Graphics g draws a clock face showing the time specified by hour and minute.
// The center is at (hc,vc) and the radius of the clock face is r.
public static void drawClock(Graphics g, int hc, int vc, int r, int hour, int min)
{
drawFace(g,hc,vc,r);
int rHour = (int) (.6*r); // Length of hour hand.
int angleHour = 90 - 30*hour - min/2; // Angle of hour hand.
drawRay(g,hc,vc,rHour,angleHour);
int rMin = (int) (.85*r); // Length of minute hand.
int angleMin = 90 - 6*min; // Angle of minute hand.
drawRay(g,hc,vc,rMin,angleMin);
}
// Graphics g draws a length r line segment from (hc,vc). The line segment
// makes a counter clockwise angle of theta degrees with the positive h-axis.
public static void drawRay(Graphics g, int hc, int vc, int r, int theta)
{
g.setColor(Color.black);
int h1 = (int)(hc + r*Math.cos(theta*Math.PI/180));
int v1 = (int)(vc - r*Math.sin(theta*Math.PI/180));
g.drawLine(hc,vc,h1,v1);
}
// Graphics g draws a filled disk of radius r and center (hc,vc).
public static void fillCircle(Graphics g,int hc,int vc, int r)
{
g.fillOval(hc-r,vc-r,2*r,2*r);
}
// Graphics g draws a designer clock face of radius r and center (hc,vc).
public static void drawFace(Graphics g, int hc, int vc, int r)
{
// Color the light gray face and black rim.
g.setColor(Color.black);
fillCircle(g,hc,vc,r);
g.setColor(Color.lightGray);
fillCircle(g,hc,vc,(int)(.9*r));
// Draw 60 equally spaced markers around the rim.
int hTick,vTick; // (hTick,vTick) = center of kth marker
double tau; // angle of kth marker.
int rTick = (int)(.03*r); // radius of the 5-minute markers
for(int k=0;k<=59;k++)
{
tau = 6*k*Math.PI/180;
hTick = (int)(hc + .95*r*Math.cos(tau));
vTick = (int)(vc - .95*r*Math.sin(tau));
if(k%5==0)
{
// Draw a 5-minute marker.
g.setColor(Color.red);
fillCircle(g,hTick,vTick,rTick);
}
else
{
// Draw a non 5-minute marker.
g.setColor(Color.blue);
fillCircle(g,hTick,vTick,rTick/2);
}
}
}
public void paint(Graphics g)
{
// Draw a clock
int hour = 12;
int min = 43;
drawClock(g,400,300,200,hour,min);
// "Pretty print" the exact time in digital form.
String s;
if (min<10)
s = hour + ":0" + min;
else
s = hour + ":" + min;
g.setFont(new Font("TimesRoman",Font.PLAIN,36));
g.drawString(s,360,550);
}
}
public class L11
{
public static void main(String args[])
{
ShowClock d = new ShowClock();
d.resize(800,600);
d.move(0,75);
d.setTitle("Time");
d.show();
d.toFront();
}
}