JAVA課程設計時鐘的設計實現(xiàn)_第1頁
JAVA課程設計時鐘的設計實現(xiàn)_第2頁
JAVA課程設計時鐘的設計實現(xiàn)_第3頁
JAVA課程設計時鐘的設計實現(xiàn)_第4頁
JAVA課程設計時鐘的設計實現(xiàn)_第5頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、 . . . JAVA程序設計 課程設計(論文)目 錄第1章課程設計的目的與要求11.1 課程設計目的11.2 課程設計的實驗環(huán)境11.3 課程設計的預備知識11.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程序設計是計算機相關專業(yè)的必修專業(yè)基礎課程,其實踐性、應用性很強。實踐教學環(huán)節(jié)是必不可少的一個重要環(huán)節(jié)。本課程的程序設計專題實際是計算機相關專業(yè)學生學習完JAVA

2、程序設計課程后,進行的一次全面的綜合訓練,JAVA程序設計的設計目的是加深對理論教學容的理解和掌握,使學生較系統(tǒng)地掌握程序設計與其在網(wǎng)絡開發(fā)中的廣泛應用,基本方法與技巧,為學生綜合運用所學知識,利用軟件工程為基礎進行軟件開發(fā)、并在實踐應用方面打下一定基礎。1.2 課程設計的實驗環(huán)境硬件要求能運行Windows 9.X操作系統(tǒng)的微機系統(tǒng)。JAVA程序設計語言與相應的集成開發(fā)環(huán)境,J2SDK和ECLIPSE開發(fā)工具。1.3 課程設計的預備知識熟悉JAVA語言與ECLIPSE開發(fā)工具。1.4 課程設計要求按課程設計指導書提供的課題,要求學生在自行完成各個操作環(huán)節(jié),并能實現(xiàn)且達到舉一反三的目的,完成一

3、個項目解決一類問題。要求學生能夠全面、深入理解和熟練掌握所學容,并能夠用其分析、設計和解答類似問題;對此能夠較好地理解和掌握,能夠進行簡單分析和判斷;能編寫出具有良好風格的程序;掌握JAVA程序設計的基本技能和面向?qū)ο蟮母拍詈头椒?;了解多線程、安全和網(wǎng)絡等編程技術。同時培養(yǎng)學生進行分析問題、解決問題的能力;培養(yǎng)學生進行設計分析、設計方法、設計操作與測試、設計過程的觀察、理解和歸納能力的提高。第2章 課程設計容2.1課程設計主要容我設計的時鐘有的界面良好,比較簡潔美觀,程序有很強的實用性,實現(xiàn)程序與電腦的時間的同步。可以顯示時鐘,也可以顯示分針秒針,并可以在相應位置調(diào)整時間。而且初始運行會自動與

4、電腦的時間校對,一般默認為同步,但還可以自己再次調(diào)節(jié),提高了實用性。本系統(tǒng)共包括1個java源文件。1、Clock源文件是本程序的主函數(shù)其作用是初始化棋盤。2、setCurrentTime源文件實現(xiàn)電腦設置時間。3、paintHourPointer源文件為時針.4、paintSecondPointer源文件實現(xiàn)人與電腦設置秒針.5、paintMinuteDot源文件人與電腦設置分針.2.2概要設計2.2.1自定義類說明* * 類名: Clock * * 作用: 自定義主類,對鼠標拖拽的初始界面進行聲明* * 繼承的父類: JComponent類 * * 實現(xiàn)的接口: 沒有 * *表1-成員變量

5、表表2.1 Clock成員變量成員變量描述變量類型名稱時針StringHour分針StringMinute秒針StringSecond時間點TextFieldtext_1表2-方法表表2 Clock方法方法名功能備注setCurrentTime設置當前時間構造方法paintHourPointer設置時針接口方法paintSecondPointer設置秒針接口方法paintMinuteDot設置分針接口方法actionPerformed事件處理run程序運行2.3詳細設計import java.awt.*; import java.awt.geom.Ellipse2D; import java.

6、awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; 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 priva

7、te static final Color INTEGRAL_COLOR = new Color(0, 128, 128); private int 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

8、setCurrentTime(long millis) this.currentTime.setTimeInMillis(millis); public 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 pr

9、otected void paintComponent(Graphics g) super.paintComponent(g); Graphics2D 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.t

10、op - insets.bottom; int r = (int) (Math.min(wid, hei) / 2 / (1+s); g2d.translate(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

11、); paintMinutePointer(r, g2d); paintSecondPointer(r, g2d); paintCenterPoint(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.fi

12、ll(rect); private void paintMinutePointer(int r, Graphics2D g2d) int minute = 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(C

13、olor.LIGHT_GRAY); g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY); 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

14、= 90 - (hour + minute / 60.0 + second / 3600.0) * 30; Shape pointerShape = 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, doub

15、le r3, double angle) GeneralPath gp = new GeneralPath(); double pos = calcPos(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

16、 + 270); gp.lineTo(float)pos30, (float)pos31); gp.closePath(); return gp; 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,

17、 angle + 180); Line2D line = new Line2D.Double(pos10, pos11, pos0, pos1); 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 Elli

18、pse2D.Double( x - r * s, y - r * s, r * s * 2, r * s * 2); g2d.fill(rect); 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.toRa

19、dians(angle); double x = r * Math.cos(radian); double y = r * Math.sin(radian); 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.setBo

20、rder(BorderFactory.createEmptyBorder(10, 10, 10, 10); JFrame f = new JFrame( 軟件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 Th

21、read() public void run() while (true) try Thread.sleep(1000); catch (InterruptedException 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(Co

22、lor.BLUE);x1 = (int)(r - r / 2.5) * Math.sin(rad * m);y1 = (int)(r - r / 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

23、+ r - y1);/數(shù)字g.setColor(Color.YELLOW);int d = 29;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 = 360) s = 0;m+=6;if (m = 72 | m = 144 | m = 216 | m = 288) h

24、+=6;if (m = 360) m = 0;h+=6;if (h =360) h = 0;this.repaint();int x, y, r;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;/獲得秒轉(zhuǎn)換成度數(shù)m = now.get(Calendar.MINUTE

25、) * 6;/獲得分鐘h = (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.BLACK);g.fillRect(0, 0, r * 3, r * 3);/畫圓g.setColor(Color.WHITE);g.drawOval(x, y, r * 2, r * 2);/秒針

26、g.setColor(Color.RED);int x1 = (int)(r - 10) * Math.sin(rad * s);/定義MyTimer類 class MyTimer1 extends JFrame static int count=0; /判斷是否重定義了時間 /構造函數(shù) public MyTimer1() /定義窗口大小 setSize(320, 200); /定義窗口標題 setTitle(測試自定義時鐘類!); Container c = getContentPane(); / new ClockCanvas(時間, GMT+8) c.add(new ClockCanva

27、s(時間, GMT+8); /定義接口 interface TimerListener1 void timeElapsed(Timer1 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(inter

28、val); target.timeElapsed(this); catch(InterruptedException e) class ClockCanvas extends JPanel /clockcanvas implements TimerListener1 static int seconds = 0; private String city; private GregorianCalendar calendar; /構造函數(shù) public ClockCanvas(String c, String tz) city = c; calendar = new GregorianCalen

29、dar(TimeZone.getTimeZone(tz); Timer1 t = new Timer1(1000, this); t.start(); 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 *

30、 60); double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 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

31、+=a;/a為秒自加 repaint(); else seconds = calendar.get(Calendar.HOUR) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND); repaint(); /定義時鐘類 class MyTimer implements TimerListener /定義時鐘類的屬性 static int intHour,intMinute,intSecond; /構造函數(shù) public MyTimer() setCurrentTimeAsSystemTim

32、e(); Timer t = new Timer(1000, this); /實例Timer類,里面有run方法 t.start(); /顯示當前時間 public void displayCurrentTime() JOptionPane.showMessageDialog(null,intHour+:+intMinute+:+intSecond); /設定當前時間 public void setCurrentTime() /從對話框輸入時,分秒 String strTemp=JOptionPane.showInputDialog(null,請輸入當前小時(24小時制):); int iHo

33、ur=Integer.parseInt(strTemp); strTemp=JOptionPane.showInputDialog(null,請輸入當前分:); 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);/設定

34、當前時間為系統(tǒng)時間,構造函數(shù)調(diào)用 public void setCurrentTimeAsSystemTime() /定義Date類的一個對象,用來獲取系統(tǒng)時間 Date timeCurrent=new Date(); catch(InterruptedException e) 2.4測試分析2.4.1程序運行情況當程序正常運行的時候,它能清晰的顯示時鐘界面。默認為與系統(tǒng)時間同步,如果出現(xiàn)錯誤,就會進行歸零。運行界面如圖2.1:圖2.1運行界面2.4.2程序異常處理當線程在活動之前或活動期間處于正在等待、休眠或占用狀態(tài)且該線程被中斷時,拋出該異常。有時候,一種方法可能希望測試當前線程是否已被中斷,如果已被中斷,則立即拋出此異常。利用trycatch來捕獲出現(xiàn)的異

溫馨提示

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

評論

0/150

提交評論