| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1109 人关注过本帖
标题:[分享]一个时钟的例子
只看楼主 加入收藏
kingarden
Rank: 2
等 级:论坛游民
威 望:1
帖 子:517
专家分:40
注 册:2004-12-8
收藏
 问题点数:0 回复次数:5 
[分享]一个时钟的例子
import java.awt.*;
import java.util.*;
public  class Mywatch

{
    public static void main(String[] args)
    {
        Frame f = new Frame("My Watch");
        Label l = new Label();
        f.add(l);
        f.pack();
        f.setVisible(true);
        while(true)
        {
            Calendar c = Calendar.getInstance();
            l.setText(c.get(Calendar.HOUR_OF_DAY)+" : " +c.get(Calendar.MINUTE)+" : " +c.get(Calendar.SECOND));
            try{Thread.sleep(1000);}
            catch(Exception e){}
        }
        
    }
}
搜索更多相关主题的帖子: 时钟 Calendar get 例子 
2005-01-12 10:56
gundamxzzg
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2004-11-28
收藏
得分:0 

import java.util.*; import java.awt.*; import java.applet.*; import java.text.*;

/** * Time! * * @author Rachel Gollub * @modified Daniel Peek replaced circle drawing calculation, few more changes */ public class Clock extends Applet implements Runnable { private volatile Thread timer; // The thread that displays clock private int lastxs, lastys, lastxm, lastym, lastxh, lastyh; // Dimensions used to draw hands private SimpleDateFormat formatter; // Formats the date displayed private String lastdate; // String to hold date displayed private Font clockFaceFont; // Font for number display on clock private Date currentDate; // Used to get date to display private Color handColor; // Color of main hands and dial private Color numberColor; // Color of second hand and numbers private int xcenter = 80, ycenter = 55; // Center position

public void init() { int x,y; lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0; formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); currentDate = new Date(); lastdate = formatter.format(currentDate); clockFaceFont = new Font("Serif", Font.PLAIN, 14); handColor = Color.blue; numberColor = Color.darkGray;

try { setBackground(new Color(Integer.parseInt(getParameter("bgcolor"), 16))); } catch (NullPointerException e) { } catch (NumberFormatException e) { } try { handColor = new Color(Integer.parseInt(getParameter("fgcolor1"), 16)); } catch (NullPointerException e) { } catch (NumberFormatException e) { } try { numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"), 16)); } catch (NullPointerException e) { } catch (NumberFormatException e) { } resize(300,300); // Set clock window size }

// Paint is the main part of the program public void update(Graphics g) { int xh, yh, xm, ym, xs, ys; int s = 0, m = 10, h = 10; String today;

currentDate = new Date(); formatter.applyPattern("s"); try { s = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { s = 0; } formatter.applyPattern("m"); try { m = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { m = 10; } formatter.applyPattern("h"); try { h = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { h = 10; } // Set position of the ends of the hands xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter); ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter); xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter); ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter); xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + xcenter); yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + ycenter); // Get the date to print at the bottom formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy"); today = formatter.format(currentDate);

g.setFont(clockFaceFont); // Erase if necessary g.setColor(getBackground()); if (xs != lastxs || ys != lastys) { g.drawLine(xcenter, ycenter, lastxs, lastys); g.drawString(lastdate, 5, 125); } if (xm != lastxm || ym != lastym) { g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); } if (xh != lastxh || yh != lastyh) { g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }

// Draw date and hands g.setColor(numberColor); g.drawString(today, 5, 125); g.drawLine(xcenter, ycenter, xs, ys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, xm, ym); g.drawLine(xcenter-1, ycenter, xm, ym); g.drawLine(xcenter, ycenter-1, xh, yh); g.drawLine(xcenter-1, ycenter, xh, yh); lastxs = xs; lastys = ys; lastxm = xm; lastym = ym; lastxh = xh; lastyh = yh; lastdate = today; currentDate = null; }

public void paint(Graphics g) { g.setFont(clockFaceFont); // Draw the circle and numbers g.setColor(handColor); g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360); g.setColor(numberColor); g.drawString("9", xcenter-45, ycenter+3); g.drawString("3", xcenter+40, ycenter+3); g.drawString("12", xcenter-5, ycenter-37); g.drawString("6", xcenter-3, ycenter+45);

// Draw date and hands g.setColor(numberColor); g.drawString(lastdate, 5, 125); g.drawLine(xcenter, ycenter, lastxs, lastys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }

public void start() { timer = new Thread(this); timer.start(); }

public void stop() { timer = null; }

public void run() { Thread me = Thread.currentThread(); while (timer == me) { try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { } repaint(); } }

public String getAppletInfo() { return "Title: A Clock \n" + "Author: Rachel Gollub, 1995 \n" + "An analog clock."; } public String[][] getParameterInfo() { String[][] info = { {"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."}, {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue."}, {"fgcolor2", "hexadecimal RGB number", "The color of the second hand and numbers. Default is dark gray."} }; return info; } } 这个时钟的例子我想不下10万人看过吧


2005-02-26 19:01
shanji
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2005-1-23
收藏
得分:0 
2005-04-07 16:45
逝去身影
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-4-29
收藏
得分:0 
我一定学好java 哈哈哈哈

2005-04-30 16:15
Doctor
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2005-5-17
收藏
得分:0 
不错,能用这么一小段代码写出来,是要点油水的!(虽然界面不怎样!)

我本将心向明月,奈何明月照沟渠。 知我者谓我心忧,不知我者谓我何求?
2005-05-21 22:41
freedxw
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2005-3-31
收藏
得分:0 

import java.awt.* ; import javax.swing.* ; import javax.swing.Timer ; import java.awt.event.* ; import java.util.* ; import java.awt.geom.* ; import java.util.Date ; import java.text.SimpleDateFormat ;

class Happy extends Panel implements ActionListener { double pointy[] = new double[61] ; double pointx[] = new double[61] ; double scaledx[] = new double[61] ; double scaledy[] = new double[61] ; Color c ; Timer Timers ; int Cnum = 0 ; Happy() { c = getBackground() ;//new Color(157,152,200); Timers = new Timer(1000,this); Timers.start() ; pointx[0]=0; pointy[0]=-120; scaledx[0]=0; scaledy[0]=-140; double jiaodu=6*Math.PI/180; for(int i=0;i<60;i++) { pointx[i+1]=pointx[i]*Math.cos(jiaodu)-pointy[i]*Math.sin(jiaodu); pointy[i+1]=pointy[i]*Math.cos(jiaodu)+pointx[i]*Math.sin(jiaodu); } pointx[60]=0; pointy[60]=-120; for(int i=0;i<60;i++) { scaledx[i+1]=scaledx[i]*Math.cos(jiaodu)-Math.sin(jiaodu)*scaledy[i]; scaledy[i+1]=scaledy[i]*Math.cos(jiaodu)+Math.sin(jiaodu)*scaledx[i]; } scaledx[60]=0;scaledy[60]=-140; }

public void paint(Graphics g) {

Cnum = (Cnum+2)%360; Color Col = new Color((3*Cnum)%225,(7*Cnum)%225,(11*Cnum)%225) ; g.drawOval(50,50,300,300) ; g.translate(200,200) ; for(int i=0;i<60;i++) { if(i%5==0) { g.setColor(Col); g.fillOval((int)scaledx[i],(int)scaledy[i],8,8) ; } else { g.fillOval((int)scaledx[i],(int)scaledy[i],3,3) ; } } Date date=new Date(); String s=date.toString(); int houra ; int hourb ; int munitea ; int muniteb ; int seconda ; int secondb ; int hour=0 ; int munite=0 ; int second=0 ; 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; int Hi=h*5+munite/12; int Mi=munite; int Si=second; seconda=(int)pointx[(Si)%60]; secondb=(int)pointy[(Si)%60]; munitea=(int)pointx[(Mi)%60]; muniteb=(int)pointy[(Mi)%60]; houra=(int)pointx[(Hi)%60]; hourb=(int)pointy[(Hi)%60]; g.setColor(c); g.drawLine(0,0,seconda,secondb); g.drawString(s.substring(17,19),seconda,secondb); g.setColor(Color.magenta); g.drawLine(0,0,seconda,secondb); g.drawString(s.substring(17,19),seconda,secondb); g.setColor(c); g.drawLine(0,0,munitea,muniteb); g.drawString(s.substring(14,16),munitea,muniteb); g.setColor(Color.blue); g.drawLine(0,0,munitea,muniteb); g.drawString(s.substring(14,16),munitea,muniteb); g.setColor(c); g.drawLine(0,0,houra,hourb); g.drawString(s.substring(11,13),houra,hourb); g.setColor(Color.red); g.drawLine(0,0,houra,hourb); g.drawString(s.substring(11,13),houra,hourb); munite=0; second=0; } public void actionPerformed(ActionEvent AE) { if(AE.getSource()==Timers) { repaint(); } } } 试试看,在Panel上画的。


天不知道,地知道, 你不知道,我知道!
2005-06-20 17:28
快速回复:[分享]一个时钟的例子
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017904 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved