JAVA課程設計 時鐘的設計實現_第1頁
JAVA課程設計 時鐘的設計實現_第2頁
JAVA課程設計 時鐘的設計實現_第3頁
JAVA課程設計 時鐘的設計實現_第4頁
JAVA課程設計 時鐘的設計實現_第5頁
已閱讀5頁,還剩16頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、遼 寧 工 業 大 學java程序設計 課程設計(論文)題目: 時鐘 院(系): 軟件學院 專業班級: 軟件技術091班 學 號: 888888888 學生姓名: tianxi 指導教師: tianxi 教師職稱: 助 教 起止時間: 2010.12.1至2010.12.16 程序設計專題(報告)任務及評語院(系):軟件學院 教研室:軟件教研室學 號8888888學生姓名tianxi專業班級軟件技術091程序設計(報告)題目時鐘程序設計(報告)任務程序設計專題的任務與要求:(1)掌握java編程、面向對象的基礎知識。(2)較熟練地編寫java應用程序application。(3)了解java的

2、常用標準類庫、編程技巧、異常處理。(5)聯系已學過的內容,鞏固所學的理論,增強獨立工作能力。(6)通過設計主要使學生有一個獨立編寫程序的過程,對理論學習及動手能力都有一個很大的提高。(7)通過本次設計,進一步培養學生熱愛專業的思想,同時對本專業綜合素質的提高起一個積極的推動作用。課程設計過程中,要嚴格遵守實踐環節的時間安排,聽從指導教師的指導。正確地完成上述內容,記錄實習日記,規范完整地撰寫出課程設計報告。指導教師評語及成績成績: 指導教師簽字: 2009 年 12 月 20 日目 錄第1章 課程設計的目的與要求11.1 課程設計目的11.2 課程設計的實驗環境11.3 課程設計的預備知識11

3、.4 課程設計要求1第2章 課程設計內容22.1課程設計主要內容22.2概要設計22.2.1自定義類說明22.3詳細設計32.4測試分析162.4.1程序運行情況162.4.2程序異常處理16第3章 課程設計總結17參考資料18第1章 課程設計的目的與要求1.1 課程設計目的java程序設計是計算機相關專業的必修專業基礎課程,其實踐性、應用性很強。實踐教學環節是必不可少的一個重要環節。本課程的程序設計專題實際是計算機相關專業學生學習完java程序設計課程后,進行的一次全面的綜合訓練,java程序設計的設計目的是加深對理論教學內容的理解和掌握,使學生較系統地掌握程序設計及其在網絡開發中的廣泛應用

4、,基本方法及技巧,為學生綜合運用所學知識,利用軟件工程為基礎進行軟件開發、并在實踐應用方面打下一定基礎。1.2 課程設計的實驗環境硬件要求能運行windows 9.x操作系統的微機系統。java程序設計語言及相應的集成開發環境,j2sdk和eclipse開發工具。1.3 課程設計的預備知識熟悉java語言及eclipse開發工具。1.4 課程設計要求按課程設計指導書提供的課題,要求學生在自行完成各個操作環節,并能實現且達到舉一反三的目的,完成一個項目解決一類問題。要求學生能夠全面、深入理解和熟練掌握所學內容,并能夠用其分析、設計和解答類似問題;對此能夠較好地理解和掌握,能夠進行簡單分析和判斷;

5、能編寫出具有良好風格的程序;掌握java程序設計的基本技能和面向對象的概念和方法;了解多線程、安全和網絡等編程技術。同時培養學生進行分析問題、解決問題的能力;培養學生進行設計分析、設計方法、設計操作與測試、設計過程的觀察、理解和歸納能力的提高。第2章 課程設計內容2.1課程設計主要內容我設計的時鐘有的界面良好,比較簡潔美觀,程序有很強的實用性,實現程序與電腦的時間的同步。可以顯示時鐘,也可以顯示分針秒針,并可以在相應位置調整時間。而且初始運行會自動與電腦的時間校對,一般默認為同步,但還可以自己再次調節,提高了實用性。本系統共包括1個java源文件。1、clock源文件是本程序的主函數其作用是初

6、始化棋盤。2、setcurrenttime源文件實現電腦設置時間。3、painthourpointer源文件為時針.4、paintsecondpointer源文件實現人與電腦設置秒針.5、paintminutedot源文件人與電腦設置分針.2.2概要設計2.2.1自定義類說明* * 類名: clock * * 作用: 自定義主類,對鼠標拖拽的初始界面進行聲明* * 繼承的父類: jcomponent類 * * 實現的接口: 沒有 * *表1-成員變量表表2.1 clock成員變量成員變量描述變量類型名稱時針stringhour分針stringminute秒針stringsecond時間點tex

7、tfieldtext_1表2-方法表表2 clock方法方法名功能備注setcurrenttime設置當前時間構造方法painthourpointer設置時針接口方法paintsecondpointer設置秒針接口方法paintminutedot設置分針接口方法actionperformed事件處理run程序運行2.3詳細設計import java.awt.*; import java.awt.geom.ellipse2d; import java.awt.geom.generalpath; import java.awt.geom.line2d; import java.awt.geom.r

8、ectangle2d; import java.util.calendar; import java.util.date; import javax.swing.borderfactory; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.uimanager; public class clock extends jcomponent private static final color integral_color = new color(0, 128, 128); private in

9、t radius; private calendar currenttime = calendar.getinstance(); private double s = 0.03; public clock(int radius) this.radius = radius; public void setcurrenttime(date time) /設置當前時間 this.currenttime.settime(time); public void setcurrenttime(long millis) this.currenttime.settimeinmillis(millis); pub

10、lic dimension getpreferredsize() insets insets = getinsets(); int r = (int) (radius = -1 ? 0 : radius*(1+s)+1; return new dimension(r * 2 + insets.left + insets.right,r * 2 + insets.top + insets.bottom); /返回一個指定寬、高的dimension protected void paintcomponent(graphics g) super.paintcomponent(g); graphics

11、2d g2d = (graphics2d) g; g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); insets insets = getinsets(); int wid = getwidth() - insets.left - insets.right; int hei = getheight() - insets.top - insets.bottom; int r = (int) (math.min(wid, hei) / 2 / (1+s); g2d.tr

12、anslate(insets.left + r * (1+s), insets.top + r * (1+s); g2d.scale(1, -1); for (int i = 0; i 60; i+) int angle = 90 - i * 6; double pos = calcpos(r, angle); paintminutedot(r, g2d, pos0, pos1, i % 5 = 0); painthourpointer(r, g2d); paintminutepointer(r, g2d); paintsecondpointer(r, g2d); paintcenterpoi

13、nt(g2d); g2d.scale(1, -1); g2d.translate(-insets.left - r * (1+s), -insets.top - r * (1+s); private void paintcenterpoint(graphics2d g2d) g2d.setcolor(color.blue); rectangle2d rect = new rectangle2d.double(-2, -2, 4, 4); g2d.fill(rect); private void paintminutepointer(int r, graphics2d g2d) int minu

14、te = currenttime.get(calendar.minute); int second = currenttime.get(calendar.second); double angle = 90 - (minute + second / 60.0) * 6; shape pointershape = createpointershape(r * 0.8, r * 0.04, r * 0.08, angle); g2d.setcolor(color.light_gray); g2d.fill(pointershape); g2d.setcolor(color.dark_gray);

15、g2d.draw(pointershape); private void painthourpointer(int r, graphics2d g2d) int hour = currenttime.get(calendar.hour); int minute = currenttime.get(calendar.minute); int second = currenttime.get(calendar.second); double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30; shape pointershape

16、= createpointershape(r * 0.6, r * 0.06, r * 0.1, angle); g2d.setcolor(color.light_gray); g2d.fill(pointershape); g2d.setcolor(color.dark_gray); g2d.draw(pointershape); private shape createpointershape(double r1, double r2, double r3, double angle) generalpath gp = new generalpath(); double pos = cal

17、cpos(r1, angle); double pos1 = calcpos(r2, angle + 90); gp.append(new line2d.double(pos0, pos1, pos10, pos11), true); double pos2 = calcpos(r3, angle + 180); gp.lineto(float)pos20, (float)pos21); double pos3 = calcpos(r2, angle + 270); gp.lineto(float)pos30, (float)pos31); gp.closepath(); return gp;

18、 private void paintsecondpointer(int r, graphics2d g2d) g2d.setcolor(color.black); int second = currenttime.get(calendar.second); int angle = 90 - second * 6; double pos = calcpos(r * 0.9, angle); double pos1 = calcpos(r * 0.2, angle + 180); line2d line = new line2d.double(pos10, pos11, pos0, pos1);

19、 g2d.draw(line); private void paintminutedot(int r, graphics2d g2d, double x, double y, boolean flag) g2d.setcolor(flag ? color.red : color.black); if (flag) /rectangle2d rect = new rectangle2d.double( ellipse2d rect = new ellipse2d.double( x - r * s, y - r * s, r * s * 2, r * s * 2); g2d.fill(rect)

20、; else /rectangle2d rect = new rectangle2d.double( ellipse2d rect = new ellipse2d.double( x - r * 0.02, y - r * 0.02, r * 0.04, r * 0.04); g2d.fill(rect); private double calcpos(double r, double angle) double radian = math.toradians(angle); double x = r * math.cos(radian); double y = r * math.sin(ra

21、dian); return new double x, y; public static void main(string args) try uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname(); catch (exception e) e.printstacktrace(); final clock clock = new clock(50); clock.setborder(borderfactory.createemptyborder(10, 10, 10, 10); jframe f = new jfra

22、me( 軟件081班 071404011 孫慶賀 ); /f.setbounds(380,200,500,600); f.getcontentpane().add(clock, borderlayout.center); f.pack(); f.setlocationrelativeto(null); f.setdefaultcloseoperation(jframe.exit_on_close); f.setvisible(true); new thread() public void run() while (true) try thread.sleep(1000); catch (int

23、erruptedexception ex) ex.printstacktrace(); clock.setcurrenttime(system.currenttimemillis(); clock.repaint(); .start(); int y1 = (int)(r - 10) * math.cos(rad * s);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 * m);y1 = (int)(r - r /

24、 2.5) * math.cos(rad * m);g.drawline(x + r, y + r, x + r + x1, y + r - y1);/時針g.setcolor(color.cyan);x1 = (int)(r - r / 1.5) * math.sin(rad * h);y1 = (int)(r - r / 1.5) * math.cos(rad * h);g.drawline(x + r, y + r, x + r + x1, y + r - y1);/數字g.setcolor(color.yellow);int d = 29;for (int i = 1; i = 12;

25、 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 = 360) s = 0;m+=6;if (m = 72 | m = 144 | m = 216 | m = 288) h+=6;if (m = 360) m = 0;h+=6;if (h =360) h = 0;this.repaint();int x, y, r

26、;int h, m, s;/小時,分鐘,秒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();s = now.get(calendar.second) * 6;/獲得秒轉換成度數m = now.get(calendar.minute) * 6;/獲得分鐘h = (now.get(calendar.hour_of_day) - 12) * 30 + now.get(calen

27、dar.minute) / 12 * 6;/獲得小時thread t = new thread(this);t.start();public void paint(graphics g) /清屏super.paint(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);/秒針g.setcolor(color.red);int x1 = (int)(r - 10) * math.sin(rad * s);/定義myti

28、mer類 class mytimer1 extends jframe static int count=0; /判斷是否重定義了時間 /構造函數 public mytimer1() /定義窗口大小 setsize(320, 200); /定義窗口標題 settitle(測試自定義時鐘類!); container c = getcontentpane(); / new clockcanvas(北京時間, gmt+8) c.add(new clockcanvas(北京時間, gmt+8); /定義接口 interface timerlistener1 void timeelapsed(timer1

29、 t); class timer1 extends thread /類timer1 private timerlistener1 target; private int interval; public timer1(int i, timerlistener1 t) target = t; interval = i; setdaemon(true); public void run() try while (!interrupted() sleep(interval); target.timeelapsed(this); catch(interruptedexception e) class

30、clockcanvas extends jpanel /clockcanvas implements timerlistener1 static int seconds = 0; private string city; private gregoriancalendar calendar; /構造函數 public clockcanvas(string c, string tz) city = c; calendar = new gregoriancalendar(timezone.gettimezone(tz); timer1 t = new timer1(1000, this); t.s

31、tart(); setsize(180, 180); /繪制鐘面 public void paintcomponent(graphics g) super.paintcomponent(g); g.drawoval(100, 5, 120, 120); g.drawoval(101, 6, 118, 118); /分離時間 double hourangle = 2 * math.pi * (seconds - 3 * 60 * 60) / (12 * 60 * 60); double minuteangle = 2 * math.pi * (seconds - 15 * 60) / (60 *

32、 60); double secondangle = 2 * math.pi * (seconds - 15) / 60; public void timeelapsed(timer1 t) calendar.settime(new date(); if(mytimer1.count=1) int a=1; seconds=mythour*60*60+mytminute*60+mytsecond; seconds+=a;/a為秒自加 repaint(); else seconds = calendar.get(calendar.hour) * 6

33、0 * 60 + calendar.get(calendar.minute) * 60 + calendar.get(calendar.second); repaint(); /定義時鐘類 class mytimer implements timerlistener /定義時鐘類的屬性 static int inthour,intminute,intsecond; /構造函數 public mytimer() setcurrenttimeassystemtime(); timer t = new timer(1000, this); /實例timer類,里面有run方法 t.start();

34、/顯示當前時間 public void displaycurrenttime() joptionpane.showmessagedialog(null,inthour+:+intminute+:+intsecond); /設定當前時間 public void setcurrenttime() /從對話框輸入時,分秒 string strtemp=joptionpane.showinputdialog(null,請輸入當前小時(24小時制):); int ihour=integer.parseint(strtemp); strtemp=joptionpane.showinputdialog(nu

35、ll,請輸入當前分:); int iminute=integer.parseint(strtemp); strtemp=joptionpane.showinputdialog(null,請輸入當前秒:); int isecond=integer.parseint(strtemp); /設定當前時間為對話框輸入的時間 if(ihour=0&ihour24) inthour=ihour; transform(angle);g.setcolor(color);/設定當前時間為系統時間,構造函數調用 public void setcurrenttimeassystemtime() /定義date類的一個對象,用來獲取系統時間 date timecurrent=new date(); catch(interruptedexception e) 2.4測試分析2.4.1程序運行情況當程序正常運行的時候,它能清晰的顯示時鐘界面。默認為與系統時間同步,如果出現錯誤,就會進行歸零。運行界面如圖2.1:圖2.1運行界面2.4.2程序異常處理當線程在活動之前或活動期間處于正在等待、休眠或占用狀態且該線程被中斷時,拋出該異常。有時候,一種方法可能希

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論