java 圆形时钟

import java.awt.*;
import javax.swing.*;
import java.util.*;

//定义测试类
//所有变量名冲突的变量名串1了,不过就效果不好~~
public class TimerTest
{
//定义主函数
public static void main(String[] args)
{

//定义JFrame类的一个对象,并创建该对象
MyTimer1 f = new MyTimer1();
//调用MyTimer的show()方法
f.show();

//----------------------------------------------------

//调用类的构造函数
MyTimer myTimer=new MyTimer();

//调用类的显示时间函数
myTimer.displayCurrentTime();

//调用类的设置时间函数
myTimer.setCurrentTime();

//调用类的显示时间函数
myTimer.displayCurrentTime();

//调用类的显示时间函数
myTimer.displayCurrentTime();
System.exit(0);
}
}

//定义MyTimer类
class MyTimer1 extends JFrame
{ static int count=0; //判断是否重定义了时间
//构造函数
public MyTimer1()
{
//定义窗口大小
setSize(320, 200);
//定义窗口标题
setTitle("测试自定义时钟类!");

Container c = getContentPane();
// new ClockCanvas("北京时间", "GMT+8")

c.add(new ClockCanvas("北京时间", "GMT+8"));
}
}

//定义接口
interface TimerListener1
{
void timeElapsed(Timer1 t);
}

class Timer1 extends Thread //类Timer1
{
private TimerListener1 target;
private int interval;

public Timer1(int i, TimerListener1 t)
{
target = t;
interval = i;
setDaemon(true);
}

public void run()
{ try
{
while (!interrupted())
{
sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
}
}

class ClockCanvas extends JPanel //clockcanvas
implements TimerListener1
{
static int seconds = 0;
private String city;

private GregorianCalendar calendar;

//构造函数
public ClockCanvas(String c, String tz)
{
city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer1 t = new Timer1(1000, this);
t.start();
setSize(180, 180);
}

//绘制钟面
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(100, 5, 120, 120);
g.drawOval(101, 6, 118, 118);
//分离时间
double hourAngle = 2 * Math.PI
* (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteAngle = 2 * Math.PI
* (seconds - 15 * 60) / (60 * 60);
double secondAngle = 2 * Math.PI
* (seconds - 15) / 60;


g.drawLine(160, 64, 160 + (int)(20* Math.cos(hourAngle)),
64 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 65, 160 + (int)(20* Math.cos(hourAngle)),
65 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 66, 160 + (int)(20* Math.cos(hourAngle)),
66 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 63, 160 + (int)(20* Math.cos(hourAngle)),
63 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 67, 160 + (int)(20* Math.cos(hourAngle)),
67 + (int)(30 * Math.sin(hourAngle)));

g.drawLine(160, 65, 160 + (int)(32* Math.cos(minuteAngle)),
65 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(160, 64, 160 + (int)(32* Math.cos(minuteAngle)),
64 + (int)(40 * Math.sin(minuteAngle)));

g.drawLine(160, 65, 160 + (int)(55* Math.cos(secondAngle)),
65 + (int)(45 * Math.sin(secondAngle)));

g.drawString(city, 130, 150);//*/
}

public void timeElapsed(Timer1 t)
{
calendar.setTime(new Date());
if(MyTimer1.count==1) {int a=1;

seconds=MyTimer.intHour*60*60+MyTimer.intMinute*60+MyTimer.intSecond;
seconds+=a;//a为秒自加


repaint();}
else

{ seconds = calendar.get(Calendar.HOUR) * 60 * 60
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
repaint();}
}
}


//定义时钟类
class MyTimer
implements TimerListener
{
//定义时钟类的属性
static int intHour,intMinute,intSecond;


//构造函数
public MyTimer()
{
setCurrentTimeAsSystemTime();
Timer t = new Timer(1000, this); //实例Timer类,里面有run方法
t.start();
}

//显示当前时间
public void displayCurrentTime()
{
JOptionPane.showMessageDialog(null,intHour+":"+intMinute+":"+intSecond);
}


//设定当前时间
public void setCurrentTime()
{
//从对话框输入时,分秒
String strTemp=JOptionPane.showInputDialog(null,"请输入当前小时(24小时制):");
int iHour=Integer.parseInt(strTemp);

strTemp=JOptionPane.showInputDialog(null,"请输入当前分:");
int iMinute=Integer.parseInt(strTemp);

strTemp=JOptionPane.showInputDialog(null,"请输入当前秒:");
int iSecond=Integer.parseInt(strTemp);

//设定当前时间为对话框输入的时间
if(iHour>=0&&iHour<24)
intHour=iHour;
else intHour=0;
if(iMinute>=0&&iMinute<60)
intMinute=iMinute;
else intMinute=0;
if(iSecond>=0&&iSecond<60)
intSecond=iSecond;
MyTimer1.count++;

// ClockCanvas.seconds=iHour*60*60+iMinute*60+iSecond;

}

//设定当前时间为系统时间,构造函数调用
public void setCurrentTimeAsSystemTime()
{
//定义Date类的一个对象,用来获取系统时间
Date timeCurrent=new Date();

//将系统的时,分秒设定为当前时间
intHour=timeCurrent.getHours();

intMinute=timeCurrent.getMinutes();

intSecond=timeCurrent.getSeconds();
}

//走时
public void timeElapsed(Timer t)
{
//系统走时

intSecond++;
if (intSecond==60)
{
intMinute++;
intSecond=0;
}

if (intMinute==60)
{
intHour++;
intMinute=0;
}

if (intHour==24)
{
intHour=0;
}

}
}

interface TimerListener //接口
{
void timeElapsed(Timer t);
}

class Timer extends Thread //类
{
private TimerListener target;
private int interval;

public Timer(int i, TimerListener t)
{
target = t;
interval = i;
setDaemon(true); //Thread 里面方法 目的跟着老大走
}

public void run()
{ try
{
while (!interrupted())
{
sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
}
}



第二个.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JFrame;

public class Clock extends JFrame implements ActionListener {
int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, ss,
mm, hh, old_m, old_h, ang;

final double RAD = Math.PI / 180; // 度数转换成弧度的比例
// 构造函数创建了一个窗体

public Clock() {

super("Java时钟");
setDefaultCloseOperation(3);
Image image = getToolkit().getImage("clock.gif");
setIconImage(image);

getContentPane().setBackground(Color.black);
setDefaultCloseOperation(3);
setSize(200, 200); // 容器大小,运行时窗口的大小
setLocation(300, 150);
setResizable(false);
setVisible(true);
int delay = 1000;

// 创建一个监听事件
ActionListener drawClock = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
// 创建一个时间计数器,每一秒触发一次
// new Timer(delay, drawClock).start();
}

// 实现ActionListener接口必须实现的方法
public void actionPerformed(ActionEvent e) {
}

// 绘制图形
public void paint(Graphics g) {
super.paint(g); // 新加这句话,背景随时变。。。。。。。。。。。。。。。。。。。。

Graphics2D g2 = (Graphics2D) g;
Insets insets = getInsets();
int L = insets.left / 2, T = insets.top / 2;
h = getSize().height;
g.setColor(Color.white); // 圆圈和字的颜色.......................................
// 画圆
g2.setStroke(new BasicStroke(4.0f));
g.drawOval(L + 40, T + 40, h - 80, h - 80);
r = h / 2 - 40;
x0 = 40 + r - 5 + L;
y0 = 40 + r - 5 - T;
ang = 60;
// 绘制时钟上的12 个字
for (int i = 1; i <= 12; i++) {
x = (int) ((r + 10) * Math.cos(RAD * ang) + x0);
y = (int) ((r + 10) * Math.sin(RAD * ang) + y0);
g.drawString(" " + i, x, h - y);
ang -= 30;
}
// 获得现在的时间
Calendar now = new GregorianCalendar();
int nowh = now.get(Calendar.HOUR_OF_DAY);
int nowm = now.get(Calendar.MINUTE);
int nows = now.get(Calendar.SECOND);
String st;
if (nowh < 10)
st = "0" + nowh;
else
st = " " + nowh;
if (nowm < 10)
st += ":0" + nowm;
else
st += ":" + nowm;
if (nows < 10)
st += ":0" + nows;
else
st += ":" + nows;
// 在窗体上显示时间
g.setColor(Color.magenta);
g.fillRect(L, T, 57, 28);
g.setColor(Color.blue);
g.drawString(st, L + 2, T + 26);
// 计算时间与度数的关系
ss = 90 - nows * 6;
mm = 90 - nowm * 6;
hh = 90 - nowh * 30 - nowm / 2;
x0 = r + 40 + L;
y0 = r + 40 + T;
g2.setStroke(new BasicStroke(1.2f));
// 擦除秒针/擦除是不必要的,后边也是
// if(olds_x>0){
// g.setColor(getBackground());
// g.drawLine(x0,y0,olds_x,h-olds_y);
// }
// else{
old_m = mm;
old_h = hh;
// }
// 绘制秒针
x = (int) (r * 0.9 * Math.cos(RAD * ss)) + x0;
y = (int) (r * 0.9 * Math.sin(RAD * ss)) + y0 - 2 * T;
g.setColor(Color.yellow);
g.drawLine(x0, y0, x, h - y);
olds_x = x;
olds_y = y;
g2.setStroke(new BasicStroke(2.2f));
// 擦除分针
// if(old_m!=mm){
// g.setColor(getBackground());
// g.drawLine(x0,y0,oldm_x,h-oldm_y);
// }
// 绘制分针
x = (int) (r * 0.7 * Math.cos(RAD * mm)) + x0;
y = (int) (r * 0.7 * Math.sin(RAD * mm)) + y0 - 2 * T;
g.setColor(Color.green);
g.drawLine(x0, y0, x, h - y);
oldm_x = x;
oldm_y = y;
old_m = mm;
g2.setStroke(new BasicStroke(3.4f));
// 擦除时针
// if(old_h!=hh){
// g.setColor(getBackground());
// g.drawLine(x0,y0,oldh_x,h-oldh_y);
// }
// 绘制时针
x = (int) (r * 0.5 * Math.cos(RAD * hh)) + x0;
y = (int) (r * 0.5 * Math.sin(RAD * hh)) + y0 - 2 * T;
g.setColor(Color.red);
g.drawLine(x0, y0, x, h - y);
oldh_x = x;
oldh_y = y;
old_h = hh;
}

public static void main(String[] args) {
Clock clock = new Clock();
}


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值