<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

    朋友的網(wǎng)站

    搜索

    •  

    最新評論

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

    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;?????? // 創(chuàng)建一個可變的線程
    ??? private int lastxs, lastys, lastxm,
    ??????????????? lastym, lastxh, lastyh;? // Dimensions used to draw hands
    ??? private SimpleDateFormat formatter;? // 創(chuàng)建一個簡單數(shù)據(jù)格式
    ??? private String lastdate;???????????? // 用于保留最后的數(shù)據(jù)
    ??? private Font clockFaceFont;????????? // 鐘面的數(shù)字
    ??? 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());//初始化前面定義的數(shù)據(jù)格式
    ??????? currentDate = new Date();
    ??????? /*?
    ??????? Date() 在 java.util.Date用于獲取當(dāng)前時間,精確到秒
    ??????? */
    ??????? lastdate = formatter.format(currentDate);//用前面定義的格式,格式化時間變量
    ???????
    ??????? clockFaceFont = new Font("Serif", Font.PLAIN, 14);//定義
    ????
    ??????? handColor = Color.blue;//定義分針顏色
    ???????
    ??????? numberColor = Color.darkGray; //定義秒針顏色
    ???????? /*
    ???????? 下面開始定義各個要接收的參數(shù),其實,運行時并沒有用到
    ???????? 如果你有興趣,可以通過定義,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);????????????? // 設(shè)置時鐘窗口大小
    ??? }

    ??? // 核心代碼,實現(xiàn)一個變動的畫圖,前面講到的閃光字代碼和這個原理有所想近
    ??? 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;
    ??????? }
    ???????? /*
    ??????? 這里先調(diào)用formatter.applyPattern("s"),讓s的格式化為前面“EEE MMM dd hh:mm:ss yyyy”中的一種格式;
    ??????? 既而使用s = Integer.parseInt(formatter.format(currentDate))獲取時間,也許有人會問怎么知道他是小時還是
    ??????? 分鐘還是秒;呵呵,這是由前面hh:mm:ss決定的,參數(shù)是從后向前取
    ??????? */
    ??????? 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;
    ??????? }
    ???
    ??????? // 設(shè)置表針最后位置
    ??????? //這里調(diào)用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分別是針尾的坐標(biāo)???????
    ??????? 看圖:
    ??????? 指針坐標(biāo).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?坐標(biāo)xcenter就得到p點的x坐標(biāo)了
    ??????? */
    ??????? 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) {//這里判斷如果指針發(fā)生變動,如果有變動
    ??????????? 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);
    ??????? }

    ??????? // 這里寫鐘面的數(shù)字,簡單,不解釋
    ??????? g.setColor(numberColor);
    ??????? g.drawString(today, 5, 125);???
    ??????? g.drawLine(xcenter, ycenter, xs, ys);//設(shè)置畫指針的參數(shù)
    ??????? 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);
    ??????? //到這里畫完數(shù)字
    ??????? //開始畫我們的時鐘指針
    ??????? g.setColor(numberColor);
    ??????? g.drawString(lastdate, 5, 125);???
    ??????? g.drawLine(xcenter, ycenter, lastxs, lastys);//op坐標(biāo)
    ??????? 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 小尋 閱讀(893) 評論(1)  編輯  收藏 所屬分類: j2se/j2ee/j2me

    FeedBack:
    # re: applet實例入門~~~~~~3~~~~~使用jdk/demo/applets/下代碼^^^^^^^外加本人詳細(xì)講解  2007-03-19 17:51 L
    多謝多謝,剛?cè)腴T中。  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲福利视频导航| 一本色道久久88亚洲精品综合| 国产99视频精品免费视频7| 国产亚洲福利一区二区免费看| AV在线播放日韩亚洲欧| 精品无码国产污污污免费网站| 亚洲男人在线无码视频| 亚洲欧美日韩一区二区三区| 91九色老熟女免费资源站| 久久综合日韩亚洲精品色| 久久av免费天堂小草播放| 免费中文字幕一级毛片| 精品韩国亚洲av无码不卡区| 国产免费爽爽视频免费可以看| 免费观看一区二区三区| 亚洲日本天堂在线| 区久久AAA片69亚洲| 中国内地毛片免费高清| 国产亚洲sss在线播放| 亚洲日韩一页精品发布| 在线免费视频一区| 精品国产日韩久久亚洲| 亚洲精品无码久久千人斩| 久久免费国产精品一区二区| 亚洲AV无码一区二区三区牲色| 久久亚洲国产成人亚| 免费a级黄色毛片| 香蕉97超级碰碰碰免费公| 亚洲人成人77777网站不卡 | 在线观看亚洲免费| 国产好大好硬好爽免费不卡| 高h视频在线免费观看| 亚洲国产成人精品无码一区二区 | h片在线播放免费高清 | 亚洲AV无码男人的天堂| 亚洲欧洲精品在线| 日本亚洲视频在线| 曰批视频免费40分钟试看天天| 国产精品免费大片一区二区| 美女的胸又黄又www网站免费| 精品亚洲成A人无码成A在线观看 |