1 ImportJava.awt.Color;2 ImportJava.awt.Font;3 ImportJava.awt.Graphics;4 ImportJava.util.Calendar;5 ImportJava.util.GregorianCalendar;6 7 ImportJavax.swing.JFrame;8 ImportJavax.swing.JPanel;9 Ten /** One * A * @authorYycat - * - */ the Public classTestimitateclockextendsjpanel{ - - Private Static Final LongSerialversionuid = 1L; - + Public Static Final intDefault_height = 350;//Height of Panel - Public Static Final intDefault_width = 300;//width of the panel + Public Static Final intdefault_x = 100;//Panel initial position A Public Static Final intDefault_y = 50; at Public Static Final intpadding_x = 30; - Public Static Final intPadding_y = 60; - Public Static Final intClockradius = (default_width-60)/2;//defines the radius radius of the clock circle - intXcenter = padding_x + Clockradius;//The center coordinate of the clock circle - intYcenter = padding_y +Clockradius; - in /** - * Define the time variable to calculate the corresponding position of the hour, minute and second hand to */ + Private intsecond, minute, hour; - the PublicTestimitateclock () { * inittime (); $ }Panax Notoginseng - the + Public voidPaint (Graphics g) { A /** the * Draw the clock contour + * define RADIUS clockradius:120px; - */ $ $ //int clockradius = default_width-60; //radius is (300-600)/2 -G.drawoval (padding_x, padding_y, clockradius*2, clockradius*2); - the Paintclocknum (g); - Wuyi Paintclockhands (g); the - Wu } - About /** $ * Draw Clock numbers - * @paramg - */ - Public voidPaintclocknum (Graphics g) { A + intStrlength = Xcenter-padding_x; theFont Afterfont =NewFont ("Arial", Font.Bold, 14); -Font Beforef =G.getfont (); $ the for(inti=0; i<12; i++){ the if(i%3==0) {//fixed position digit bold display the G.setfont (afterfont); the } -g.DrawString (i*1 + "", (int) (Xcenter + strlength*math.sin (2*math.pi/12*i)), (int) (Ycenter + strlength*math.cos (Math.PI + 2*math.pi/12*i))); in G.setfont (beforef); the } the About } the the /** the * Draw hour, minute and second hand + */ - Public voidpaintclockhands (Graphics g) { the Bayi /** the * The length, color and width of the seconds stitch when set the */ - DoubleSecondlen = Clockradius * 0.85; - DoubleMinutelen = Clockradius * 0.75; the DoubleHourlen = Clockradius * 0.65; the theColor Colorbefore =G.getcolor (); the - the //seconds the G.setcolor (color.red); theG.drawline (Xcenter, Ycenter, (int) (Xcenter + secondlen*math.sin (2*math.pi/60*second)), (int) (Ycenter + Secondlen*math.cos (math.pi+2*math.pi/60*( second) ));94 the //minute the G.setcolor (Color.cyan); theG.drawline (Xcenter, Ycenter, (int) (Xcenter + minutelen*math.sin (2*math.pi/60*minute)), (int) (Ycenter + Minutelen*math.cos (math.pi+2*math.pi/60*( minute) ));98 About //Hour - G.setcolor (color.dark_gray);101G.drawline (Xcenter, Ycenter, (int) (Xcenter + hourlen*math.sin (2*math.pi/12*hour)), (int) (Ycenter + Hourlen*math.cos (math.pi+2*math.pi/12*( Hour) ));102 103 G.setcolor (colorbefore);104 } the /**106 * Get Time107 */108 Public voidInittime () {109Calendar Calendar =NewGregorianCalendar (); theSecond =Calendar.get (calendar.second);111minute =Calendar.get (calendar.minute); thehour = Calendar.get (Calendar.hour_of_day);//24-hour system113 the } the Public Static voidMain (string[] args) { the 117 FinalJFrame frame =NewJFrame ();118Frame.settitle ("Clock");119 frame.setsize (Default_width, default_height); - frame.setlocation (default_x, default_y);121 frame.setdefaultcloseoperation (jframe.exit_on_close);122Frame.setvisible (true);123 124Thread thread =NewThread () { the Public voidrun () {126 while(true){127Testimitateclock Clock =NewTestimitateclock (); - frame.add (clock);129Clock.setvisible (true); theFrame.validate ();//Next, the clock is redrawn every second-that is (the clock component is removed from the frame), so the Validate method is called so that the container is re-assigned its subcomponents131 Try { theThread.Sleep (1000);133}Catch(interruptedexception e) {134 //TODO auto-generated Catch block135 e.printstacktrace ();136 }137Clock.setvisible (false);138Frame.remove (clock);//Delete it in the parent container139Clock.invalidate ();//invalidate the container $ }141 }142 };143 144 Thread.Start ();145 }146 147}
Operating effect :
Adding panel components to Frame.add (panel)//To containers
Frame.validate ()//enables the container to re-arrange its subcomponents and use when modifying subcomponents of this container
Thread thread = new Thread () {...} The use of threads, there is no use of too advanced things
Math.sin ()//function See API
Java-swing Simulation for clock effects