import j**a.awt.*;import j**a.applet.applet;import j**a.util.calendar;import j**a.text.simpledateformat;import j**a.util.date;public class clockapplet extends applet implements runnable //applet支持线程{ private thread athread; //线程 private simpledateformat sdateformat; //日期格式public void init() { th**.setbackground(color.white);//背景颜色设为白色 th**.athread = **; } public void paint(graphics g) { th**.sdateformat = new simpledateformat("hh时mm分ss秒"); g.drawstring(th**.sdateformat.format(new date()),25,131); calendar rightnow = calendar.getinstance(); int second = rightnow.get(calendar.second); int minute = rightnow.get(calendar.minute); int hour = rightnow.get(calendar.hour); //半径 int r_h = 20,r_m = 4,r_s = 4; //时针的坐标 //x ====(9-3)[0-6] (3-9)[6-0] //y ====(12-6)[0-6] (6-12)[6-0] int h_x ; int h_y; //x if(hour == 0) { hour = 12; } if( hour >= 3 && hour <= 9 ) { h_x = r_h*math.abs(hour - 9); } else { if(hour > 9) { h_x = r_h*math.abs(hour - 9); } else { h_x = r_h*math.abs(hour+3); } } //y if( hour >= 6 && hour <= 12 ) { h_y = r_h*math.abs(hour - 12); } else { h_y = r_h*hour; } //分针的坐标 int m_x; int m_y; if(minute == 0) { minute = 60; } if( minute >= 15 && minute <= 45 ) { m_x = r_m*math.abs(minute - 45); } else { if(minute > 45) { m_x = r_m*math.abs(minute - 45); } else { m_x = r_m*math.abs(minute+15); } } //y if( minute >= 30 && minute < 60 ) { m_y = r_m*math.abs(minute - 60); } else { m_y = r_m*minute; } //秒针的坐标 int s_x; int s_y; if(second == 0) { second = 60; } if( second >= 15 && second <= 45 ) { s_x = r_s*math.abs(second - 45); } else { if(second > 45) { s_x = r_s*math.abs(second - 45); } else { s_x = r_s*math.abs(second+15); } } //y if( second >= 30 && second <= 60 ) { s_y = r_s*math.abs(second - 60); } else { s_y = r_s*second; }// g.drawstring(string.valueof(second),25,50);// g.drawstring(string.valueof(minute),25,60); // g.drawstring(string.valueof(hour),25,70); // g.drawstring(string.valueof(h_x),25,80); // g.drawstring(string.valueof(h_y),25,90); g.drawoval(0,0,120,120);//距离相差10像素 g.setcolor(color.darkgray); g.drawstring("9",5,65); g.drawstring("3",110,65); g.drawstring("12",55,15); g.drawstring("6",55,115); g.drawstring("1",80,20); g.drawstring("2",100,40); g.drawstring("4",100,90); g.drawstring("5",80,110); g.drawstring("7",30,110); g.drawstring("8",10,90); g.drawstring("10",10,40); g.drawstring("11",30,20); g.setcolor(color.red); g.drawline(60,60,h_x,h_y);//前一个点表示起点,另一个表示终点 g.setcolor(color.blue); g.drawline(60,60,m_x,m_y); g.setcolor(color.yellow); g.drawline(60,60,s_x,s_y);} public void start() { if(athread == **) { athread = new thread(th**); athread.start(); } } public void stop() { if(athread != **) { athread.interrupt(); athread = **; } } public void run() { while(athread != **) { repaint(); try { athread.sleep(1000); } catch(interruptedexception e) { } } }} 20210311