大家好啊這一篇,這是我和網友邪逸一起完成的,多虧邪逸,不然我也許還迷糊著畫錯圖了;呵呵
Clock.java 代碼
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class Clock extends Applet implements Runnable {
??? private volatile Thread timer;?????? // 創建一個可變的線程
??? private int lastxs, lastys, lastxm,
??????????????? lastym, lastxh, lastyh;? // Dimensions used to draw hands
??? private SimpleDateFormat formatter;? // 創建一個簡單數據格式
??? private String lastdate;???????????? // 用于保留最后的數據
??? private Font clockFaceFont;????????? // 鐘面的數字
??? 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; // 中間點
??? 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();
??????? /*?
??????? Date() 在 java.util.Date用于獲取當前時間,精確到秒
??????? */
??????? lastdate = formatter.format(currentDate);//用前面定義的格式,格式化時間變量
???????
??????? clockFaceFont = new Font("Serif", Font.PLAIN, 14);//定義
????
??????? handColor = Color.blue;//定義分針顏色
???????
??????? numberColor = Color.darkGray; //定義秒針顏色
???????? /*
???????? 下面開始定義各個要接收的參數,其實,運行時并沒有用到
???????? 如果你有興趣,可以通過定義,html文件上的
???????? <applet code="Clock.class" width=170 height=150>
???????? <param name=bgcolor value="000000">
???????? <param name=fgcolor1 value="ff0000">
???????? <param name=fgcolor2 value="ff00ff">
???????? </applet>
???????? 定義要顯示的顏色
???????? */
??????? 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);????????????? // 設置時鐘窗口大小
??? }
??? // 核心代碼,實現一個變動的畫圖,前面講到的閃光字代碼和這個原理有所想近
??? 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("s"),讓s的格式化為前面“EEE MMM dd hh:mm:ss yyyy”中的一種格式;
??????? 既而使用s = Integer.parseInt(formatter.format(currentDate))獲取時間,也許有人會問怎么知道他是小時還是
??????? 分鐘還是秒;呵呵,這是由前面hh:mm:ss決定的,參數是從后向前取
??????? */
??????? 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;
??????? }
???
??????? // 設置表針最后位置
??????? //這里調用PI圓周率
??????? 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);
??????? /*
??????? 這里只解釋這一個,xs,ys分別是針尾的坐標???????
??????? 看圖:
??????? 
??????? 這里只證明線段ok的大小,至于kp就照樣畫葫蘆,容易
????????這里ok=op×cos<pok=45×cos<pok,所以只要求出cos<pok問題解決
??????? cos<pok的計算使用的鐘面的時間7.5,cos<pok=cos(7.5×pi/2 - pi/2)
????????講得到的ok?長度加上0點的x?坐標xcenter就得到p點的x坐標了
??????? */
??????? 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);
??????? }
??????? // 這里寫鐘面的數字,簡單,不解釋
??????? 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);
??????? //到這里畫完數字
??????? //開始畫我們的時鐘指針
??????? g.setColor(numberColor);
??????? g.drawString(lastdate, 5, 125);???
??????? g.drawLine(xcenter, ycenter, lastxs, lastys);//op坐標
??????? 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);//這里用的是毫秒100=1秒
??????????? } catch (InterruptedException e) {
??????????? }
??????????? repaint();
??????? }
??? }
}
index.htm代碼:
<HTML>
? <HEAD>
??? <TITLE>A Clock (1.6)</TITLE>
? </HEAD>
? <BODY>
??? <h1>A Clock (1.6)</h1>
??? <hr>
??? <applet code="Clock.class" width=170 height=150>
????? alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
????? Your browser is completely ignoring the <APPLET> tag!
</applet>
????? <p>
??????? The clock applet now has three parameters; the background
??????? color (bgcolor), the main foreground color (the hands and
??????? dial) (fgcolor1) and the secondary foreground color (the
??????? seconds hand and numbers) (fgcolor2).? These three parameters
??????? are hexadecimal RGB numbers (like the ones used for the body
??????? bgcolor tag in HTML).? For example:
????? <p>
??????? <applet code="Clock.class" width=170 height=150><br>
??????? <param name=bgcolor? value="000000"><br>
??????? <param name=fgcolor1 value="ff0000"><br>
??????? <param name=fgcolor2 value="ff00ff"><br>
??????? </applet><p>
??????? would give you a black background, a red dial and hands, and purple numbers.
????? <p>
??????? For those who don't convert to hexadecimal easily, here are some common
??????? values:
????? <ul>
??????? <li>black = 000000
??????? <li>blue = 0000ff
??????? <li>cyan = 00ffff
??????? <li>darkGray = 404040
??????? <li>gray = 808080
??????? <li>green = 00ff00
??????? <li>lightGray = c0c0c0
??????? <li>magenta = ff00ff
??????? <li>orange = ffc800
??????? <li>pink = ffafaf
??????? <li>red = ff0000
??????? <li>white = ffffff
??????? <li>yellow = ffff00
????? </ul>
????? <hr>
????? <a href="Clock.java">The source</a>.
? </BODY>
</HTML>
地震讓大伙知道:居安思危,才是生存之道。
posted on 2007-03-16 23:41
小尋 閱讀(889)
評論(1) 編輯 收藏 所屬分類:
j2se/j2ee/j2me