


下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、線程程序設(shè)計一、課題內(nèi)容和要求內(nèi)容:設(shè)計和編寫一個編寫一個指針式時鐘程序,應(yīng)用線程實現(xiàn)時鐘的走 動。要求:本實驗旨在通過實驗,培養(yǎng)學(xué)生將JAVA線程的相關(guān)知識點(包括線程調(diào)度,線程同步等)有機(jī)結(jié)合并加以綜合應(yīng)用,在實驗中設(shè)計多線程程序的能 力。二、設(shè)計思路分析class Clock: 一個指針式時鐘的主類class Layout: 添加窗口和時鐘組件class ClockPai nt: 定義時鐘組件三、概要設(shè)計public class Clock exte nds JFrame public static void main( Stri ng s);class Layout exte nds
2、JFrame public Layout();class ClockPa int exte nds JPa nel impleme nts Runn able int x, y, r;int h, m, s;double rad = Math.PI / 180;public ClockPa in t(i nt x, int y, int r);public void pain "Graphics g);public void run();時鐘的繪制:運行時鐘:()IIhrcad.sk-cpdOOO)中斷一秒石車新運行s += 6秒"針走b度5 = O,ID +»
3、6h + 6m = 0*h +工 6(結(jié)束)*四、詳細(xì)設(shè)計import java.awt.*;import javax.swi ng.*;import java.util.*;public class Clock exte nds JFrame public static void main(String s) new Layout。;class Layout extends JFrame / 添加窗口和時鐘組件 public Layout() ClockPai nt cp = new ClockPai nt(20, 20, 70); add(cp);setBounds(260, 120, 2
4、00, 200);setResizable(false);this.setTitle(" 指針式時鐘 ");this.setVisible(true); class ClockPaint extends JPanel implements Runnable / 定義時鐘組件 int x, y, r;/ 時鐘的位置坐標(biāo)和半徑 int h, m, s;/ 小時,分鐘,秒 double rad = Math.PI / 180;/ 定義弧度public ClockPaint(int x, int y, int r) / 構(gòu)造函數(shù)this.x = x;this.y = y;this.
5、r = r;Calendar now = Calendar.getInstance();/ 初始化日歷對象s = now.get(Calendar.SECOND) * 6;/ 獲得初始秒轉(zhuǎn)換成度數(shù)m = now.get(Calendar.MINUTE) * 6;/ 獲得初始分鐘轉(zhuǎn)換成度數(shù)h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30 + now.get(Calendar.MINUTE) * 6 / 12;/ 獲得初始小時轉(zhuǎn)換成度數(shù)加分鐘 實現(xiàn)連貫Thread t = new Thread(this);/ 新建線程t.start();/ 啟動線程pub
6、lic void paint(Graphics g) / 繪制時鐘g.setColor(Color.BLACK);/ 畫背景g.fillRect(0, 0, r * 3, r * 3);g.setColor(Color.WHITE);/ 畫圓 g.drawOval(x, y, r * 2, r * 2);int d = 0;/ 每 6度畫一個小點int x1, y1, x2, y2;for (int i = 0; i < 60; i+) x1 = (int) (r - 2) * Math.sin(rad * d); y1 = (int) (r - 2) * Math.cos(rad *
7、d); g.drawString(".", x + r + x1 - 1, x + r - y1 + 1); d += 6;d = 30;/ 從 30 度開始每 30度畫一個數(shù)字和一線for (int i = 1; i <= 12; i+) x1 = (int) (r - 14) * Math.sin(rad * d);y1 = (int) (r - 14) * Math.cos(rad * d); g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);x1 = (int) (r - 6) * M
8、ath.sin(rad * d);y1 = (int) (r - 6) * Math.cos(rad * d);x2 = (int) (r - 2) * Math.sin(rad * d);y2 = (int) (r - 2) * Math.cos(rad * d);g.drawLine(x + r + x2, y + r - y2, x + r + x1, y + r - y1); d += 30;g.setColor(Color.RED);/ 按時間畫秒針x1 = (int) (0.8 * r) * Math.sin(rad * s);y1 = (int) (0.8 * r) * Math
9、.cos(rad * s); g.drawLine(x + r, y + r, x + r + x1, y + r - y1);g.setColor(Color.BLUE);/ 按時間畫分針 x1 = (int) (0.6 * r) * Math.sin(rad * m); y1 = (int) (0.6 * r) * Math.cos(rad * m);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);g.setColor(Color.YELLOW);/ 按時間畫時針 x1 = (int) (0.4 * r) * Math.sin(rad *
10、h); y1 = (int) (0.4 * r) * Math.cos(rad * h);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);g.setColor(Color.GREEN);/ 顯示時間Calendar now1 = Calendar.getInstance(); g.drawString(now1.get(Calendar.HOUR_OF_DAY) + " : " + now1.get(Calendar.MINUTE) + " : " + now1.get(Calendar.SECOND),
11、 0, 10);public void run() / 運行時鐘while (true) try Thread.sleep(1000);/ 中斷一秒后重新運行 catch (Exception ex) System.out.println(ex);s += 6;/ 秒針走 6 度if (s >= 360) / 秒針走完一分鐘后重置s = 0;m += 6;if (m = 72 | m = 144 | m = 216 | m = 288) /分針走完 5分鐘后重置h += 6;if (m >= 360) /分針走完一小時后重置m = 0;h += 6;if (h >= 360) /時針走完12小時后重置h = 0;this.repaint();重新繪制時
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 加快醫(yī)療器械臨床試驗的審批流程和管理
- 2025至2030中國甲基丙烯酸正丙酯行業(yè)產(chǎn)業(yè)運行態(tài)勢及投資規(guī)劃深度研究報告
- 產(chǎn)學(xué)研合作推動時空數(shù)據(jù)挖掘教學(xué)與研究的互動發(fā)展
- 企業(yè)參與智慧城市公共服務(wù)的激勵措施探討
- 企業(yè)級網(wǎng)絡(luò)安全事件響應(yīng)流程研究
- 探索教育游戲化從理論到實踐的綜評
- 技術(shù)工人培訓(xùn)課件
- 教育心理學(xué)在校園欺凌預(yù)防中的作用
- 影樓銷售技法培訓(xùn)課件
- 探索智能教學(xué)系統(tǒng)在職業(yè)技能培訓(xùn)中的應(yīng)用
- T/CGMA 033002-2020壓縮空氣站節(jié)能設(shè)計指南
- 南通國家級南通經(jīng)濟(jì)技術(shù)開發(fā)區(qū)公開招聘招商人員筆試歷年參考題庫附帶答案詳解
- 留疆戰(zhàn)士考試試題及答案
- 2025+CSCO前列腺癌診療指南進(jìn)展
- 義務(wù)教育物理課程標(biāo)準(zhǔn)解讀全文
- 2025年云南新華印刷五廠有限責(zé)任公司招聘筆試參考題庫含答案解析
- 2025年浙江金華市義烏市雙江湖開發(fā)集團(tuán)有限公司招聘筆試參考題庫附帶答案詳解
- 新課標(biāo)(水平三)體育與健康《籃球》大單元教學(xué)計劃及配套教案(18課時)
- GB/T 44910-2024琥珀分級
- 臨時施工出入口交通疏導(dǎo)方案
- 醫(yī)療設(shè)備器材供貨安裝、調(diào)試及售后服務(wù)方案
評論
0/150
提交評論