南郵JAVA程式設計實驗4 線程程式設計(指標式時鐘)

來源:互聯網
上載者:User

標籤:南郵java程式設計實驗

南郵JAVA程式設計實驗4  線程程式設計(指標式時鐘)


實驗目的:

本實驗旨在通過實驗,培養學生將JAVA 線程的相關知識點(包括線程調度,線程同步等)有機結合并加以綜合應用,在實驗中設計多線程程式的能力。


實驗內容:

設計和編寫一個編寫一個指標式時鐘程式,應用線程實現時鐘的走動。


實驗設計:

主要是控制時針分針秒針的轉動度數,這個直接通過座標的三角函數值求得,線程方面,隔一秒休眠一下,然後通過時分秒的換算關係來改變三個對應指示針在時鐘上的位置


實驗代碼:

import java.awt.*;import javax.swing.*;import java.util.*;public class JavaClock extends JFrame {    public JavaClock() {        ClockPaint cp = new ClockPaint(20, 20, 70);        this.add(cp);        this.setSize(200, 200);        this.setLocation(260, 120);        this.setTitle("指標時鐘");        this.setVisible(true);        this.setResizable(false);    }    public static void main(String[] s) {        new JavaClock();    }}class ClockPaint extends JPanel implements Runnable {    int x, y, r;    int hour, minute, second; //時,分,秒    final double rad = Math.PI / 180;    public ClockPaint(int x, int y, int r) {        this.x = x;        this.y = y;        this.r = r;        Calendar now = new GregorianCalendar();        //獲得時間轉換成度數        second = now.get(Calendar.SECOND) * 6;        minute = now.get(Calendar.MINUTE) * 6;        hour = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30 + now.get(Calendar.MINUTE) / 12 * 6;        Thread t = new Thread(this);        t.start();    }    public void paint(Graphics g) {        super.paint(g);        g.setColor(Color.WHITE);        g.fillRect(0, 0, r * 3, r * 3);        g.setColor(Color.BLACK);        g.drawOval(x, y, r * 2, r * 2);        //秒針        g.setColor(Color.GREEN);        int x1 = (int)((r - 10) * Math.sin(rad * second));        int y1 = (int)((r - 10) * Math.cos(rad * second));        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);        //分針        g.setColor(Color.BLUE);        x1 = (int)((r - r / 2.5) * Math.sin(rad * minute));        y1 = (int)((r - r / 2.5) * Math.cos(rad * minute));        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);        //時針        g.setColor(Color.RED);        x1 = (int)((r - r / 1.5) * Math.sin(rad * hour));        y1 = (int)((r - r / 1.5) * Math.cos(rad * hour));        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);        //數字        g.setColor(Color.BLACK);        int d = 28;        for (int i = 1; i <= 12; i++) {            x1 = (int)((r - 10) * Math.sin(rad * d));            y1 = (int)((r - 10) * Math.cos(rad * d));            g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);            d += 30;        }        //刻度標記        d = 0;        for (int i = 0; i < 60; i++) {            int len = 0; //控制每個分割點的長度,數字點上的分割線長一點            if(d % 30 == 0) {                len = 5;            }            else {                len = 2;            }            for (int j = 1; j <= len; j++)            {                x1 = (int)((r - j) * Math.sin(rad * d));                y1 = (int)((r - j) * Math.cos(rad * d));                g.drawString(".", x + r + x1 - 1, x + r - y1 + 1);            }            d += 6;        }    }    public void run() {        while (true) {            try {                Thread.sleep(1000); //休眠一秒            }            catch (Exception ex) {            }            second += 6; //每一秒,秒針動6度            if (second == 60 || second == 120 || second == 180 || second == 240 || second == 300) {                minute += 1;  //每10秒,分針動1度            }            //大於一分鐘,分針和時針開始出現改變            if (second == 360) {                second = 0;                minute += 1;                if (minute == 72 || minute == 144 || minute == 216 || minute == 288) {                    hour += 6;                }                if (minute >= 360) {                    minute = 0;                    hour += 6;                }                if (hour >= 360) {                    hour = 0;                }            }            this.repaint();        }    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

南郵JAVA程式設計實驗4 線程程式設計(指標式時鐘)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.