用Java编写小时钟

用Java编写小时钟要求:1)在HTML网页上显示2)可以显示模拟与数字两种形式的时钟,通过点击按钮进行切换... 用Java编写小时钟
要求:1)在HTML网页上显示
2)可以显示模拟与数字两种形式的时钟,通过点击按钮进行切换
展开
 我来答
咖悟怀8558
2009-09-26 · TA获得超过277个赞
知道小有建树答主
回答量:299
采纳率:0%
帮助的人:189万
展开全部
貌似这个网上有JS插件的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
来味嫣闪1e
推荐于2016-01-29 · TA获得超过320个赞
知道小有建树答主
回答量:231
采纳率:0%
帮助的人:84.4万
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* This is the main Function of the program.
*/
public class Clock{
public static void main(String []args){
ClockFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* This class is used to define the main frame of the clock
*/
class ClockFrame extends JFrame{
//constructor function
public ClockFrame(){
setTitle("小时钟");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(DEFAULT_LOC_WIDTH, DEFAULT_LOC_HEIGHT);
ClockPanel panel = new ClockPanel();
add(panel);
}
//variables of the frame
private int DEFAULT_LOC_WIDTH = 300;
private int DEFAULT_LOC_HEIGHT = 300;
private int DEFAULT_WIDTH = 330;
private int DEFAULT_HEIGHT = 330;
}
/**
* This class is used to defind the main panel of the clock
*/
class ClockPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//get the time of the system
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
//draw the clock face
Ellipse2D clockFace = new Ellipse2D.Double();
clockFace.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+RADIUS, CENTER_Y+RADIUS);
g2.setColor(Color.BLUE);
g2.draw(clockFace);
//draw the clock center
Ellipse2D clockCenter = new Ellipse2D.Double();
clockCenter.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+INNER_RADIUS, CENTER_Y+INNER_RADIUS);
g2.setColor(Color.RED);
g2.fill(clockCenter);
//help to get the exact position of the lines
double lenX, lenY, posX, posY;
//draw the clock second line
Line2D clockSecond = new Line2D.Double();
double secondTime = (double) calendar.get(Calendar.SECOND);
lenX = SECOND_LEN*Math.sin(2*Math.PI*secondTime/60.0);
lenY = SECOND_LEN*Math.cos(2*Math.PI*secondTime/60.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockSecond.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.PINK);
g2.draw(clockSecond);
//draw the clock minute line
Line2D clockMinute = new Line2D.Double();
double minuteTime = (double) calendar.get(Calendar.MINUTE);
lenX = MINUTE_LEN*Math.sin(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
lenY = MINUTE_LEN*Math.cos(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockMinute.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.GREEN);
g2.draw(clockMinute);
//draw the clock hour line
Line2D clockHour = new Line2D.Double();
double hourTime = (double) calendar.get(Calendar.HOUR);
lenX = HOUR_LEN*Math.sin(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
lenY = HOUR_LEN*Math.cos(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockHour.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.BLUE);
g2.draw(clockHour);
int delay = 1000;
// actionListener
ActionListener drawClock;
drawClock=new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//create timer
new Timer(delay, drawClock).start();
}
//variables of the panel
private int HOUR_LEN = 50;
private int MINUTE_LEN = 70;
private int SECOND_LEN = 90;
private int RADIUS = 100;
private int INNER_RADIUS = 2;
private int CENTER_X = 150;
private int CENTER_Y = 150;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式