Java Implementation Clock

Source: Internet
Author: User
Tags cos dateformat sin

Package Com.js.ai.modules.pointwall.testxfz;import Java.awt.color;import Java.awt.dimension;import Java.awt.Font; Import Java.awt.graphics;import java.awt.graphics2d;import Java.text.dateformat;import Java.util.Calendar;import Java.util.date;import Java.util.gregoriancalendar;import Javax.swing.jframe;public class ClockDemo extends JFrame Implements runnable{thread clock;final int xpoint=180;final int ypoint=180;final int R=80;int xhour=0,yhour=0,xsecond=0 , Ysecond=0,xmin=0,ymin=0;public Clockdemo () {super ("Digital Clock"), SetFont (New Font ("Arial", Font.Bold,)); start (); SetSize ( SetVisible (True); Setdefaultcloseoperation (jframe.exit_on_close);} public void Start ()//START process {if (clock==null)//If process is null value {clock=new Thread (this);//Instantiate process Clock.start ();//START process}}public void Run ()//runs the process {while (Clock!=null) {repaint ();//calls the Paint method to redraw the interface try {thread.sleep (1000);//thread pauses for one second (1000 milliseconds)} catch ( Interruptedexception e) {e.printstacktrace ();}}} public void Stop ()//stop process {clock=null;} public void Paint (Graphics g)//paint method for overloaded components{graphics2d g2= (graphics2d) g;//get Graphics2D object DateFormat dateformat=dateformat.getdateinstance (DateFormat.FULL); Calendar now =new GregorianCalendar ();//Instantiate Calendar Object Now.settime (new Date ());//dateformat.format (Now.gettime ()) String Timeinfo= "";//output information int hour=now.get (calendar.hour_of_day);//Get hours int minute=now.get (calendar.minute);//Get fractional int Second=now.get (Calendar.second);//Gets the number of seconds if (hour<=9) timeinfo+= "0" +hour+ ":";//Format Output elsetimeinfo+=hour+ ":"; if ( minute<=9) timeinfo+= "0" +minute+ ":"; elsetimeinfo+=minute+ ":"; if (second<=9) timeinfo+= "0" +second+ ":"; Elsetimeinfo+=second+ ":"; G.setcolor (Color.yellow);//set current color to yellow dimension dim=getsize ();//Get window size g.fillrect (0, 0, Dim.width, dim.height);//Fill Background g.setcolor (color.red);//Set Current color g.drawstring (Timeinfo, 130, 340);//Display time string G.setcolor ( Color.green), g.DrawString (Dateformat.format (Now.gettime ()), 20,60), G.setcolor (Color.Black); G.setFont (New Font (" San_serif ", Font.Bold,)); for (int i=0,num=12;i<360;i+=6) {double Alfa=math.toradians (i); int xpox=xpoint+ (int) (R *math.sIn (ALFA)), int ypos=ypoint-(int) (R*math.cos (Alfa)), if (i==0) {if (num%3==0) G.setcolor (color.red);//Number 3, 6,9,12 for Red Elseg.setcolor (Color.Black); The remaining numbers are black g.drawstring ("" +num,xpox-5,ypos+3); Write digital num= (num+1);} else {g.setcolor (color.black); g.DrawString (".", Xpox,ypos);}} G.setcolor (Color.Black); G.filloval (xpoint-4,ypoint-4,8,8);//Draw the second hand xsecond= (int) (xpoint+ (R-10) *math.sin (second* MATH.PI/60)); ysecond= (int) (ypoint-(R-10) *math.cos (second* (2*MATH.PI/60))); G.setcolor (color.red); G.drawline ( Xpoint,ypoint,xsecond,ysecond);//Draw minute hand xmin= (int) (xpoint+ (R-20) *math.sin ((MINUTE+SECOND/60) * (2*MATH.PI/60)); yMin = (int) (ypoint-(R-20) *math.cos ((MINUTE+SECOND/60) * (2*MATH.PI/60)); G.setcolor (color.red); G.drawline (Xpoint, Ypoint,xmin,ymin);//Draw the hour xhour= (int) (xpoint+ (R-30) *math.sin ((HOUR+MINUTE/60+SECOND/60/60) * (2*MATH.PI/12))); YHour = (int) (ypoint-(R-30) *math.cos ((HOUR+MINUTE/60+SECOND/60/60) * (2*MATH.PI/12)); G.setcolor (color.red); G.drawline ( Xpoint,ypoint,xhour,yhour);} public static void Main (string[] args) {New Clockdemo ();}} 

  

Java Implementation Clock

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.