1. Automatically read the system time into a wall clock, with the system time to walk the second
Set the font JFrame join various components to initialize and adjust the component 2D Brush double buffer listener The Timer is about these exercises ...
Package Com.lovo.homework3;import Java.awt.basicstroke;import Java.awt.color;import java.awt.font;import Java.awt.graphics;import Java.awt.graphics2d;import Java.awt.image;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.image.bufferedimage;import Java.text.decimalformat;import Java.util.calendar;import Javax.swing.jframe;import javax.swing.jlabel;import javax.swing.Timer;/** * class: Clock ~ Automatic Read system time and walk the second * @author Abe * * */@SuppressWarnings ("Serial") public class Clock extends JFrame implements ActionListener {private int[ ] Timenum = new Int[3];p rivate double[] radian = new Double[3];p rivate Timer timer;private String str = new string ();p Riva Te JLabel lbl;private color[] Color = {color.red, color.blue, color.black};p rivate Image offimage = new BufferedImage (40 0, 500,BUFFEREDIMAGE.TYPE_INT_RGB);//double-buffered private jlabel[] Lbls = new Jlabel[4];p ublic clock () {This.settitle ("clock"); This.setsize (+), this.setdefaultcloseoperation (exit_on_close); this.setresizable(false); This.setlocationrelativeto (null); This.setlayout (null); timer = new Timer (n, this); lbl = new JLabel (str); Displays the digital time Lbl.setfont (new Font ("Consolas", Font.plain, Panax Notoginseng)), Lbl.setbounds ((), This.add (LBL); for (int i = 0; i < lbls.length; i++) {//Display dial number lbls[i] = new JLabel (0 + 3 * i + ""); Lbls[i].setfont (New Font ("Consolas", Font.plain)); This.add (Lbls [i]);} Lbls[0].setbounds (Lbls[1].setbounds, 310, and ()); Lbls[2].setbounds (285, N.); Lbls[3]. ". SetBounds (70, 170, 50, 50);} @Overridepublic void Paint (Graphics g) {Graphics NEWG = Offimage.getgraphics (); Super.paint (NEWG); This.draw (NEWG); G.drawimage (offimage, 0, 0, +, +, null); Timer.start ();} public void Draw (Graphics NEWG) {newg.setcolor (color.black); Newg.drawoval (50, 70, 300, 300);//Outer circle graphics2d g2d = (graphi CS2D) newg;for (int i = 0; i < timenum.length; i++) {//Pointer G2d.setstroke (new Basicstroke (1 + i * 2)); G2d.setcolor (color [i]); G2d.drawline (220,200 + (int) (Math.sin (Radian[i)) * (120-20 * i), +-(int) (Math.Cos (Radian[i]) * (120-20 * i))); for (int i = 0; i <; i++) {//dial double Radmark = 2 * Math.PI * i/60;int j;if (i% 5! = 0) {G2d.setstroke (new Bas Icstroke (1)); j = 145;} else {G2d.setstroke (new Basicstroke (5)); j = 135;} G2d.drawline (+ + (int) (Math.sin (Radmark) * j), +-(int) (Math.Cos (Radmark) * j), + (int) (Math.sin (radmark) * 145) , up to (int) (Math.Cos (radmark) * 145));}} @Overridepublic void actionperformed (ActionEvent e) {if (e.getsource () = = timer) {Calendar cal = Calendar.getinstance (); /read system time This.timenum[0] = Cal.get (Calendar.second); this.timenum[1] = Cal.get (Calendar.minute); this.timenum[2] = Cal.get (calendar.hour_of_day);D ecimalformat df = new DecimalFormat ("XX"), str = Df.format (timenum[2]) + ":" + Df.format ( TIMENUM[1]) + ":" + Df.format (timenum[0]); Lbl.settext (str); for (int i = 0; i < radian.length; i++) {//time converted to radians if (I < 2) {Radian[i] = 2 * Math.PI * timenum[i]/MAX;} else {//hour to convert radians, plus minutes provided by radians radian[i] = 2 * Math. Pi* (Timenum[i] + timenum[i-1]/60.0)/12;}} Repaint ();}} public static void Main (string[] args) {new Clock (). setvisible (True);}}
Java Programming (15.3)-----Graphical user interface GUI making wall clocks