Import java.awt.*;
Import Java.util.Date;
Import javax.swing.*; public class Clock extends jcomponent{/** * */private static final long Serialversionuid =-53794
72973578609775L;
Private font f = new font ("Microsoft Ya Hei", font.plain,15);
Private font f2 = new Font ("Microsoft Ya Hei", font.bold,15);
Private JLabel L = new JLabel ("Current Time:");
Private JLabel display = new JLabel ();
Private JLabel display2 = new JLabel ("");
private int hour = 0;
private int min = 0;
private int sec = 0;
Private date now = new Date ();
Private graphics2d G;
Final double PI = Math.PI;
Private String Strtime = "";
@SuppressWarnings ("deprecation") public Clock () {Add (L);
L.setbounds (120, 320, 80, 20);
L.setfont (f);
Add (display);
Display.setbounds (195, 320, 80, 20);
Display.setfont (f);
Display.setborder (Borderfactory.createlineborder (Color.Black));
Add (Display2); Display2.SetBounds (90, 350, 250, 20);
Display2.setfont (f);
hour = Now.gethours ();
min = Now.getminutes ();
SEC = Now.getseconds ();
SetVisible (TRUE);
} public void Paintcomponent (Graphics g1) {double x,y;
Super.paintcomponent (G1);
g = (graphics2d) G1;
Anti-sawtooth Switch open g.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on);
Draw the Dial G.setpaint (new Gradientpaint (5,40,color.red,15,50,color.yellow,true));
G.setstroke (New Basicstroke (3,basicstroke.cap_butt,basicstroke.join_bevel));
G.drawoval (75, 40, 250, 250);
G.filloval (195, 160, 10, 10);
G.setcolor (Color.Black);
Draw 60 dots for (int i = 0;i < 60;i++) {double[] co = new double[2];
CO = Paint_dot (i * 2 * pi/60);
x = co[0];
y = co[1];
if (i = = 0 | | | | | | | | i = = 45)/Draw 3,6,9,12 four big points {g.filloval ((int) (x-5 +), (int) (y-5 + 165), 10,10);
else//Other dots {g.filloval (int) (x-2.5 +), (int) (y-2.5 + 165), 3, 3);
///Draw four digital g.setfont (F2);
g.DrawString ("3", 300, 171);
g.DrawString ("6", 195, 273);
g.DrawString ("9", 91, 171); g.DrawString ("12", 195, 68);
g.DrawString ("12", 190, 68); Draw the hour hand, the minute hand, the second hand paint_hourpointer (hour*3600 + min*60 + sec,g);/The number of seconds in the hour Paint_minutepointer (min*60 + sec,g);
The minute hand traversed the number of seconds Paint_secondpointer (SEC,G);//seconds passed by second hand} public void ShowUI () {new Thread () {
@SuppressWarnings ("deprecation") public void Run () {while (true) {
now = new Date ();
hour = Now.gethours ();
min = Now.getminutes (); SEC = Now.getseConds ();
try {thread.sleep (1000);
catch (Interruptedexception ex) {ex.printstacktrace ();
} showTime ();
Repaint ();
}}.start (); The public void paint_hourpointer (int second,graphics2d g) {//second indicates how many seconds to walk the hour of the current time relative to 00:00:00 X,y,angl
E
Angle = Second * pi/21600;//The speed of the clockwise is pi/21600 (rad/s) x = + Math.sin (angle);
y = 165-60 * Math.Cos (angle);
G.setstroke (New Basicstroke (5,basicstroke.cap_butt,basicstroke.join_round));
G.setpaint (New Gradientpaint (200,165,color.red,260,165,color.blue,true));
G.drawline (165, (int) x, (int) y); The public void paint_minutepointer (int second,graphics2d g) {//second indicates how many seconds the minute hand of the current time has gone in relation to 00:00:00 X,y,an
Gle
Angle = second * pi/1800;//minute hand speed is pi/1800 (rad/s)x = + * Math.sin (angle);
y = 165-80 * Math.Cos (angle);
G.setstroke (New Basicstroke (3,basicstroke.cap_butt,basicstroke.join_round));
G.setpaint (New Gradientpaint (200,165,color.magenta,280,165,color.blue,true));
G.drawline (165, (int) x, (int) y); The public void paint_secondpointer (int second,graphics2d g) {//second indicates how many seconds the second hand of the current time has gone in relation to 00:00:00 x,y,x1
, Y1,x2,y2,x3,y3,angle;
Double cos = 90/math.sqrt (8125);//90*90+5*5 double sin = 5/math.sqrt (8125);
Angle = Second * pi/30;//The speed of the clockwise is Pi/30 (rad/s) x = + Math.sin (angle);
y = 165-95 * Math.Cos (angle);
X1 = + * Math.sin (angle + PI);
Y1 = 165-20 * Math.Cos (angle + PI); x2 = + MATH.SQRT (8125) * (Math.sin (angle) *cos-math.cos (angle) *sin); Sin (a-b) y2 = 165-math.sqrt (8125) * (Math.Cos (angle) *cos + math.sin (angle) *sin); cos (a-b) x3 = + MATH.SQRT (8125) * (Math.sin (angLe) *cos + math.cos (angle) *sin); Sin (a+b) y3 = 165-math.sqrt (8125) * (Math.Cos (angle) *cos-math.sin (angle) *sin);
cos (a+b) g.setstroke (new Basicstroke (2,basicstroke.cap_butt,basicstroke.join_bevel));
G.setpaint (New Gradientpaint (180,165,color.cyan,295,165,color.magenta,true));
G.drawline (int) x1, (int) y1, (int) x, (int) y);
G.drawline (int) x2, (int) y2, (int) x, (int) y);
G.drawline (int) x3, (int) y3, (int) x, (int) y);
Public double[] Paint_dot (double angle) {double[] co = new double[2];
Co[0] = Math.Cos (angle),//horizontal axis CO[1] = * * Math.sin (angle);/ordinate return co;
@SuppressWarnings ("deprecation") private void ShowTime () {String date;
int hour_temp = Hour,min_temp = Min,sec_temp = sec;
Sec_temp + 1;
if (sec_temp >=) {sec_temp = 0;
Min_temp + 1; } if (min_temp>=60) {mIn_temp=0;
Hour_temp+=1;
} if (Hour_temp <) Strtime = "0" + hour_temp + ":";
else Strtime = "" + hour_temp + ":";
if (Min_temp <) Strtime = Strtime + "0" + min_temp + ":";
else Strtime = Strtime + "" + Min_temp + ":";
if (sec < a) Strtime = Strtime + "0" + sec_temp;
else Strtime = Strtime + "" + sec_temp;
Set the display time on the form date = "" + (Now.getyear () +1900) + "year" + (Now.getmonth () +1) + "month" + now.getdate () + "Day" + "Week";
Switch (Now.getday ()) {Case 1:date = "one";
Break
Case 2:date + = "two";
Break
Case 3:date + = "three";
Break
Case 4:date + = "four";
Break
Case 5:date + = "Five";
Break
Case 6:date + = "Six"; Break;
Case 7:date = "Day";
Break Date = = "CST";
Chinese Standard Time Strtime = "" + strtime;
Display.settext (Strtime);
Display2.settext (date);
public static void Main (String args[]) {Clock c = new Clock ();
C.showui ();
JFrame f = new JFrame ("Yunyaniu");
Image Img=toolkit.getdefaulttoolkit (). GetImage ("Image/logo small. jpg");//Window Icon F.seticonimage (IMG);
F.setsize (400,420);
F.setresizable (FALSE);
F.add (c, Borderlayout.center);
F.setlocationrelativeto (NULL);
F.setdefaultcloseoperation (Jframe.exit_on_close);
F.setvisible (TRUE); }
}