This article introduces the dynamic simulation clock using JFRE, draw clocks in the panel and extract the system at the current moment. The main method will stop the thread for 1 second and refresh the panel.
Implement code as follows
Import javax.swing.*; Import Java.awt.*; Import Java.util.*; Import Java.lang.thread; Import Java.text.DECIMALFORMAT; OCK EXTENDS JPANEL { /** * @param ARGS * / Private int Hour; Private int Minute; Private int Second; // Construct function public stillClock () {setcurrenTtime ();} // return the hour public into get get gethour () {Ret urn house;} public int getminute () {Return minute;} Public int GetSecond () {Return Second;} // Draw the clock ProteCTD VOID PAINTCOMPONENT (Graphics G) {Super. Paintcomponent (g); // Initialize int ClockRadius = (int ) (Math.min (getwidth (), getheight () ) * 0.8 * 0.5); int Xcenter = getWidth () / 2; int YCenter = Getheight () / 2; // Drawing g.SetColor (color.black); g.Drawoval (XCenter -CLOCKRADIUS, Y Center -CLOCKRADIUS, 2 * Clockradius, 2 * CLOCKRADIUS); G.Drawstring ("12", xcenter -5, Ycenter -Clockradius + 15); G.Drawstring ("9", XCENTER -Clockradius + 3, Ycentr + 5); G.Drawstring ("3", XCenter + CLOCKRADIUS -10, Ycenter + 3); G.Drawstring ("6", XCenter -3, Ycenter + Clockradius -3); // The second point intoxid = (int) (CLOCKRADIUS * 0. 8) ; int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60))); int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.pi /60))); g.SetColor (color.red); g.Drawline (xcenter, ycenter, xsecond, ysecond); // Draw a point of intranom = (int) (CLOCKRADIUS * 0.65); int xmin UTE = (int) (xcenter + mlenth * math.sin (minute * (2 * math.pi / 60)); int yminute = (int) (xcenter -mlenth * math.cos (minute * (2 * math.pi / 60))); g.SetColor (color.blue); g.Drawline (xcenter, ycenter, xminute, yminute); // painting timest needle int hlength = (int) (clockradius * 0.5); int xhour = ( int) (int) ( Xcenter + Hlength * Math.sin minute / 60.0) * (2 * math.pi / 12)); g.SetColor (color.green); g.Drawline (Xcenter, Ycenter, XHour, YHOUR); // Draw a digital clock g.SetColor (color. Black); decimalFormat s = New DecimalFormat ("00"); g.format (gethour (getHour ()) + ":" + s.Format (Getminute ()) + ": + s.Format (Getsecond )), XCenter -22, Ycenter -CLOCKRADIUS -15);} Public Void Setcurrenttime () {Calendar Calendar = New GregorianCalendar (); this.hour = Calendar.Get (Calen dar.hour_of_dy); This.minute = Calendar.get ( Calendar.minute); This.Second = Calendar.get (Calendar.Second);} Public Static Void Main (String [] ARGS) {// Todo-Generatd Method Stub JFRAME FRA me = new jframe ("diaplayClock"); Frame .setResizable (FALSE); Frame.Settitle ("DiaplayClock"); Frame.SetDefaultCloseoperation (JFRAME.EXIT_ON_CLOSE) (300,350); Frame.setvisible (true); While (TRUE) {StillClock CLOCK = New StillClock () ; Frame.getContentPane (). add (clock); Clock.setvisible (true); frame.validate (); Try {thread.sleep (1000);} Catch (InterruptedException E) {e.printstacktrace ();} clock. setvisible (false); frame.remove (clock); clock = null; frame.validate ();}}Effect map:
The above is all the contents of this article. I hope everyone can like it.