java时钟代码解释 java实现时钟

JAVA画时钟代码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sun.util.calendar.Gregorian;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ClockPointer extends JFrame{
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 ClockPointer(){
super("Java时钟");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image image = getToolkit().getImage("clock.gif");
setIconImage(image);
setSize(400,400);
setBackground(Color.white);
//setLocation(300,150);
this.setLocationRelativeTo(null);
setResizable(true);
int delay = 1000;
//创建一个监听事件
setVisible(true);
ActionListener drawClock = new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//创建一个时间计数器java时钟代码解释,每一秒触发一次
new Timer(delay, drawClock).start();
}
java.text.SimpleDateFormat fmTime = new java.text.SimpleDateFormat("HH:mm:ss");
//绘制图形
public void paint(Graphics g){
super.paint(g);
g.setFont(null);
Graphics2D g2D = (Graphics2D)g;
Insets insets = getInsets();
int L = insets.left/2, T = insets.top/2;
h = getSize().height;
g.setColor(Color.white);
//画圆
g2D.setStroke(new BasicStroke(2.0f));
g.setColor(Color.gray);
g.drawOval(L 40, T 40, h-80, h-80);
r = h/2 - 40;
x0 = 40r - 5L;
y0 = 40r - 5 - T;
ang = 60;
//绘制时钟上java时钟代码解释的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.setColor(Color.black);
g.drawString("" i, x, h-y);
ang -=30;
}
//获得现在java时钟代码解释的时间
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=fmTime.format(now.getTime());
//在窗体上显示时间
g.setColor(Color.pink);
g.fillRect(L, T, 50, 28);
g.setColor(Color.blue);
g.drawString(st,L 2,T 26);
//计算时间与度数java时钟代码解释的关系
ss = 90 - nows*6;
mm = 90 - nowm*6;
hh = 90 - nowh*30 - nowm/2;
x0 = r 40 L;
y0 = r 40 T;
g2D.setStroke(new BasicStroke(1.2f));
//擦除秒针
//if(olds_x0){
//g.setColor(getBackground());
//// g.setColor(Color.gray);
//g.drawLine(x0, y0, olds_x, h-olds_y); // (?)
//}
//绘制秒针
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;
g2D.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;
g2D.setStroke(new BasicStroke(3.2f));
//擦除时针
//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){
new ClockPointer();
}
}
//整理一下
java 小时钟,怎么理解用数学函数求指针端点坐标角度deg转弧度rad的方法如下
rad = deg/ 180.0 * 3.1415926;
java里封装好的方法是
rad=Math.toRadians(deg);
已知 圆心是 x,y ,半径是r ,弧度是rad 根据弧度计算圆周上点的坐标的方法
如果是正东方向,作为0度 起点,计算公式如下
double x1 = xcos(rad)*r;
double y1 = ysin(rad)*r;
如果是正北方向,作为0度的起点, 计算公式如下
[*说明,时钟就是以正北方向作为0度起点来计算度数的]
第一种写法
double x1 = xsin(rad)*r;
double y1 = y - cos(rad)*r;
第二种写法
double x1 = xsin(rad)*r;
double y1 = ycos(rad 3.14159265358979323846)*r;
但是明显第一种写法更优雅一点 .
所以下面的代码,就可以简化修改
hx=r1*Math.sin(hdeg) x;
hy=r1*Math.cos(hdeg Math.PI) y;
mx=r2*Math.sin(mdeg) x;
my=r2*Math.cos(mdeg Math.PI) y;
sx=r3*Math.sin(sdeg) x;
sy=r3*Math.cos(sdeg Math.PI) y;
建议修改成
hx = xr1 * Math.sin(hdeg);
hy = y - r1 * Math.cos(hdeg);
mx = xr2 * Math.sin(mdeg);
my = y - r2 * Math.cos(mdeg);
sx = xr3 * Math.sin(sdeg);
sy = y - r3 * Math.cos(sdeg);
其他细节:
时钟的效果有严重的锯齿现象, 如下图
修改paintComponent 方法即可
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 把g转成g2d对象
Graphics2D g2d = (Graphics2D) g;
//消除图像锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//消除文本锯齿
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//设置字体格式
g2d.setFont(new Font("宋体",Font.BOLD,18));
//开始绘制
g2d.clearRect(0, 0, 500, 500);
g2d.drawString("我的简易时钟", 210, 50);
g2d.drawString("12", 245, 135);
g2d.drawString("3", 355, 250);
g2d.drawString("6", 245, 365);
g2d.drawString("9", 125, 250);
g2d.drawOval(120, 120, 250, 250);
g2d.drawLine(x, y, (int) hx, (int) hy);
g2d.drawLine(x, y, (int) mx, (int) my);
g2d.drawLine(x, y, (int) sx, (int) sy);
}
Java 编程题,定义一个时钟类(Clock)class Clock{
private int hourse;
private int minute;
private int second;
public Clock(){
this.hourse=0;
this.minute=0;
this.second=0;
}
public void incSecond(){
this.second =1;
}
public void toTime(){
if(this.hourse24){this.hourse=0;}
if(this.minute60){this.hourse =1;this.minute-=60;}
if(this.second60){this.minute =1;this.second-=60;}
System.out.printf(hourse ":" minute ":" second);
}
public void setHourse(int hourse) {
this.hourse = hourse;
}
public void setMinute(int minute) {
this.minute = minute;
}
public void setSecond(int second) {
this.second = second;
}
}
public class test {
public static void main(String[] args) {
Clock c=new Clock();
c.setMinute(66);//测试数据
c.setSecond(61);
c.toTime();//输出1:7:1
}
}
基本功能实现 。。没有添加访问器 。。但有些代码可以修改的更好,可以的话给分我吧 。。谢谢!
用 java 写一个clock的类,100毫秒的时钟 求代码 。。越简单越好 。。最好有注释这里有一个时钟的类,你看一下吧,不明白你的100毫秒的时钟 是什么意思 。
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ClockAs extends Canvas implements ActionListener {
static JButton jb = new JButton("开始");
static JButton jb1 = new JButton("暂停");
Date date;
Timer secondTime;
int hour, munite, second;
Line2D secondLine, muniteLine, hourLine;
int a, b, c;
double pointSX[] = new double[60],// 用来表示秒针端点坐标的数组
pointSY[] = new double[60], pointMX[] = new double[60], // 用来表示分针端点坐标的数组
pointMY[] = new double[60], pointHX[] = new double[60], // 用来表示时针端点坐标的数组
pointHY[] = new double[60];
ClockAs() {
secondTime = new Timer(1000, this);
pointSX[0] = 0; // 12点秒针位置
pointSY[0] = -100;
pointMX[0] = 0; // 12点分针位置
pointMY[0] = -90;
pointHX[0] = 0; // 12点时针位置
pointHY[0] = -70;
double angle = 6 * Math.PI / 180; // 刻度为6度
for (int i = 0; i59; i) // 计算出各个数组中的坐标
{
pointSX[i1] = pointSX[i] * Math.cos(angle) - Math.sin(angle)
* pointSY[i];
pointSY[i1] = pointSY[i] * Math.cos(angle)pointSX[i]
* Math.sin(angle);
pointMX[i1] = pointMX[i] * Math.cos(angle) - Math.sin(angle)
* pointMY[i];
pointMY[i1] = pointMY[i] * Math.cos(angle)pointMX[i]
* Math.sin(angle);
pointHX[i1] = pointHX[i] * Math.cos(angle) - Math.sin(angle)
* pointHY[i];
pointHY[i1] = pointHY[i] * Math.cos(angle)pointHX[i]
* Math.sin(angle);
}
for (int i = 0; i60; i) {
pointSX[i] = pointSX[i]120; // 坐标平移
pointSY[i] = pointSY[i]120;
pointMX[i] = pointMX[i]120; // 坐标平移
pointMY[i] = pointMY[i]120;
pointHX[i] = pointHX[i]120; // 坐标平移
pointHY[i] = pointHY[i]120;
}
secondLine = new Line2D.Double(0, 0, 0, 0);
muniteLine = new Line2D.Double(0, 0, 0, 0);
hourLine = new Line2D.Double(0, 0, 0, 0);
secondTime.start(); // 秒针开始计时
}
public void paint(Graphics g) {
for (int i = 0; i60; i) // 绘制表盘上的小刻度和大刻度
{
int m = (int) pointSX[i];
int n = (int) pointSY[i];
if (i % 5 == 0) {
g.setColor(Color.red);
g.fillOval(m - 4, n - 4, 8, 8);
} else {
g.setColor(Color.cyan);
g.fillOval(m - 2, n - 2, 4, 4);
}
}
g.fillOval(115, 115, 10, 10); // 钟表中心的实心圆
Graphics2D g_2d = (Graphics2D) g;
g_2d.setColor(Color.red);
g_2d.draw(secondLine);
BasicStroke bs = new BasicStroke(3f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.blue);
g_2d.draw(muniteLine);
bs = new BasicStroke(6f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.green);
g_2d.draw(hourLine);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == secondTime) {
date = new Date();
String s = date.toString();
hour = Integer.parseInt(s.substring(11, 13));
munite = Integer.parseInt(s.substring(14, 16));
second = Integer.parseInt(s.substring(17, 19)); // 获取时间中的秒
int h = hour % 12;
a = second; // 秒针端点的坐标
b = munite; // 分针端点的坐标
c = h * 5munite / 12; // 时针端点的坐标
secondLine.setLine(120, 120, (int) pointSX[a], (int) pointSY[a]);
muniteLine.setLine(120, 120, (int) pointMX[b], (int) pointMY[b]);
hourLine.setLine(120, 120, (int) pointHX[c], (int) pointHY[c]);
repaint();
}
if (e.getSource() == jb) {
secondTime.start();
}
if (e.getSource() == jb1) {
secondTime.stop();
}
}
public static void main(String args[]) {
JFrame win = new JFrame("时钟");
JPanel jp = new JPanel();
jp.add(jb);
jp.add(jb1);
ClockAs clock = new ClockAs();
jb.addActionListener(clock);
jb1.addActionListener(clock);
win.add(clock, BorderLayout.CENTER);
win.add(jp, "South");
win.setVisible(true);
win.setSize(246, 300);
win.setDefaultCloseOperation(3);
win.validate();
}
}
JAVA时钟代码解释lz这个代码有什么看不懂java时钟代码解释的 如果这个代码看不懂的话 建议java时钟代码解释你从基础的开始学习吧
首先可以看到extends JFrame implements ActionListener 关于ActionListener 接口和JFrame 类lz有必要去看看 加入api慢慢研究下吧
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;
这些东西java时钟代码解释我也看不懂 因为java时钟代码解释你的代码不完整
final double RAD=Math.PI/180; 这个应该是算一个弧度吧
super("made by mcj and wdh"); 这句无意义 是父类的代码
setDefaultCloseOperation(3);这个方法是什么??代码不完整
setSize(300,300);设置窗体大小
setBackground(Color.white);设置背景色
setLocation(300,150); 定位
setResizable(false);不调整大小
setVisible(true);可见
后面的就没有代码java时钟代码解释了
JAVA中关于时钟的程序,高分跪求解释java时钟代码解释你这个比较有点意思java时钟代码解释,呵呵
就说一个吧,时针怎么算
360 * minutes / (12 * 60) 这个很容易理解 , 就是当前分针多少度 。
90 - 360 * minutes / (12 * 60)
为什么是90减去 360 * minutes / (12 * 60)呢?
其实写成 0 减去 360 * minutes / (12 * 60)也可以 。java时钟代码解释你不过那样java时钟代码解释的话,java时钟代码解释你的表会快3个小时
日常来说的几点是相对于12点的位置来讲的,要是用图形描述的话就是 Y轴,90度的位置
这里用90减去360 * minutes / (12 * 60)实在计算时针距离12点的相对弧度 。
剩下的你自己看吧 。估计差不多了
【java时钟代码解释 java实现时钟】java时钟代码解释的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现时钟、java时钟代码解释的信息别忘了在本站进行查找喔 。

    推荐阅读