[java]代码库
package day01;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Clock {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ClockFrame extends JFrame{
public ClockFrame(){
setTitle("java clock");
setBounds(100, 200, 600, 800);
setVisible(true);
ClockPanel cp = new ClockPanel();
add(cp);
}
}
class ClockPanel extends JPanel{
int h;
int m;
int s;
int sx;
int sy;
int mx;
int my;
int hx;
int hy;
public ClockPanel() {
move();
for(int i=0;i<60;i++){
int r = 100;
if(i%5==0){
if(i%15==0){
r=120;
}else{
r=140;
}
}
int x1 = (int)(300+Math.sin(Math.toRadians(6*i))*r);
int y1 = (int)(300-Math.cos(Math.toRadians(i*6))*r);
int x2 = (int)(300+Math.sin(Math.toRadians(6*i))*180);
int y2 = (int)(300-Math.cos(Math.toRadians(i*6))*180);
}
}
public void paint(Graphics g ) {
super.paint(g);
setBackground(Color.LIGHT_GRAY);
Graphics2D g2 = (Graphics2D) g;
g2.drawString(h + ":" + m + ":" + s, 20, 50);
g2.setStroke(new BasicStroke(5));
g.setColor(Color.yellow);
g.drawLine(300,300,sx,sy);
g.setColor(Color.red);
g.drawLine(300,300,mx,my);
g.setColor(Color.gray);
g.drawLine(300,300,hx,hy);
g.setColor(Color.orange);
g.drawOval(120,120,360,360);
g.drawOval(205,205,190,190);
g.setColor(Color.black);
g.fillOval(292, 292, 15, 15);
for(int i=0;i<60;i++){
int r = 100;
if(i%5==0){
if(i%15==0){
r=120;
}else{
r=140;
}
}
int x1 = (int)(300+Math.sin(Math.toRadians(6*i))*r);
int y1 = (int)(300-Math.cos(Math.toRadians(i*6))*r);
int x2 = (int)(300+Math.sin(Math.toRadians(6*i))*180);
int y2 = (int)(300-Math.cos(Math.toRadians(i*6))*180);
g.setColor(Color.green);
g.drawLine(x1,y1,x2,y2);
}
}
public void move() {
new Thread() {
public void run() {
while (true) {
Date d = new Date();
System.out.println(d);
SimpleDateFormat ss = new SimpleDateFormat("ss");
SimpleDateFormat mm = new SimpleDateFormat("mm");
SimpleDateFormat hh = new SimpleDateFormat("HH");
s = Integer.parseInt(ss.format(d));
m = Integer.parseInt(mm.format(d));
h = Integer.parseInt(hh.format(d));
sx = (int) (300 + Math.sin(Math.toRadians(6 * s)) * 150);
sy = (int) (300 - Math.cos(Math.toRadians(6 * s)) * 150);
mx = (int) (300 + Math.sin(Math.toRadians(6 * m)) * 120);
my = (int) (300 - Math.cos(Math.toRadians(6 * m)) * 120);
hx = (int) (300 + Math.sin(Math.toRadians(h * 30 + m * 0.5)) * 90);
hy = (int) (300 - Math.cos(Math.toRadians(h * 30 + m * 0.5)) * 90);
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
初级程序员
by: 虾仔 发表于:2018-05-11 14:37:50 顶(0) | 踩(0) 回复
6666 唯一美中不足的就是分针得在秒针跑一圈才跳
回复评论