Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch17 專案 (1)- 賽馬 物件導向系統實務. 2015/12/52 五匹馬賽跑 _DOS 版 1/2 1.class Horse5_DOS 2.{ 3.static Horse[] h = new Horse[5]; 4.static Thread[] t = new Thread[h.length];

Similar presentations


Presentation on theme: "Ch17 專案 (1)- 賽馬 物件導向系統實務. 2015/12/52 五匹馬賽跑 _DOS 版 1/2 1.class Horse5_DOS 2.{ 3.static Horse[] h = new Horse[5]; 4.static Thread[] t = new Thread[h.length];"— Presentation transcript:

1 Ch17 專案 (1)- 賽馬 物件導向系統實務

2 2015/12/52 五匹馬賽跑 _DOS 版 1/2 1.class Horse5_DOS 2.{ 3.static Horse[] h = new Horse[5]; 4.static Thread[] t = new Thread[h.length]; 5.public static void main(String [] args) throws Exception 6.{ 7.for(int i=0; i<h.length; i++) 8.{ 9.h[i] = new Horse(); 10.t[i] = new Thread(h[i]); 11.} 12.for(int i=0; i<h.length; i++) 13.t[i].start(); 14.do 15.{ 16.Thread.sleep(10); 17.for(int i=0; i<h.length; i++) 18.System.out.print( (int) h[i].distance + "\t"); 19.System.out.println(); 20.}while( isAllAlive() );

3 2015/12/53 五匹馬賽跑 _DOS 版 2/2 21.static boolean isAllAlive()// 是否每個執行緒都還活著 22.{ 23.for(int i=0; i<h.length; i++) 24.if( t[i].isAlive() == false) 25.return false; 26.return true; 27.} 28.//Horse 類別 29.static class Horse implements Runnable 30.{ 31.double distance=0; 32.public void run() 33.{ 34.try{ 35.while(distance<100) 36.{ 37.Thread.sleep(10); 38.distance += Math.random(); 39.} 40.}catch(Exception e){} 41.} 42.} 43.}

4 2015/12/54 動畫 1/3 1./* 程式範例 */ 2.import javax.swing.*; 3.import java.awt.*; 4.import java.awt.event.*; 5.// 繼承 JFrame 類別, 實作 ActionListener 介面 6.public class Ch05_06 extends JFrame 7. implements ActionListener 8.{ private int offset = -10; 9. private Timer timer; 10. private AnimationPane [] animationPane = new AnimationPane[5]; 11. // 建構子 12. public Ch05_06() 13. { super(" 動畫功能的顯示範例 "); 14. int delay = 100; 15. timer = new Timer(delay, this); 16. timer.setInitialDelay(0); 17. Container c = getContentPane(); 18. c.setLayout(new FlowLayout()); 19. c.setBackground(Color.gray); 20. Toolkit toolkit = Toolkit.getDefaultToolkit(); 21. Image image = toolkit.getImage("sample.jpg"); 22. for(int i = 0; i < 5; i++) 23. { 24. animationPane[i] = new AnimationPane(image); 25. c.add(animationPane[i]); 26. } 27. timer.start(); 28. }

5 2015/12/55 動畫 2/3 29. // 顯示動畫的 JPanel 30. class AnimationPane extends JPanel 31. { Image image; 32. // 建構子 33. public AnimationPane(Image image) 34. { setPreferredSize(new Dimension(680, 100)); 35. setBackground(Color.lightGray); 36. this.image = image; 37. } 38. public void paintComponent(Graphics g) 39. { super.paintComponent(g); 40. int width = getWidth(); 41. int height = getHeight(); 42. // 計算圖片的尺寸 43. int imgWidth = image.getWidth(this); 44. int imgHeight = image.getHeight(this); 45. g.drawImage(image,((offset*5)%(imgWidth+width)) 46. - imgWidth, (height-imgHeight)/2, this); 47. } 48. }

6 2015/12/56 動畫 3/3 49. // 實作事件處理方法 50. public void actionPerformed(ActionEvent evt) 51. { offset++; 52. for(int i = 0; i<5; i++) 53. animationPane[i].repaint(); // 重繪 54. } 55. // 主程式 56. public static void main(String[] args) 57. { // 建立 Swing 應用程式 58. Ch05_06 app = new Ch05_06(); 59. // 關閉視窗事件, 結束程式的執行 60. app.addWindowListener(new WindowAdapter() 61. { public void windowClosing(WindowEvent evt) 62. { System.exit(0); } 63. }); 64. app.setSize(700, 550); // 設定尺寸 65. app.setVisible(true); // 顯示視窗 66. } 67.}


Download ppt "Ch17 專案 (1)- 賽馬 物件導向系統實務. 2015/12/52 五匹馬賽跑 _DOS 版 1/2 1.class Horse5_DOS 2.{ 3.static Horse[] h = new Horse[5]; 4.static Thread[] t = new Thread[h.length];"

Similar presentations


Ads by Google