<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 303  文章 - 883  trackbacks - 0
    <2007年3月>
    25262728123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    歡迎光臨! 
    閑聊 QQ:1074961813

    隨筆分類(357)

    我管理的群

    公共blog

    • n維空間
    • Email : java3d@126.com 群 : 12999758

    參與管理的論壇

    好友的blog

    我的其他blog

    朋友的網站

    搜索

    •  

    最新評論

    大家好啊這一篇,這是我和網友邪逸一起完成的,多虧邪逸,不然我也許還迷糊著畫錯圖了;呵呵

    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分別是針尾的坐標???????
    ??????? 看圖:
    ??????? 指針坐標.png
    ??????? 這里只證明線段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 &lt;APPLET&gt; tag but isn't running the applet, for some reason."
    ????? Your browser is completely ignoring the &lt;APPLET&gt; 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>
    ??????? &lt;applet code="Clock.class" width=170 height=150&gt;<br>
    ??????? &lt;param name=bgcolor? value="000000"&gt;<br>
    ??????? &lt;param name=fgcolor1 value="ff0000"&gt;<br>
    ??????? &lt;param name=fgcolor2 value="ff00ff"&gt;<br>
    ??????? &lt;/applet&gt;<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

    FeedBack:
    # re: applet實例入門~~~~~~3~~~~~使用jdk/demo/applets/下代碼^^^^^^^外加本人詳細講解  2007-03-19 17:51 L
    多謝多謝,剛入門中。  回復  更多評論
      
    主站蜘蛛池模板: 黄桃AV无码免费一区二区三区| 亚洲AV成人一区二区三区在线看 | 亚洲白色白色永久观看| 大片免费观看92在线视频线视频| 韩国18福利视频免费观看| 亚洲中文无码线在线观看| 久久免费看黄a级毛片 | 国产日韩AV免费无码一区二区| 亚洲午夜日韩高清一区| 一级做a爰片久久毛片免费陪| 亚洲免费视频一区二区三区| 春意影院午夜爽爽爽免费| 亚洲中文久久精品无码| 成人无码WWW免费视频| 亚洲人成亚洲精品| 人与禽交免费网站视频| 77777亚洲午夜久久多喷| 拨牐拨牐x8免费| 大桥未久亚洲无av码在线| 午夜亚洲av永久无码精品| gogo免费在线观看| 亚洲v高清理论电影| 国产乱子精品免费视观看片| 亚洲国产精品成人AV在线 | 女人18毛片水真多免费看| 狼色精品人妻在线视频免费| 日韩亚洲变态另类中文| 一区二区免费视频| 亚洲精华国产精华精华液| 久久亚洲精品无码播放| 99久9在线|免费| 亚洲av午夜国产精品无码中文字| 亚洲天堂免费在线视频| 免费无码毛片一区二区APP| 国产成人亚洲综合网站不卡| 国产精品V亚洲精品V日韩精品| 午夜影院免费观看| 亚洲1区2区3区精华液| 亚洲国产成人精品无码区在线观看| 美女网站免费福利视频| 国产免费福利体检区久久|