Java implementation of the small clock sample sharing _java

Source: Internet
Author: User
Tags cos sin

Copy Code code as follows:

Package com.clock;

Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Insets;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Java.util.Calendar;
Import Java.util.GregorianCalendar;

Import javax.swing.*;

Import Javax.swing.JFrame;

public class Clock extends JFrame implements actionlistener{
Private final double rad=math.pi/180;

Public Clock () {
Super ("Clock");
SetSize (400, 400);
SetLocation (400, 200);
SetVisible (TRUE);
Setresizable (TRUE);
SetBackground (Color.White);
Addwindowlistener (New Windowadapter () {
@Override
public void windowclosing (WindowEvent e) {
System.exit (0);
}

});
ActionListener drawclock=new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
Repaint ();
}
};
New Timer (1000,drawclock). Start ();

}
@Override
public void actionperformed (ActionEvent e) {}
public void Paint (Graphics g) {
Graphics2D g2= (graphics2d) G;
Insets insert=getinsets ();
int h=getsize (). Height;
G2.setstroke (New Basicstroke (3.0f));
G.drawoval (40,40, h-80,h-80);

int r=200;
int r1=140;
int initrad=60;
for (int i=1;i<=5;i++) {
int x0= (int) (R+r1*math.cos (Rad*initrad));
int y0= (int) (R-r1*math.sin (Rad*initrad));
G.setfont (New Font ("italics", Font.Bold, 20));
g.DrawString ("" +i, x0, y0);
initrad-=30;
}
int r2=150;
int initrad2=-120;
for (int i=7;i<=11;i++) {
int x0= (int) (R+r2*math.cos (rad*initrad2));
int y0= (int) (R-r2*math.sin (rad*initrad2));
G.setfont (New Font ("italics", Font.Bold, 20));
g.DrawString ("" +i, x0, y0);
initrad2-=30;
}
g.DrawString ("6", 190, 350);
g.DrawString ("12", 190, 60);

Calendar now=new GregorianCalendar ();
int Hour=now.get (calendar.hour_of_day);
int Minute=now.get (calendar.minute);
int Second=now.get (Calendar.second);
String time;
if (hour<10) {time= "0" +hour;} Else{time= "" +hour;}

if (minute<10) {time+= ": 0" +minute;} else{time+= ":" +minute;}

if (second<10) {time+= ": 0" +second;} else{time+= ":" +second;}

G.setcolor (Color.White);
G.fillrect (10, 30, 90, 28);
G.setcolor (color.red);
g.DrawString (Time, 10, 50);

int s=90-second*6;
int m=90-minute*6;
int H=90-HOUR*30-MINUTE/2;


G2.setstroke (New Basicstroke (1.0f));

int x= (int) (200+130*math.cos (rad*s));
int y= (int) (200-130*math.sin (rad*s));

int ss=90-(second-1) *6;
int xx= (int) (200+130*math.cos (RAD*SS));
int yy= (int) (200-130*math.sin (RAD*SS));
G.setcolor (Getbackground ());
G.drawline (M, xx, yy);
G.setcolor (Color.Black);
G.drawline (M, x, y);

G2.setstroke (New Basicstroke (2.2f));

int x1= (int) (200+110*math.cos (rad*m));
int y1= (int) (200-110*math.sin (rad*m));

int mm=90-(minute-1) *6;
int x11= (int) (200+110*math.cos (rad*mm));
int y11= (int) (200-110*math.sin (rad*mm));
G.setcolor (Getbackground ());
G.drawline (X11, Y11);
G.setcolor (Color.Blue);
G.drawline (x1, y1);

G2.setstroke (New Basicstroke (4.3f));
int xx1= (int) (200+90*math.cos (rad*h));
int yy1= (int) (200-90*math.sin (rad*h));

int hh=90-(hour-1) *30-minute/2;;
int xxx1= (int) (200+90*math.cos (RAD*HH));
int yyy1= (int) (200-90*math.sin (RAD*HH));
G.setcolor (Color.White);
G.drawline (xxx1, yyy1);
G.setcolor (Color.green);
G.drawline (xx1, yy1);
}
public static void Main (string[] args) {
New Clock ();
}

}

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.