你这个比较有点意思, 呵呵
就说一个吧,时针怎么算
360 minutes / (12 60) 这个很容易理解,就是当前分针多少度。
90 - 360 minutes / (12 60)
为什么是90减去 360 minutes / (12 60)呢?
其实写成 0 减去 360 minutes / (12 60)也可以。你不过那样的话,你的表会快3个小时
日常来说的几点是相对于12点的位置来讲的,要是用图形描述的话就是 Y轴, 90度的位置
这里用90减去360 minutes / (12 60)实在计算时针距离12点的相对弧度。
剩下的你自己看吧。估计差不多了
import javautil;
import javaawt;
import javaapplet;
import javatext;
import javaawtevent;
public class Alarm extends Applet implements Runnable
{
Thread timer=null; //创建线程timer
Image gif1; //clockp:闹钟的外壳,闹铃和报时物
boolean setflag=false,stopflag=false,cancelflag=false;
Panel setpanel;
//获取声音文件
AudioClip ring=getAudioClip(getCodeBase(), "1mid");
Button setbutton=new Button("SET");
Button cancelbutton=new Button("CANCEL");
Button stopbutton=new Button("STOP");
//响应按钮事件
private ActionListener setli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setflag=true;
}
};
private ActionListener cancelli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setflag=true;
}
};
private ActionListener stopli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ringstop();
//清除的方法
//gclearRect(83,280,20,30);
}
};
Label note1=new Label("Alarm clock:");
//GregorianCalendar提供的是一个日历式的东东,上面又多了很多的参数,是方便 *** 作了不少。而Date类的功能远不及其,求个和日期有联系的还要自己计算。
GregorianCalendar cal=new GregorianCalendar();
GregorianCalendar cal2=new GregorianCalendar();
SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式
Date dummy=new Date(); //生成Data对象
String lastdate=dfformat(dummy);
Font F=new Font("TimesRoman",FontPLAIN,14);//设置字体格式
Date dat=null;
Date timeNow;
Color fgcol=Colorblue;
Color fgcol2=ColordarkGray;
Color backcolor=Colorblue;
Label hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)
int i;
int s,m,h;
TextField sethour,setmin,setsec;//显示当前时间文本框和定时文本框
//在Applet程序中,首先自动调用初始化完成必要的初始化工作,紧接着自动调用start,在进入执行程序和返回到该页面时被调用,而从该页面转到别的页面时,stop被调用,关闭浏览器时,执行destroy。
public void init()//初始化方法
{
int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数
setLayout(null); //将布局管理器初始化为null
setpanel=new Panel();
setpanelsetLayout(null);
setpaneladd(note1);
note1setBounds(30,100,60,20);
note1setBackground(backcolor);
note1setForeground(Colorblack);
//定时用的文本框(时、分、秒)
sethour=new TextField("00",5);
setmin=new TextField("00",5);
setsec=new TextField("00",5);
hlabel2=new Label();
mlabel2=new Label();
slabel2=new Label();
//定时的小时文本框的位置、大小
setpaneladd(sethour);
sethoursetBounds(fieldx,fieldy2,fieldw,fieldh);
sethoursetBackground(Colorwhite);
//在文本框后加入单位“时”
setpaneladd(hlabel2);
hlabel2setText("h");
hlabel2setBackground(backcolor);
hlabel2setForeground(Colorblack);
hlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定时的分钟文本框的位置、大小
setpaneladd(setmin);
setminsetBounds(fieldx,fieldy2,fieldw,fieldh);
setminsetBackground(Colorwhite);
//在文本框后加入单位“分”
setpaneladd(mlabel2);
mlabel2setText("m");
mlabel2setBackground(backcolor);
mlabel2setForeground(Colorblack);
mlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定时的秒文本框的位置、大小
setpaneladd(setsec);
setsecsetBounds(fieldx,fieldy2,fieldw,fieldh);
setsecsetBackground(Colorwhite);
//在文本框后加入单位“秒”
setpaneladd(slabel2);
slabel2setText("s");
slabel2setBackground(backcolor);
slabel2setForeground(Colorblack);
slabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);
//设置闹钟控制按钮(on,off)
setpaneladd(cancelbutton);
setpaneladd(setbutton);
setpaneladd(stopbutton);
cancelbuttonsetBounds(90,180,40,20);
setbuttonsetBounds(140,180,40,20);
stopbuttonsetBounds(522,180,40,20);
setbuttonaddActionListener(setli);
cancelbuttonaddActionListener(cancelli);
stopbuttonaddActionListener(stopli);
stopbuttonsetVisible(false);
//将面板加入当前容器中,并设置面板的大小和背景色
add(setpanel);
setpanelsetBounds(300,1,250,420);
setpanelsetBackground(backcolor);
/int xcenter,ycenter,s,m,h;
//闹钟中心点所在位置
xcenter=145;
ycenter=162;
s=(int)calget(CalendarSECOND);
m=(int)calget(CalendarMINUTE);
h=(int)calget(CalendarHOUR_OF_DAY);
//初始化指针位置
lastxs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);
lastys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);
lastxm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);
lastym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);
lastxh=(int)(Mathcos((h30+m/2)314f/180-314f/2)18+xcenter);
lastyh=(int)(Mathsin((h30+m/2)314f/180-314f/2)18+ycenter);
lasts=s; /
MediaTracker mt=new MediaTracker(this);//为给定组件创建一个跟踪媒体的MediaTracker对象,把添加到被跟踪的组
//Java允SappletHTML所在的位置(decument base)下dY料,也允Sapplet钠涑淌酱a所在的位置(code base)下dY料。藉由呼叫getDocumentBase()cgotCodeBase()可得到URL物件。@些函湍阏业侥阆胂螺d的n案的位置
//clockp=getImage(getDocumentBase(),"11png");
gif1=getImage(getCodeBase(),"2gif");
//i为id号
mtaddImage(gif1,i++);
try
{
mtwaitForAll();
}
catch(InterruptedException e)
{};//等待加载结束
resize(600,420);//设置窗口大小
}
//窗口显示有改变的时候调用paint
public void paint(Graphics g)
{//重写paint()方法
int xh,yh,xm,ym,xs,ys,strike_times;
int xcenter,ycenter;
String today;
xcenter=148;
ycenter=186;
dat=new Date();
//用当前时间初始化日历时间
calsetTime(dat);
//读取当前时间
s=(int)calget(CalendarSECOND);
m=(int)calget(CalendarMINUTE);
h=(int)calget(CalendarHOUR_OF_DAY);
//换一种时间表达形式
today=dfformat(dat);
//指针位置
xs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);
ys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);
xm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);
ym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);
xh=(int)(Mathcos((h30+m/2)314f/180-314f/2)12+xcenter);
yh=(int)(Mathsin((h30+m/2)314f/180-314f/2)12+ycenter);
//设置字体和颜色
gsetFont(F);
//前景色
gsetColor(getBackground()); //取背景色的
gdrawImage(gif1,75,110,this);
//以数字方式显示年、月、日和时间
gdrawString(today,55,415);
//画指针
gdrawLine(xcenter,ycenter,xs,ys);
gdrawLine(xcenter,ycenter-1,xm,ym); //(x1,y1,x2,y2)
gdrawLine(xcenter-1,ycenter,xm,ym);
gdrawLine(xcenter,ycenter-1,xh,yh);
gdrawLine(xcenter-1,ycenter,xh,yh);
int timedelta;//记录当前时间与闹铃定时的时差
Integer currh,currm,currs;//分别记录当前的时、分、秒
Date dat2=new Date();
cal2setTime(dat2);
//读取当前时间
currh=(int)cal2get(CalendarSECOND);
currm=(int)cal2get(CalendarMINUTE);
currs=(int)cal2get(CalendarHOUR_OF_DAY);
//这样做的话说我API已过时
//timeNow=new Date();
//currh=new Integer(timeNowgetHours());
//currm=new Integer(timeNowgetMinutes());
//currs=new Integer(timeNowgetSeconds());
if(setflag)
{ //判断是否设置了闹钟
//判断当前时间是否为闹钟所定的时间
if((currhintValue()==IntegervalueOf(sethourgetText())intValue())&&(currmintValue()==IntegervalueOf(setmingetText())intValue())&&(currsintValue()==IntegervalueOf(setsecgetText())intValue()))
{
ringplay();
gdrawImage(gif1,83,280,this);
stopbuttonsetVisible(true);
}
timedelta=currmintValue()60+currsintValue()-IntegervalueOf(setmingetText())intValue()60-IntegervalueOf(setsecgetText())intValue();
if((timedelta>=30))
{
//若当前时间与闹钟相差时间超过30秒,闹钟自动停
ringstop();
//清除的方法
gclearRect(83,280,20,30);
}
}
dat=null;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);//将timer实例化
timerstart();
}
}
public void stop()
{
timer=null;
}
//给创建线程后start之后自动执行的函数
public void run()
{
//在run()方法中,调用repaint()方法,以重绘小程序区,进行时钟显示的更新。接着调用sleep方法让当前线程(也就是我们创建的线程clockthread)睡眠1000毫秒,因为我们每秒钟要更新一下显示,所以让它睡眠1秒
while(timer!=null)
{
try
{
timersleep(1000);
}
catch(InterruptedException e)
{}
//调用repaint时,会首先清除掉paint方法之前的画的内容,再调用paint方法
repaint();//刷新画面
}
timer=null;
}
//当AWT接收到一个applet的重绘请求时,它就调用applet的 update(),默认地,update() 清除applet的背景,然后调用 paint()。重载 update(),将以前在paint()中的绘图代码包含在update()中,从而避免每次重绘时将整个区域清除
//有两种方法可以明显地减弱闪烁:重载 update()或使用双缓冲。
//使用双缓冲技术:另一种减小帧之间闪烁的方法是使用双缓冲,它在许多动画Applet中被使用。其主要原理是创建一个后台图像,将需要绘制的一帧画入图像,然后调用DrawImage()将整个图像一次画到屏幕上去;好处是大部分绘制是离屏的,将离屏图像一次绘至屏幕上比直接在屏幕上绘制要有效得多,大大提高做图的性能。
// 双缓冲可以使动画平滑,但有一个缺点,要分配一张后台图像,如果图像相当大,这将需要很大一块内存;当你使用双缓冲技术时,应重载 update()。
public void update(Graphics g)
{
Image offscreen_buf=null;
//采用双缓冲技术的update()方法
if(offscreen_buf==null)
offscreen_buf=createImage(600,420);
Graphics offg=offscreen_bufgetGraphics();
offgclipRect(1,1,599,419);
paint(offg);
Graphics ong=getGraphics();
ongclipRect(1,1,599,419);
ongdrawImage(offscreen_buf,0,0,this);
}
/ Creates a new instance of AlarmClock /
}
import javaawt;
import javaawtevent;
import javaxswing;
import sunutilcalendarGregorian;
import javautilCalendar;
import javautilGregorianCalendar;
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 = MathPI/180;
public ClockPointer(){
super("Java时钟");
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
Image image = getToolkit()getImage("clockgif");
setIconImage(image);
setSize(400,400);
setBackground(Colorwhite);
//setLocation(300,150);
thissetLocationRelativeTo(null);
setResizable(true);
int delay = 1000;
//创建一个监听事件
setVisible(true);
ActionListener drawClock = new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//创建一个时间计数器,每一秒触发一次
new Timer(delay, drawClock)start();
}
javatextSimpleDateFormat fmTime = new javatextSimpleDateFormat("HH:mm:ss");
//绘制图形
public void paint(Graphics g){
superpaint(g);
gsetFont(null);
Graphics2D g2D = (Graphics2D)g;
Insets insets = getInsets();
int L = insetsleft/2, T = insetstop/2;
h = getSize()height;
gsetColor(Colorwhite);
//画圆
g2DsetStroke(new BasicStroke(20f));
gsetColor(Colorgray);
gdrawOval(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)Mathcos(RADang)+x0);
y = (int)((r+10)Mathsin(RADang)+y0);
gsetColor(Colorblack);
gdrawString(""+i, x, h-y);
ang -=30;
}
//获得现在的时间
Calendar now = new GregorianCalendar();
int nowh = nowget(CalendarHOUR_OF_DAY);
int nowm = nowget(CalendarMINUTE);
int nows = nowget(CalendarSECOND);
String st=fmTimeformat(nowgetTime());
//在窗体上显示时间
gsetColor(Colorpink);
gfillRect(L, T, 50, 28);
gsetColor(Colorblue);
gdrawString(st,L+2,T+26);
//计算时间与度数的关系
ss = 90 - nows6;
mm = 90 - nowm6;
hh = 90 - nowh30 - nowm/2;
x0 = r+40+L;
y0 = r+40+T;
g2DsetStroke(new BasicStroke(12f));
//擦除秒针
//if(olds_x > 0){
// gsetColor(getBackground());
// // gsetColor(Colorgray);
// gdrawLine(x0, y0, olds_x, h-olds_y); // ()
//}
//绘制秒针
x = (int)(r09Mathcos(RADss))+x0;
y = (int)(r09Mathsin(RADss))+y0-2T;
gsetColor(Coloryellow);
gdrawLine(x0, y0, x, h-y);
olds_x = x;
olds_y = y;
g2DsetStroke(new BasicStroke(22f));
//擦除分针
//if(old_m!=mm){
// gsetColor(getBackground());
// gdrawLine(x0,y0,oldm_x,h-oldm_y);
//}
//绘制分针
x = (int)(r07Mathcos(RADmm))+x0;
y = (int)(r07Mathsin(RADmm))+y0-2T;
gsetColor(Colorgreen);
gdrawLine(x0,y0,x,h-y);
oldm_x = x;
oldm_y = y;
old_m = mm;
g2DsetStroke(new BasicStroke(32f));
//擦除时针
//if(old_h!=hh){
// gsetColor(getBackground());
// gdrawLine(x0,y0,oldh_x,h-oldh_y);
//}
//绘制时针
x = (int)(r05Mathcos(RADhh))+x0;
y = (int)(r05Mathsin(RADhh))+y0-2T;
gsetColor(Colorred);
gdrawLine(x0,y0,x,h-y);
oldh_x = x;
oldh_y = y;
old_h = hh;
}
public static void main(String[] args){
new ClockPointer();
}
}
//整理一下
这里有一个时钟的类,你看一下吧,不明白你的 100毫秒的时钟 是什么意思。
import javaawtBasicStroke;
import javaawtBorderLayout;
import javaawtCanvas;
import javaawtColor;
import javaawtGraphics;
import javaawtGraphics2D;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawtgeomLine2D;
import javautilDate;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingTimer;
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 MathPI / 180; // 刻度为6度
for (int i = 0; i < 59; i++) // 计算出各个数组中的坐标
{
pointSX[i + 1] = pointSX[i] Mathcos(angle) - Mathsin(angle)
pointSY[i];
pointSY[i + 1] = pointSY[i] Mathcos(angle) + pointSX[i]
Mathsin(angle);
pointMX[i + 1] = pointMX[i] Mathcos(angle) - Mathsin(angle)
pointMY[i];
pointMY[i + 1] = pointMY[i] Mathcos(angle) + pointMX[i]
Mathsin(angle);
pointHX[i + 1] = pointHX[i] Mathcos(angle) - Mathsin(angle)
pointHY[i];
pointHY[i + 1] = pointHY[i] Mathcos(angle) + pointHX[i]
Mathsin(angle);
}
for (int i = 0; i < 60; 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 Line2DDouble(0, 0, 0, 0);
muniteLine = new Line2DDouble(0, 0, 0, 0);
hourLine = new Line2DDouble(0, 0, 0, 0);
secondTimestart(); // 秒针开始计时
}
public void paint(Graphics g) {
for (int i = 0; i < 60; i++) // 绘制表盘上的小刻度和大刻度
{
int m = (int) pointSX[i];
int n = (int) pointSY[i];
if (i % 5 == 0) {
gsetColor(Colorred);
gfillOval(m - 4, n - 4, 8, 8);
} else {
gsetColor(Colorcyan);
gfillOval(m - 2, n - 2, 4, 4);
}
}
gfillOval(115, 115, 10, 10); // 钟表中心的实心圆
Graphics2D g_2d = (Graphics2D) g;
g_2dsetColor(Colorred);
g_2ddraw(secondLine);
BasicStroke bs = new BasicStroke(3f, BasicStrokeCAP_ROUND,
BasicStrokeJOIN_MITER);
g_2dsetStroke(bs);
g_2dsetColor(Colorblue);
g_2ddraw(muniteLine);
bs = new BasicStroke(6f, BasicStrokeCAP_BUTT, BasicStrokeJOIN_MITER);
g_2dsetStroke(bs);
g_2dsetColor(Colorgreen);
g_2ddraw(hourLine);
}
public void actionPerformed(ActionEvent e) {
if (egetSource() == secondTime) {
date = new Date();
String s = datetoString();
hour = IntegerparseInt(ssubstring(11, 13));
munite = IntegerparseInt(ssubstring(14, 16));
second = IntegerparseInt(ssubstring(17, 19)); // 获取时间中的秒
int h = hour % 12;
a = second; // 秒针端点的坐标
b = munite; // 分针端点的坐标
c = h 5 + munite / 12; // 时针端点的坐标
secondLinesetLine(120, 120, (int) pointSX[a], (int) pointSY[a]);
muniteLinesetLine(120, 120, (int) pointMX[b], (int) pointMY[b]);
hourLinesetLine(120, 120, (int) pointHX[c], (int) pointHY[c]);
repaint();
}
if (egetSource() == jb) {
secondTimestart();
}
if (egetSource() == jb1) {
secondTimestop();
}
}
public static void main(String args[]) {
JFrame win = new JFrame("时钟");
JPanel jp = new JPanel();
jpadd(jb);
jpadd(jb1);
ClockAs clock = new ClockAs();
jbaddActionListener(clock);
jb1addActionListener(clock);
winadd(clock, BorderLayoutCENTER);
winadd(jp, "South");
winsetVisible(true);
winsetSize(246, 300);
winsetDefaultCloseOperation(3);
winvalidate();
}
}
以上就是关于JAVA中关于时钟的程序,高分跪求解释全部的内容,包括:JAVA中关于时钟的程序,高分跪求解释、JAVA 闹钟程序、JAVA画时钟代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)