import java.awt.*;
import java.applet.*;
import java.util.*;
import java.net.*;
class Hms extends Date //Hms类是进行时间换算的类
{
public Hms(double localOffset){ //如果HTML文件中设置了时区,则把时间设为当地时区时间
super();
long tzOffset=getTimezoneOffset()*60L*1000L;
localOffset *= 3600000.0;
setTime(getTime() + tzOffset + (long)localOffset);
}
public Hms(){ //如果没有设置时区,则就使用当地的时间
super();
}
public double get_hours() //时间换算,把如5:30这样的时间换算成5.5
{
return (double)super.getHours()+(double)getMinutes()/60.0;
}
}
abstract class ClockHand //绘制指针的抽象基类,同时提供将角度转换为坐标的方法
{
protected int baseX[], baseY[];
protected int transX[],transY[];
protected int numberOfPoints;
public ClockHand(int originX, int originY, int length,int thickness,int points){
baseX= new int[points]; baseY=new int[points];
transX= new int[points]; transY=new int[points];
initiallizePoints(originX,originY,length,thickness);
numberOfPoints=points;
}
abstract protected void initiallizePoints( int originX,
int originY,
int length,
int thickness);
abstract public void draw(Color color, double angle, Graphics g);
protected void transform(double angle) //通过角度计算出时针、分针、秒针的绘制位置
{
for(int i=0;i<numberOfPoints;i++){
transX[i]=(int)((baseX[0]-baseX[i]) * Math.cos(angle) -(baseY[0]-baseY[i]) * Math.sin(angle) +baseX[0]);
transY[i]=(int)((baseX[0]-baseX[i]) * Math.sin(angle) +(baseY[0]-baseY[i]) * Math.cos(angle) +baseY[0]);
}
}
}
class SweepHand extends ClockHand //秒针绘制使用的类
{
public SweepHand(int originX,int originY, int length, int points)
{
super(originX,originY,length,0,points);
}
protected void initiallizePoints(int originX,int originY, int length, int unused) //初始化点
{
baseX[0]=originX; baseY[0]=originY;
baseX[1]=originX; baseY[1]=originY-length/5;
baseX[2]=originX; baseY[2]=originY+length;
}
public void draw(Color color, double angle, Graphics g) //绘制秒针,为线段
{
transform(angle);
g.setColor(color);
g.drawLine(transX[1],transY[1],transX[2],transY[2]);
}
}
class HmHand extends ClockHand //分针时针使用的类
{
public HmHand(int originX,int originY, int length,int thickness, int points){
super(originX,originY,length,thickness,points);
}
protected void initiallizePoints( int originX, //初始化点
int originY,
int length,
int thickness)
{
baseX[0]=originX;
baseY[0]=originY;
baseX[1]=baseX[0]-thickness/2;
baseY[1]=baseY[0]+thickness/2;
baseX[2]=baseX[1];
baseY[2]=baseY[0]+length- thickness;
baseX[3]=baseX[0];
baseY[3]=baseY[0]+length;
baseX[4]=baseX[0]+thickness/2;
baseY[4]=baseY[2];
baseX[5]=baseX[4];
baseY[5]=baseY[1];
}
public void draw(Color color,double angle, Graphics g) //绘制时针分针,为多边形
{
transform(angle);
g.setColor(color);
g.fillPolygon(transX,transY,numberOfPoints);
}
}
public class Clock extends Applet implements Runnable
{
//some DEFINE'd constants
static final int BACKGROUND=0; //Background image index
static final int LOGO=1; //Logo image index
static final String JAVEX="J***X"; //Default text on clock face
static final double MINSEC=0.104719755; //Radians per minute or second
static final double HOUR=0.523598776; //Radians per hour
Thread clockThread = null;
//User options, see getParameterInfo(), below.
int width = 100;
int height = 100;
Color bgColor = new Color(0,0,0);
Color faceColor = new Color(0,0,0);
Color sweepColor = new Color(255,0,0);
Color minuteColor = new Color (192,192,192);
Color hourColor = new Color (255,255,255);
Color textColor = new Color (255,255,255);
Color caseColor = new Color (0,0,0);
Color trimColor = new Color (192,192,192);
String logoString=null;
Image images[] = new Image[2]; //Array to hold optional images
boolean isPainted=false; //Force painting on first update, if not painted
//Center point of clock
int x1,y1;
//Array of points for triangular icon at 12:00
int xPoints[]=new int[3], yPoints[]=new int[3];
//Class to hold time, with method to return (double)(hours + minutes/60)
Hms cur_time;
//The clock's seconds, minutes, and hours hands.
SweepHand sweep;
HmHand minuteHand,
hourHand;
//The last parameters used to draw the hands.
double lastHour;
int lastMinute,lastSecond;
//The font used for text and date.
Font font;
//Offscreen image and device context, for buffered output.
Image offScrImage;
Graphics offScrGC;
// Use to test background image, if any.
MediaTracker tracker;
int minDimension; // Ensure a round clock if applet is not square.
int originX; // Upper left corner of a square enclosing the clock
int originY; // with respect to applet area.
double tzDifference=0;
boolean localOnly=false;
//Users' parameters - self-explanatory?
public String[][] getParameterInfo()
{
String[][] info = {
{"width", "int", "width of the applet, in pixels"},
{"height", "int", "height of the applet, in pixels"},
{"bgColor", "string", "hex color triplet of the background, i.e. 000000 for black <black>"},
{"faceColor", "string", "hex color triplet of clock face, i.e. 000000 for black <black>"},
{"sweepColor", "string", "hex color triplet of seconds hand, i.e. FF0000 for red <red>"},
{"minuteColor", "string", "hex color triplet of minutes hand, i.e. C0C0C0 for lt.gray <lt.gray>"},
{"hourColor", "string", "hex color triplet of hours hand, i.e. FFFFFF for white <white>"},
{"textColor", "string", "hex color triplet of numbers, etc., i.e. FFFFFF for white <white>"},
{"caseColor", "string", "hex color triplet of case, i.e. 000000 for black <black>"},
{"trimColor", "string", "hex color triplet of case outliners, i.e. C0C0C0 for lt.gray <lt.gray>"},
{"bgImageURL", "string", "URL of background image, if any <null>"},
{"logoString", "string", "Name to display on watch face <JAVEX>"},
{"logoImageURL","string", "URL of logo image to display on watch face <null>"},
{"timezone", "real", "Timezone difference from GMT (decimal hours,- West/+ East)<0>"},
{"localonly", "int", "Non-zero will cause clock to display current local time <0>"}
};
return info;
}
//Applet name, author and info lines
public String getAppletInfo()
{
return "billsClock 1.05 (C) 1996-98 by Bill Giel<bgiel@ct2.nai.net>";
}
void showURLerror(Exception e)
{
String errorMsg = "JAVEX URL error: "+e;
showStatus(errorMsg);
System.err.println(errorMsg);
}
// This lets us create clocks of various sizes, but with the same
// proportions.
private int size(int percent)
{
return (int)((double)percent/100.0 * (double)minDimension);
}
public void init()
{