#眉標=Java
#副標=智慧型樂高機器人與Java程式開發(8)
#大標=設計NXT圖形使用者介面程式
#作者=文/圖	何致億
#引言= 



==<box>===========
程式1:ChooseProgram.java
package basic;
01. import icommand.nxtcomm.*;
02. import icommand.platform.nxt.*;
03. import java.io.*;
04. 
05. public class ChooseProgram {
06.   public static void main(String[] args) throws Exception {
07.     char choice;
08.     NXTCommand.setVerify(true);
09.     InputStreamReader ir = new InputStreamReader(System.in);
10.     
11.     System.out.println("\n=========   程式開始   ===========");
12.     System.out.println("A: 執行 NXT 的 ShowImage.rxe 程式");
13.     System.out.println("B: 播放 NXT 的 Hello.rso 聲音檔");
14.     System.out.println("C: 讓 A 馬達旋轉 2 秒");
15.     System.out.println("D: 結束本程式");
16.     System.out.print("\n請選擇 A ~ D : ");
17.     choice = (char)ir.read();
18.     System.out.println("\n你選擇了: " + choice);
19.     
20.     switch (choice) {
21.       case 'A':
22.         NXTCommand.startProgram("ShowImage.rxe");
23.         break;
24.       case 'B':
25.         NXTCommand.playSoundFile("Hello.rso", false);
26.         break;
27.       case 'C':
28.         Motor.A.forward();
29.         Thread.sleep(2000);
30.         Motor.A.stop();
31.         break;
32.       case 'D':
33.         break;
34.       default:
35.         break;
36.     }
37.     
38.     System.out.println("\n=========   程式結束   ===========");
39.     NXTCommand.close();
40.   }
41. }
==<end>==============





==<box>===========
程式2:ChooseProgramGUI.java
42. package gui;
43. import java.awt.*;
44. import javax.swing.*;
45. import icommand.nxtcomm.NXTCommand;
46. import icommand.platform.nxt.*;
47. 
48. public class ChooseProgramGUI extends JFrame {
49.   private static final long serialVersionUID = 1L;
50.   private JPanel jContentPane = null;
51.   private JButton jBtn_ShowImage = null;
52.   private JButton jBtn_PlaySoundFile = null;
53.   private JButton jBtn_TurnOnMotor = null;
54.   private JButton jBtn_Exit = null;
55. 
56.   private JButton getJBtn_ShowImage() {
57.     if (jBtn_ShowImage == null) {
58.       jBtn_ShowImage = new JButton();
59.       jBtn_ShowImage
60.       .setFont(new Font("Dialog", Font.PLAIN, 18));
61.       jBtn_ShowImage.setLocation(new Point(51, 30));
62.       jBtn_ShowImage.setSize(new Dimension(195, 45));
63.       jBtn_ShowImage.setText("執行ShowImage.rxe");
64.       jBtn_ShowImage
65.       .addActionListener(new java.awt.event.ActionListener() {
66.         public void actionPerformed(
67.         java.awt.event.ActionEvent e) {
68.           NXTCommand.startProgram("ShowImage.rxe");
69.         }
70.       });
71.     }
72.     return jBtn_ShowImage;
73.   }
74. 
75.   private JButton getJBtn_PlaySoundFile() {
76.     if (jBtn_PlaySoundFile == null) {
77.       jBtn_PlaySoundFile = new JButton();
78.       jBtn_PlaySoundFile.setFont(new Font("Dialog",
79.       Font.PLAIN, 18));
80.       jBtn_PlaySoundFile.setLocation(new Point(285, 30));
81.       jBtn_PlaySoundFile.setSize(new Dimension(195, 45));
82.       jBtn_PlaySoundFile.setText("播放Hello.rso");
83.       jBtn_PlaySoundFile
84.       .addActionListener(new java.awt.event.ActionListener() {
85.         public void actionPerformed(
86.         java.awt.event.ActionEvent e) {
87.           NXTCommand.playSoundFile("Hello.rso", false);
88.         }
89.       });
90.     }
91.     return jBtn_PlaySoundFile;
92.   }
93.  
94.   private JButton getJBtn_TurnOnMotor() {
95.     if (jBtn_TurnOnMotor == null) {
96.       jBtn_TurnOnMotor = new JButton();
97.       jBtn_TurnOnMotor.setFont(new Font("Dialog", Font.PLAIN,
98.       18));
99.       jBtn_TurnOnMotor.setLocation(new Point(51, 100));
100.       jBtn_TurnOnMotor.setSize(new Dimension(195, 45));
101.       jBtn_TurnOnMotor.setText("讓A馬達轉動2秒");
102.       jBtn_TurnOnMotor
103.       .addActionListener(new java.awt.event.ActionListener() {
104.         public void actionPerformed(
105.         java.awt.event.ActionEvent e) {
106.           Motor.A.forward();
107.           try {
108.           Thread.sleep(2000);
109.           } catch (InterruptedException ie) {
110.             ie.printStackTrace();
111.           }
112.           Motor.A.stop();
113.         }
114.       });
115.     }
116.     return jBtn_TurnOnMotor;
117.   }
118. 
119.   private JButton getJBtn_Exit() {
120.     if (jBtn_Exit == null) {
121.       jBtn_Exit = new JButton();
122.       jBtn_Exit.setFont(new Font("Dialog", Font.PLAIN, 18));
123.       jBtn_Exit.setLocation(new Point(285, 100));
124.       jBtn_Exit.setSize(new Dimension(195, 45));
125.       jBtn_Exit.setBackground(Color.lightGray);
126.       jBtn_Exit.setText("結束本程式");
127.       jBtn_Exit
128.       .addActionListener(new java.awt.event.ActionListener() {
129.         public void actionPerformed(
130.         java.awt.event.ActionEvent e) {
131.           NXTCommand.close();
132.           System.exit(0);
133.         }
134.       });
135.     }
136.     return jBtn_Exit;
137.   }
138. 
139.   public static void main(String[] args) {
140.     SwingUtilities.invokeLater(new Runnable() {
141.       public void run() {
142.     ChooseProgramGUI thisClass = new ChooseProgramGUI();
143.     thisClass
144.         .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
145.     thisClass.setVisible(true);
146.       }
147.     });
148.   }
149. 
150.   public ChooseProgramGUI() {
151.     super();
152.     initialize();
153.   }
154. 
155.   private void initialize() {
156.     this.setSize(541, 200);
157.     this.setContentPane(getJContentPane());
158.     this.setTitle("改寫第14章的ChooseProgram範例程式");
159.     NXTCommand.setVerify(true);
160.   }
161. 
162.   private JPanel getJContentPane() {
163.     if (jContentPane == null) {
164.       jContentPane = new JPanel();
165.       jContentPane.setLayout(null);
166.       jContentPane.add(getJBtn_ShowImage(), null);
167.       jContentPane.add(getJBtn_PlaySoundFile(), null);
168.       jContentPane.add(getJBtn_TurnOnMotor(), null);
169.       jContentPane.add(getJBtn_Exit(), null);
170.     }
171.     return jContentPane;
172.   }
173. }
==<end>==============





====<反灰>===========
private int speedA = 0;
private int speedB = 0;
private int speedC = 0;
==<end>==============





====<反灰>===========
import icommand.nxtcomm.NXTCommand;
import icommand.platform.nxt.*;
==<end>==============





=====<反灰>===========
speedA = jSldr_A.getValue();
speedB = jSldr_B.getValue();
speedC = jSldr_C.getValue();
jTF_A.setText(String.valueOf(speedA));
jTF_B.setText(String.valueOf(speedB));
jTF_C.setText(String.valueOf(speedC));
NXTCommand.setVerify(true);
==<end>==============




==<box>===========
程式3:ControlMotors.java
01. package gui;
02. import java.awt.*;
03. import javax.swing.*;
04. import java.awt.event.*;
05. import icommand.nxtcomm.NXTCommand;
06. import icommand.platform.nxt.*;
07. 
08. public class ControlMotors extends JFrame {
09.   private static final long serialVersionUID = 1L;
10.   private JPanel jContentPane = null;
11.   private JLabel jLabel_A = null;
12.   private JLabel jLabel_B = null;
13.   private JLabel jLabel_C = null;
14.   private JSlider jSldr_A = null;
15.   private JSlider jSldr_B = null;
16.   private JSlider jSldr_C = null;
17.   private JButton jBtn_A_F = null; //呼叫Motor.A.forward()
18.   private JButton jBtn_A_B = null; //呼叫Motor.A.backward()
19.   private JButton jBtn_A_S = null; //呼叫Motor.A.stop()
20.   private JButton jBtn_B_F = null; //呼叫Motor.B.forward()
21.   private JButton jBtn_B_B = null; //呼叫Motor.B.backward()
22.   private JButton jBtn_B_S = null; //呼叫Motor.B.stop()
23.   private JButton jBtn_C_F = null; //呼叫Motor.C.forward()
24.   private JButton jBtn_C_B = null; //呼叫Motor.C.backward()
25.   private JButton jBtn_C_S = null; //呼叫Motor.C.stop()
26.   private JButton jBtn_Exit = null;
27.   private JTextField jTF_A = null;
28.   private JTextField jTF_B = null;
29.   private JTextField jTF_C = null;
30.   private int speedA = 0;
31.   private int speedB = 0;
32.   private int speedC = 0;
33. 
34.   private JSlider getJSldr_A() {
35.     if (jSldr_A == null) {
36.       jSldr_A = new JSlider();
37.       jSldr_A.setLocation(new Point(170, 35));
38.       jSldr_A.setSize(new Dimension(100, 30));
39.       jSldr_A.addChangeListener(
40.           new javax.swing.event.ChangeListener() {
41.     public void stateChanged(
42.         javax.swing.event.ChangeEvent e) {
43.       speedA = jSldr_A.getValue();
44.       jTF_A.setText(String.valueOf(speedA));
45.     }
46.       });
47.     }
48.     return jSldr_A;
49.   }
50. 
51.   private JButton getJBtn_A_F() {
52.     if (jBtn_A_F == null) {
53.       jBtn_A_F = new JButton();
54.       jBtn_A_F.setText("A馬達正轉");
55.       jBtn_A_F.setSize(new Dimension(120, 35));
56.       jBtn_A_F.setFont(new Font("Dialog", Font.PLAIN, 18));
57.       jBtn_A_F.setLocation(new Point(294, 30));
58.       jBtn_A_F.addActionListener(
59.       new java.awt.event.ActionListener() {
60.         public void actionPerformed(
61.             java.awt.event.ActionEvent e) {
62.           Motor.A.setSpeed(speedA);
63.           Motor.A.forward();
64.     }
65.       });
66.     }
67.     return jBtn_A_F;
68.   }
69. 
70.   private JButton getJBtn_A_B() {
71.     if (jBtn_A_B == null) {
72.       jBtn_A_B = new JButton();
73.       jBtn_A_B.setText("A馬達反轉");
74.       jBtn_A_B.setLocation(new Point(430, 30));
75.       jBtn_A_B.setSize(new Dimension(120, 35));
76.       jBtn_A_B.setFont(new Font("Dialog", Font.PLAIN, 18));
77.       jBtn_A_B.setMnemonic(KeyEvent.VK_UNDEFINED);
78.       jBtn_A_B.addActionListener(
79.       new java.awt.event.ActionListener() {
80.         public void actionPerformed(
81.             java.awt.event.ActionEvent e) {
82.           Motor.A.setSpeed(speedA);
83.           Motor.A.backward();
84.     }
85.       });
86.     }
87.     return jBtn_A_B;
88.   }
89. 
90.   private JButton getJBtn_A_S() {
91.     if (jBtn_A_S == null) {
92.       jBtn_A_S = new JButton();
93.       jBtn_A_S.setLocation(new Point(568, 30));
94.       jBtn_A_S.setText("A馬達停止");
95.       jBtn_A_S.setFont(new Font("Dialog", Font.PLAIN, 18));
96.       jBtn_A_S.setSize(new Dimension(120, 35));
97.       jBtn_A_S.addActionListener(
98.       new java.awt.event.ActionListener() {
99.     public void actionPerformed(
100.         java.awt.event.ActionEvent e) {
101.       Motor.A.stop();
102.     }
103.       });
104.     }
105.     return jBtn_A_S;
106.   }
107. 
108.   private JSlider getJSldr_B() {
109.     if (jSldr_B == null) {
110.       jSldr_B = new JSlider();
111.       jSldr_B.setName("");
112.       jSldr_B.setSize(new Dimension(100, 30));
113.       jSldr_B.setLocation(new Point(170, 102));
114.       jSldr_B.addChangeListener(
115.       new javax.swing.event.ChangeListener() {
116.         public void stateChanged(
117.             javax.swing.event.ChangeEvent e) {
118.           speedB = jSldr_B.getValue();
119.           jTF_B.setText(String.valueOf(speedB));
120.     }
121.       });
122.     }
123.     return jSldr_B;
124.   }
125. 
126.   private JButton getJBtn_B_F() {
127.     if (jBtn_B_F == null) {
128.       jBtn_B_F = new JButton();
129.       jBtn_B_F.setText("B馬達正轉");
130.       jBtn_B_F.setSize(new Dimension(120, 35));
131.       jBtn_B_F.setFont(new Font("Dialog", Font.PLAIN, 18));
132.       jBtn_B_F.setLocation(new Point(293, 101));
133.       jBtn_B_F.addActionListener(
134.       new java.awt.event.ActionListener() {
135.         public void actionPerformed(
136.             java.awt.event.ActionEvent e) {
137.           Motor.B.setSpeed(speedB);
138.           Motor.B.forward();
139.     }
140.       });
141.     }
142.     return jBtn_B_F;
143.   }
144. 
145.   private JButton getJBtn_B_B() {
146.     if (jBtn_B_B == null) {
147.       jBtn_B_B = new JButton();
148.       jBtn_B_B.setText("B馬達反轉");
149.       jBtn_B_B.setSize(new Dimension(120, 35));
150.       jBtn_B_B.setFont(new Font("Dialog", Font.PLAIN, 18));
151.       jBtn_B_B.setLocation(new Point(431, 100));
152.       jBtn_B_B.addActionListener(
153.       new java.awt.event.ActionListener() {
154.         public void actionPerformed(
155.             java.awt.event.ActionEvent e) {
156.           Motor.B.setSpeed(speedB);
157.           Motor.B.backward();
158.     }
159.       });
160.     }
161.     return jBtn_B_B;
162.   }
163. 
164.   private JButton getJBtn_B_S() {
165.     if (jBtn_B_S == null) {
166.       jBtn_B_S = new JButton();
167.       jBtn_B_S.setFont(new Font("Dialog", Font.PLAIN, 18));
168.       jBtn_B_S.setLocation(new Point(568, 100));
169.       jBtn_B_S.setSize(new Dimension(120, 35));
170.       jBtn_B_S.setText("B馬達停止");
171.       jBtn_B_S.addActionListener(
172.       new java.awt.event.ActionListener() {
173.         public void actionPerformed(
174.            java.awt.event.ActionEvent e) {
175.           Motor.B.stop();
176.     }
177.       });
178.     }
179.     return jBtn_B_S;
180.   }
181. 
182.   private JSlider getJSldr_C() {
183.     if (jSldr_C == null) {
184.       jSldr_C = new JSlider();
185.       jSldr_C.setLocation(new Point(171, 167));
186.       jSldr_C.setSize(new Dimension(100, 30));
187.       jSldr_C.addChangeListener(
188.       new javax.swing.event.ChangeListener() {
189.         public void stateChanged(
190.             javax.swing.event.ChangeEvent e) {
191.           speedC = jSldr_C.getValue();
192.           jTF_C.setText(String.valueOf(speedC));
193.     }
194.       });
195.     }
196.     return jSldr_C;
197.   }
198. 
199.   private JButton getJBtn_C_F() {
200.     if (jBtn_C_F == null) {
201.       jBtn_C_F = new JButton();
202.       jBtn_C_F.setText("C馬達正轉");
203.       jBtn_C_F.setSize(new Dimension(120, 35));
204.       jBtn_C_F.setFont(new Font("Dialog", Font.PLAIN, 18));
205.       jBtn_C_F.setLocation(new Point(293, 165));
206.       jBtn_C_F.addActionListener(
207.       new java.awt.event.ActionListener() {
208.         public void actionPerformed(
209.             java.awt.event.ActionEvent e) {
210.           Motor.C.setSpeed(speedC);
211.           Motor.C.forward();
212.     }
213.       });
214.     }
215.     return jBtn_C_F;
216.   }
217. 
218.   private JButton getJBtn_C_B() {
219.     if (jBtn_C_B == null) {
220.       jBtn_C_B = new JButton();
221.       jBtn_C_B.setText("C馬達反轉");
222.       jBtn_C_B.setSize(new Dimension(120, 35));
223.       jBtn_C_B.setFont(new Font("Dialog", Font.PLAIN, 18));
224.       jBtn_C_B.setLocation(new Point(431, 164));
225.       jBtn_C_B.addActionListener(
226.       new java.awt.event.ActionListener() {
227.         public void actionPerformed(
228.            java.awt.event.ActionEvent e) {
229.           Motor.C.setSpeed(speedC);
230.           Motor.C.backward();
231.     }
232.       });
233.     }
234.     return jBtn_C_B;
235.   }
236. 
237.   private JButton getJBtn_C_S() {
238.     if (jBtn_C_S == null) {
239.       jBtn_C_S = new JButton();
240.       jBtn_C_S.setFont(new Font("Dialog", Font.PLAIN, 18));
241.       jBtn_C_S.setLocation(new Point(568, 164));
242.       jBtn_C_S.setSize(new Dimension(120, 35));
243.       jBtn_C_S.setText("C馬達停止");
244.       jBtn_C_S.addActionListener(
245.       new java.awt.event.ActionListener() {
246.         public void actionPerformed(
247.             java.awt.event.ActionEvent e) {
248.           Motor.C.stop();
249.     }
250.       });
251.     }
252.     return jBtn_C_S;
253.   }
254. 
255.   private JTextField getJTF_A() {
256.     if (jTF_A == null) {
257.       jTF_A = new JTextField();
258.       jTF_A.setLocation(new Point(123, 35));
259.       jTF_A.setHorizontalAlignment(JTextField.RIGHT);
260.       jTF_A.setEnabled(false);
261.       jTF_A.setFont(new Font("Dialog", Font.PLAIN, 18));
262.       jTF_A.setSize(new Dimension(40, 30));
263.     }
264.     return jTF_A;
265.   }
266. 
267.   private JTextField getJTF_B() {
268.     if (jTF_B == null) {
269.       jTF_B = new JTextField();
270.       jTF_B.setLocation(new Point(123, 100));
271.       jTF_B.setHorizontalAlignment(JTextField.RIGHT);
272.       jTF_B.setEnabled(false);
273.       jTF_B.setFont(new Font("Dialog", Font.PLAIN, 18));
274.       jTF_B.setSize(new Dimension(40, 30));
275.     }
276.     return jTF_B;
277.   }
278. 
279.   private JTextField getJTF_C() {
280.     if (jTF_C == null) {
281.       jTF_C = new JTextField();
282.       jTF_C.setLocation(new Point(123, 166));
283.       jTF_C.setHorizontalAlignment(JTextField.RIGHT);
284.       jTF_C.setEnabled(false);
285.       jTF_C.setFont(new Font("Dialog", Font.PLAIN, 18));
286.       jTF_C.setSize(new Dimension(40, 30));
287.     }
288.     return jTF_C;
289.   }
290. 
291.   private JButton getJBtn_Exit() {
292.     if (jBtn_Exit == null) {
293.       jBtn_Exit = new JButton();
294.       jBtn_Exit.setBounds(new Rectangle(437, 225, 251, 39));
295.       jBtn_Exit.setBackground(Color.pink);
296.       jBtn_Exit.setFont(new Font("Dialog", Font.PLAIN, 18));
297.       jBtn_Exit.setText("停止所有馬達, 離開程式");
298.       jBtn_Exit.addActionListener(
299.       new java.awt.event.ActionListener() {
300.         public void actionPerformed(
301.             java.awt.event.ActionEvent e) {
302.           Motor.A.stop();
303.           Motor.B.stop();
304.           Motor.C.stop();
305.           NXTCommand.close();
306.           System.exit(0);
307.     }
308.       });
309.     }
310.     return jBtn_Exit;
311.   }
312. 
313.   public static void main(String[] args) {
314.     SwingUtilities.invokeLater(new Runnable() {
315.       public void run() {
316.     ControlMotors thisClass = new ControlMotors();
317.     thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
318.     thisClass.setVisible(true);
319.       }
320.     });
321. 
322.   }
323. 
324.   public ControlMotors() {
325.     super();
326.     initialize();
327.   }
328. 
329.   private void initialize() {
330.     this.setSize(735, 312);
331.     this.setContentPane(getJContentPane());
332.     this.setTitle("透過藍芽控制NXT伺服馬達");
333.     speedA = jSldr_A.getValue();
334.     speedB = jSldr_B.getValue();
335.     speedC = jSldr_C.getValue();
336.     jTF_A.setText(String.valueOf(speedA));
337.     jTF_B.setText(String.valueOf(speedB));
338.     jTF_C.setText(String.valueOf(speedC));
339.     NXTCommand.setVerify(true);
340.   }
341. 
342.   private JPanel getJContentPane() {
343.     if (jContentPane == null) {
344.       jLabel_A = new JLabel();
345.       jLabel_A.setText("A馬達速度:");
346.       jLabel_A.setForeground(Color.red);
347.       jLabel_A.setLocation(new Point(23, 31));
348.       jLabel_A.setSize(new Dimension(98, 34));
349.       jLabel_A.setFont(new Font("Dialog", Font.PLAIN, 18));
350.       jLabel_B = new JLabel();
351.       jLabel_B.setText("B馬達速度:");
352.       jLabel_B.setForeground(Color.red);
353.       jLabel_B.setSize(new Dimension(98, 34));
354.       jLabel_B.setLocation(new Point(23, 98));
355.       jLabel_B.setFont(new Font("Dialog", Font.PLAIN, 18));
356.       jLabel_C = new JLabel();
357.       jLabel_C.setText("C馬達速度:");
358.       jLabel_C.setForeground(Color.red);
359.       jLabel_C.setSize(new Dimension(98, 34));
360.       jLabel_C.setLocation(new Point(23, 162));
361.       jLabel_C.setFont(new Font("Dialog", Font.PLAIN, 18));;
362.       jContentPane = new JPanel();
363.       jContentPane.setLayout(null);
364.       jContentPane.setFont(new Font("Dialog",Font.PLAIN,18));
365.       jContentPane.add(jLabel_A, null);
366.       jContentPane.add(getJSldr_B(), null);
367.       jContentPane.add(getJSldr_A(), null);
368.       jContentPane.add(getJBtn_A_B(), null);
369.       jContentPane.add(getJBtn_A_F(), null);
370.       jContentPane.add(jLabel_B, null);
371.       jContentPane.add(getJBtn_C_F(), null);
372.       jContentPane.add(getJBtn_B_F(), null);
373.       jContentPane.add(getJBtn_B_B(), null);
374.       jContentPane.add(getJSldr_C(), null);
375.       jContentPane.add(jLabel_C, null);
376.       jContentPane.add(getJBtn_C_B(), null);
377.       jContentPane.add(getJTF_A(), null);
378.       jContentPane.add(getJTF_B(), null);
379.       jContentPane.add(getJTF_C(), null);
380.       jContentPane.add(getJBtn_A_S(), null);
381.       jContentPane.add(getJBtn_B_S(), null);
382.       jContentPane.add(getJBtn_C_S(), null);
383.       jContentPane.add(getJBtn_Exit(), null);
384.     }
385.     return jContentPane;
386.   }
387. } 
==<end>==============