(Original by Xu jiansheng) Java Swing uses simple multithreading to achieve dynamic clock, swing Multithreading
Note: This article is only for learning and communication.
The above is, the code below
The first class is Circle.
package org.xt.util;import java.awt.Point;public class Circle { private Point centre; private int radius; public Circle(Point centre, int radius) { this.centre = centre; this.radius = radius; } public Point getCentre() { return centre; } public void setCentre(Point centre) { this.centre = centre; } public int getRadius() { return this.radius; } public void setRadius(int radius) { this.radius = radius; }}
The second class is Clock.
Package org. xt. clock; import java. awt. basicStroke; import java. awt. borderLayout; import java. awt. color; import java. awt. graphics; import java. awt. graphics2D; import java. awt. renderingHints; import java. util. calendar; import javax. swing. JComponent; import javax. swing. JFrame; import javax. swing. JPanel; @ SuppressWarnings ("serial") public class Clock extends JComponent implements Runnable {private int radius; pub Lic Clock (int radius) {this. radius = radius;} public void paint (Graphics g) {Graphics2D g2d = (Graphics2D) g; // these two statements are the key to removing line aliasing, as for the principle, I am not clear about it. These two sentences are obtained by the author from the Internet (these two sentences are not self-written, haha) g2d. setStroke (new BasicStroke (1.0f, BasicStroke. CAP_BUTT, BasicStroke. JOIN_BEVEL); g2d. setRenderingHint (RenderingHints. KEY_ANTIALIASING, RenderingHints. VALUE_ANTIALIAS_ON); // The coordinate int clockPlateX = 0 in the upper left corner of the rectangle where the clock disk is located; int clockPlateY = 0; g2d. setColor (Color. GRAY); // draw two concentric discs as the frame g2d of the clock. drawOval (clockPlateX, clockPlateY, this. radius * 2, this. radius * 2); g2d. drawOval (clockPlateX + 5, clockPlateY + 5, this. radius * 2-10, this. radius * 2-10); // The Center Coordinate int centreX = this. radius; int centreY = centreX; g2d. setStroke (new BasicStroke (0.8f, BasicStroke. CAP_BUTT, BasicStroke. JOIN_BEVEL); // This is the g2d point in the center of the clock disk. fillOval (centreX-3, centreY -3, 6, 6); g2d. setStroke (new BasicStroke (2.0f, BasicStroke. CAP_BUTT, BasicStroke. JOIN_BEVEL); // start to draw a large scale. There are 12 in total, indicating 12 hours. The benchmark is the scale of 9, double degree = 0; double radian = Math. toRadians (degree); for (int I = 0; I <12; ++ I) {// calculate the point on the left of the curve, int leftX = (int) (double) centreX-(this. radius-15) * Math. cos (radian); int leftY = (int) (double) centreY-(this. radius-15) * Math. sin (radian ));// Calculate the right vertex of the dial line, int rightX = (int) (double) centreX-(this. radius-25) * Math. cos (radian); int rightY = (int) (double) centreY-(this. radius-25) * Math. sin (radian); g2d. drawLine (leftX, leftY, rightX, rightY); degree + = 30; // converts the angle to radian = Math. toRadians (degree);} g2d. setStroke (new BasicStroke (1.5f, BasicStroke. CAP_BUTT, BasicStroke. JOIN_BEVEL); // draw a small scale starting from the following, with a total of 60-12 = 48. The scale indicated by the benchmark is degr. Ee = 0; radian = Math. toRadians (degree); for (int I = 0; I <60; ++ I) {// This condition is to avoid duplicate if (I! = 0 & degree % 30! = 0) {int leftX = (int) (double) centreX-(this. radius-15) * Math. cos (radian); int leftY = (int) (double) centreY-(this. radius-15) * Math. sin (radian); int rightX = (int) (double) centreX-(this. radius-17) * Math. cos (radian); int rightY = (int) (double) centreY-(this. radius-17) * Math. sin (radian); g2d. drawLine (leftX, leftY, rightX, rightY);} // increase the degree of degree + = 360/60 each time; // convert the angle to radian = Math. toRadians (degree);} // obtain the current time, minute, second (Date class has been eliminated, use Calendar ar here) Calendar cal = Calendar. getInstance (); int second = cal. get (Calendar. SECOND); int minute = cal. get (Calendar. MINUTE); int hour = cal. get (Calendar. HOUR_OF_DAY); if (hour> 12) {hour = hour % 12;} // calculates the radian from the start position of the second needle, the remaining two sentences are the split needle and the double secondProp = Math. toRadians (second * (360/60); double minuteProp = Math. toRadians (minute * (360/60); double hourProp = Math. toRadians (hour + minuteProp/(2 * Math. PI) * (360/12); int leftX = (int) (centreX + (this. radius-25) * Math. sin (secondProp); int leftY = (int) (centreY-(this. radius-25) * Math. cos (secondProp); g2d. setStroke (new BasicStroke (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 (new BasicStroke (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 (new BasicStroke (2.5f, BasicStroke. CAP_BUTT, BasicStroke. JOIN_BEVEL); g. drawLine (leftX, leftY, centreX, centreY);} public void run () {while (true) {try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();} this. repaint () ;}} public static void main (String [] args) {JFrame frame = new JFrame (); JPanel = new JPanel (new BorderLayout (); frame. setContentPane (panel); Clock clock = new Clock (150); new Thread (clock ). start (); panel. add (clock, BorderLayout. CENTER); frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); frame. setSize (600,400); frame. setVisible (true );}}
Haha, the level is limited, and the code writing is messy
-- Written on 20:30:00