Recently I have been writing the teacher's clock date tool, but because some of the features have not yet been implemented, so it is not possible to post the full code, but the following will be updated on the spreadsheet and calendar applet. Although this clock is not particularly good-looking, but the basic features have been implemented. Readers can try to make it look beautiful.
The code for the simple clock is as follows:
1 Importjava.awt.*;2 ImportJava.awt.geom.AffineTransform;3 ImportJava.util.Calendar;4 ImportJavax.swing.JFrame;5 ImportJavax.swing.JPanel;6 //@Author: M-y7 //@Time: 2016/12/18 Public classTimeextendsjframe{9 PublicTime () {TenJPanel panel=NewJPanel (); OnePanel.setlayout (NewGridLayout ()); AClock1 c=NewClock1 (); - Panel.add (c); - This. Add (panel); the } - Public Static voidMain (string[] args) { -Time t=NewTime (); -T.setsize (300,300);//Window Size +T.settitle ("Clock");//window name -T.setvisible (true);//set to visible + t.setdefaultcloseoperation (jframe.exit_on_close); A } at } - classClock1extendsjpanel{ - Thread th; - Public voidrun () { - while(true){ - Try{ in repaint (); -}Catch(Exception e) {} to } + } - Public voidPaint (Graphics g) { theGraphics2D g2d=(graphics2d) G; * //Remove Jagged $ G2d.setrenderinghint (renderinghints.key_antialiasing,renderinghints.value_antialias_on); Panax Notoginseng //clear the original graphic - G2d.setcolor (Color.White); theG2d.fillrect (0,0, This. GetWidth (), This. GetHeight ()); + G2d.setcolor (color.black); A //Center (x, y) Radius radius=100 the intx0=150; + inty0=150; - intradius=100; $G2d.drawoval (50,50,200,200); $G2d.drawoval (48,48,204,204); - //Draw a scale - for(inti=0;i<60;i++){ theG2d.fillrect (x0-2,46,3,3); -G2d.rotate (Math.toradians (6), x0,y0); Wuyi } the //Draw 12 Numbers - for(inti=0;i<12;i++){ Wu Doubled=math.pi/180*i* (360/12);//360/12 degrees per turn - intx= (x0-4) + (int) ((radius-12) *Math.Cos (d)); About intY= (y0+4) + (int) ((radius-12) *Math.sin (d)); $ //because it is drawn from the clockwise clock 3 o'clock, the index I need to add 3 - intJ=i+3; - if(j>12) -J=j-12; A g2d.drawstring (Integer.tostring (j), X, y); + } the //Set rotation Reset -AffineTransform old=g2d.gettransform (); $ g2d.settransform (old); theCalendar c=calendar.getinstance (); the intH=c.get (Calendar.hour_of_day);//when you get hour---- the intM=c.get (Calendar.minute);//Get minute---points the intS=c.get (Calendar.second);//get second---seconds - //Draw the hour hand in DoubleHangle= (h-12+m/60d) *360d/12d; the g2d.rotate (Math.toradians (hangle), x0,y0); theG.drawline (x0,y0,x0,100); About //draw the minute hand the DoubleMangle= (m+s/60d) *360d/60d; the g2d.rotate (Math.toradians (mAngle), x0,y0); theG.drawline (x0,y0,x0,70); + //Draw the second hand - DoubleSangle=s*360d/60d; the g2d.rotate (Math.toradians (sangle), x0,y0); BayiG.drawline (x0,y0,x0,55); the repaint (); the } -}
The operating interface is as follows:
Draw a simple clock in Java