<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用于獲取當前時間,精確到秒
    ??????? */
    ??????? 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分別是針尾的坐標???????
    ??????? 看圖:
    ??????? 指針坐標.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) {//這里判斷如果指針發(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坐標
    ??????? 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
    多謝多謝,剛?cè)腴T中。  回復(fù)  更多評論
      
    主站蜘蛛池模板: 国产色无码精品视频免费| 台湾一级毛片永久免费| 亚洲人成电影福利在线播放 | 亚洲av日韩av天堂影片精品| 国产精品免费网站| 特a级免费高清黄色片| 国产日韩亚洲大尺度高清| 97人妻无码一区二区精品免费| 日本亚洲中午字幕乱码| 亚洲一区影音先锋色资源| 女人被男人桶得好爽免费视频| WWW国产成人免费观看视频| 亚洲精品乱码久久久久久下载 | 无人视频在线观看免费播放影院| 亚洲AV无码专区亚洲AV伊甸园| 影音先锋在线免费观看| 中文字幕免费播放| 亚洲久热无码av中文字幕| 亚洲处破女AV日韩精品| 国产精品免费视频网站| **一级一级毛片免费观看| 男女作爱免费网站| va天堂va亚洲va影视中文字幕| 亚洲人成网77777色在线播放| 四虎在线视频免费观看| 99热在线观看免费| 一级做a免费视频观看网站| 亚洲永久在线观看| 亚洲色四在线视频观看| 久久亚洲av无码精品浪潮| 成年网站免费视频A在线双飞| 久久精品视频免费看| h片在线播放免费高清| 国产偷国产偷亚洲清高APP| 久久久亚洲欧洲日产国码是AV| 亚洲精品无码你懂的网站| 日本免费一区二区三区最新vr| 97碰公开在线观看免费视频| 一级毛片全部免费播放| 最近2019中文免费字幕在线观看| 免费看黄福利app导航看一下黄色录像|