This article describes the use of JFrame to complete the dynamic simulation clock, draw the clock in the panel and extract the current moment of the system, the main method to suspend the thread 1 seconds, refresh the panel.
The implementation code is as follows
Import javax.swing.*;
Import java.awt.*;
Import java.util.*;
Import Java.lang.Thread;
Import Java.text.DecimalFormat;
public class Stillclock extends JPanel {/** * @param args * * private int hour;
private int minute;
private int second;
Constructor public Stillclock () {setcurrenttime ();
//returns the hour public int gethour () {return hour;
public int Getminute () {return minute;
public int Getsecond () {return second;
///Draw clock protected void paintcomponent (Graphics g) {super.paintcomponent (g);
Initialize int clockradius = (int) (Math.min (GetWidth (), getheight ()) * 0.8 * 0.5);
int xcenter = GetWidth ()/2;
int ycenter = GetHeight ()/2;
Draw round G.setcolor (Color.Black);
G.drawoval (Xcenter-clockradius, Ycenter-clockradius, 2 * clockradius, 2 * clockradius);
g.DrawString ("A", xCenter-5, Ycenter-clockradius + 15); g.DrawString ("9", Xcenter-clockradius + 3, YCEnter + 5);
g.DrawString ("3", Xcenter + clockRadius-10, Ycenter + 3);
g.DrawString ("6", xCenter-3, Ycenter + clockRadius-3);
Draw second hand int slength = (int) (Clockradius * 0.8);
int xsecond = (int) (Xcenter + slength * Math.sin (Second * (2 * math.pi/60)));
int ysecond = (int) (Ycenter-slength * Math.Cos (Second * (2 * math.pi/60)));
G.setcolor (color.red);
G.drawline (Xcenter, Ycenter, Xsecond, Ysecond);
Draw the minute hand int mlenth = (int) (Clockradius * 0.65);
int xminute = (int) (Xcenter + mlenth * Math.sin (Minute * (2 * math.pi/60)));
int yminute = (int) (Xcenter-mlenth * Math.Cos (Minute * (2 * math.pi/60)));
G.setcolor (Color.Blue);
G.drawline (Xcenter, Ycenter, Xminute, Yminute);
Draw clockwise int hlength = (int) (Clockradius * 0.5);
int xhour = (int) (Xcenter + hlength * Math.sin (hour% + minute/60.0) * (2 * MATH.PI/12))); int yhour = (int) (Ycenter-hlength * MATH.COS (hour% + minute/60.0) * (2 * MATH.PI/12)));
G.setcolor (Color.green);
G.drawline (Xcenter, Ycenter, Xhour, Yhour);
Draw Digital Clock g.setcolor (Color.Black);
DecimalFormat s=new DecimalFormat ("00"); g.DrawString (S.format (Gethour ()) + ":" + S.format (Getminute ()) + ":" + S.format (Getsecond ()), XCenter-22, Ycenter-cloc
KRADIUS-15);
public void Setcurrenttime () {Calendar calendar = new GregorianCalendar ();
This.hour = Calendar.get (Calendar.hour_of_day);
This.minute = Calendar.get (Calendar.minute);
This.second = Calendar.get (Calendar.second); The public static void main (string[] args) {//TODO auto-generated the method stub JFrame frame = new JFrame (
"Diaplayclock");
Frame.setresizable (FALSE);
Frame.settitle ("Diaplayclock");
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.setsize (300,350);
Frame.setvisible (TRUE);
while (true) {Stillclock clock = new Stillclock (); Frame.getcontentpane (). Add (clock);
Clock.setvisible (TRUE);
Frame.validate ();
try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace ();
} clock.setvisible (FALSE);
Frame.remove (clock);
Clock = null;
Frame.validate ();
}
}
}
Effect Chart:
The above is the entire content of this article, I hope you can enjoy.