(original by Xu Jianseng) "Java Swing uses simple multithreading to realize dynamic clocks"

Source: Internet
Author: User
Tags cos sin

Note: This article is for learning and communication only

The above is the code below

The first class is circle

 PackageOrg.xt.util;ImportJava.awt.Point; Public classCircle {PrivatePoint Centre; Private intradius;  PublicCircle (Point Centre,intradius) {         This. Centre =Centre;  This. Radius =radius; }     PublicPoint Getcentre () {returnCentre; }     Public voidsetcentre (Point centre) { This. Centre =Centre; }     Public intGetradius () {return  This. Radius; }     Public voidSetradius (intradius) {         This. Radius =radius; }}

The second class is clock

 PackageOrg.xt.clock;ImportJava.awt.BasicStroke;Importjava.awt.BorderLayout;ImportJava.awt.Color;ImportJava.awt.Graphics;ImportJava.awt.Graphics2D;Importjava.awt.RenderingHints;ImportJava.util.Calendar;Importjavax.swing.JComponent;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel; @SuppressWarnings ("Serial") Public classClockextendsJComponentImplementsRunnable {Private intradius;  PublicClock (intradius) {         This. Radius =radius; }     Public voidPaint (Graphics g) {graphics2d g2d=(graphics2d) G; //These two words are the key to remove the line sawtooth, as to the principle of the author is not clear, the two sentences are the author from the network to find (the two sentences are not written by themselves, hehe)G2d.setstroke (NewBasicstroke (1.0f, Basicstroke.cap_butt, Basicstroke.join_bevel));     G2d.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on); //The coordinates of the upper- left corner of the rectangle where the dial is located        intClockplatex = 0; intClockplatey = 0;     G2d.setcolor (Color.gray); //draw two concentric discs as a border of the clockG2d.drawoval (Clockplatex, Clockplatey, This. Radius * 2, This. RADIUS * 2); G2d.drawoval (Clockplatex+ 5, Clockplatey + 5, This. Radius * 2-10, This. Radius * 2-10); //Center coordinates        intCentreX = This. Radius; intCentrey =CentreX; G2d.setstroke (NewBasicstroke (0.8f, Basicstroke.cap_butt, Basicstroke.join_bevel)); //That 's the point in the center of the clock plate.G2d.filloval (CentreX-3, centreY-3, 6, 6); G2d.setstroke (NewBasicstroke (2.0f, Basicstroke.cap_butt, Basicstroke.join_bevel)); //The following is the beginning of the large scale, a total of 12, indicating that there is 12 hours, the benchmark is the scale of 9 o'clock        Doubledegree = 0; DoubleRadian =Math.toradians (degree);  for(inti = 0; I < 12; ++i) {//calculates the point at the left of the tick mark relative to the position            intLEFTX = (int) ((Double) CentreX-( This. radius-15) *Math.Cos (radian)); intLefty = (int) ((Double) Centrey-( This. radius-15) *Math.sin (radian)); //calculates the point at the right of the tick mark relative to the position            intRIGHTX = (int) ((Double) CentreX-( This. radius-25) *Math.Cos (radian)); intrighty = (int) ((Double) Centrey-( This. radius-25) *Math.sin (radian));            G2d.drawline (LEFTX, Lefty, RIGHTX, righty); Degree+ = 30; //convert an angle to radiansRadian =Math.toradians (degree); } g2d.setstroke (NewBasicstroke (1.5f, Basicstroke.cap_butt, Basicstroke.join_bevel)); //The following starts the small scale, a total of 60-12 = 48, the datum is the scale of 0 o'clockdegree = 0; Radian=Math.toradians (degree);  for(inti = 0; I < 60; ++i) {//This condition is to avoid small scale and large scale repetition            if(I! = 0 && degree% 30! = 0) {                intLEFTX = (int) ((Double) CentreX-( This. radius-15) *Math.Cos (radian)); intLefty = (int) ((Double) Centrey-( This. radius-15) *Math.sin (radian)); intRIGHTX = (int) ((Double) CentreX-( This. radius-17) *Math.Cos (radian)); intrighty = (int) ((Double) Centrey-( This. radius-17) *Math.sin (radian));            G2d.drawline (LEFTX, Lefty, RIGHTX, righty); }//6 degrees per increase .degree + = 360/60; //convert an angle to radiansRadian =Math.toradians (degree); }//gets the current time, minutes, seconds (date class has been deprecated, use calendar here)Calendar cal =calendar.getinstance (); intSecond =Cal.get (Calendar.second); intminute =Cal.get (Calendar.minute); inthour =Cal.get (Calendar.hour_of_day); if(Hour > 12) {Hour= Hour% 12; }//calculates the arc of the second hand at the beginning of the current distance, and the remaining two sentences are the minute hand and hour hand        DoubleSecondprop = Math.toradians (Second * (360/60)); DoubleMinuteprop = Math.toradians (Minute * (360/60)); DoubleHourprop = Math.toradians ((hour + Minuteprop/(2 * Math.PI)) * (360/12)); intLEFTX = (int) (CentreX + ( This. radius-25) *Math.sin (Secondprop)); intLefty = (int) (Centrey-( This. radius-25) *Math.Cos (Secondprop)); G2d.setstroke (NewBasicstroke (1.0f, Basicstroke.cap_butt, Basicstroke.join_bevel));        G.drawline (LEFTX, Lefty, CentreX, Centrey); LEFTX= (int) (CentreX + ( This. radius-45) *Math.sin (Minuteprop)); Lefty= (int) (Centrey-( This. radius-45) *Math.Cos (Minuteprop)); G2d.setstroke (NewBasicstroke (1.5f, Basicstroke.cap_butt, Basicstroke.join_bevel));        G.drawline (LEFTX, Lefty, CentreX, Centrey); LEFTX= (int) (CentreX + ( This. radius-70) *Math.sin (Hourprop)); Lefty= (int) (Centrey-( This. radius-70) *Math.Cos (Hourprop)); G2d.setstroke (NewBasicstroke (2.5f, Basicstroke.cap_butt, Basicstroke.join_bevel));    G.drawline (LEFTX, Lefty, CentreX, Centrey); }     Public voidrun () { while(true) {            Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }             This. repaint (); }    }     Public Static voidMain (string[] args) {JFrame frame=NewJFrame (); JPanel Panel=NewJPanel (NewBorderLayout ());        Frame.setcontentpane (panel); Clock Clock=NewClock (150); NewThread (Clock). Start ();        Panel.add (clock, borderlayout.center);        Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.setsize (600, 400); Frame.setvisible (true); }}

Hehe, the level is limited, the code writes the comparison disorderly

--Written on 2014-09-08 20:30:00 up and down

(original by Xu Jianseng) "Java Swing uses simple multithreading to realize dynamic clocks"

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.