Ch17 專案 (1)- 賽馬 物件導向系統實務. 2015/12/52 五匹馬賽跑 _DOS 版 1/2 1.class...

6
Ch17 專專 (1)- 專專 專專專專專專專專

Transcript of Ch17 專案 (1)- 賽馬 物件導向系統實務. 2015/12/52 五匹馬賽跑 _DOS 版 1/2 1.class...

Page 1: 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];

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

Page 2: 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];

112/04/18 2

五匹馬賽跑 _DOS 版1/2

1. class Horse5_DOS2. {3. static Horse[] h = new Horse[5];4. static Thread[] t = new Thread[h.length];5. public static void main(String [] args) throws Exception6. {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. do15. {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() );

Page 3: 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];

112/04/18 3

五匹馬賽跑 _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 Runnable30. {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. }

Page 4: 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];

112/04/18 4

動畫 1/31. /* 程式範例 */2. import javax.swing.*;3. import java.awt.*;4. import java.awt.event.*;5. // 繼承 JFrame 類別 , 實作 ActionListener 介面6. public class Ch05_06 extends JFrame7. implements ActionListener8. { 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. }

Page 5: 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];

112/04/18 5

動畫 2/3

29. // 顯示動畫的 JPanel30. class AnimationPane extends JPanel31. { 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. }

Page 6: 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];

112/04/18 6

動畫 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. }