root / trunk / code / projects / colonet / utilities / manual_control / manualControlServer / colonet_web_test / Colonet.java @ 13
History | View | Annotate | Download (2.24 KB)
1 |
|
---|---|
2 |
import java.awt.*; |
3 |
import java.awt.event.*; |
4 |
import java.applet.*; |
5 |
import javax.swing.*; |
6 |
import java.net.*; |
7 |
import java.io.*; |
8 |
|
9 |
public class Colonet extends JApplet implements ActionListener { |
10 |
|
11 |
JButton btnGo;
|
12 |
JButton btnLeft;
|
13 |
JButton btnRight;
|
14 |
JTextField txtHost;
|
15 |
JTextField txtPort;
|
16 |
JButton btnConnect;
|
17 |
JTextField txtStatus;
|
18 |
Socket socket;
|
19 |
OutputStreamWriter out;
|
20 |
|
21 |
public void init () { |
22 |
Container content = getContentPane();
|
23 |
content.setLayout(new GridLayout(10,3)); |
24 |
|
25 |
txtHost = new JTextField("localhost"); |
26 |
txtPort = new JTextField("5678"); |
27 |
btnConnect = new JButton("Connect"); |
28 |
txtStatus = new JTextField(); |
29 |
btnGo = new JButton("Forward"); |
30 |
btnLeft = new JButton("Left"); |
31 |
btnRight = new JButton("Right"); |
32 |
|
33 |
content.add(new JLabel("Host")); |
34 |
content.add(txtHost); |
35 |
content.add(new JLabel("Port")); |
36 |
content.add(txtPort); |
37 |
content.add(btnConnect); |
38 |
content.add(txtStatus); |
39 |
content.add(btnGo); |
40 |
content.add(btnLeft); |
41 |
content.add(btnRight); |
42 |
|
43 |
btnGo.addActionListener(this);
|
44 |
btnLeft.addActionListener(this);
|
45 |
btnRight.addActionListener(this);
|
46 |
btnConnect.addActionListener(this);
|
47 |
|
48 |
} |
49 |
|
50 |
|
51 |
public void actionPerformed (ActionEvent event) { |
52 |
Object source = event.getSource();
|
53 |
try {
|
54 |
if (source == btnGo) {
|
55 |
out.write('w');
|
56 |
out.flush(); |
57 |
} else if (source == btnLeft) { |
58 |
out.write('a');
|
59 |
out.flush(); |
60 |
} else if (source == btnRight) { |
61 |
out.write('d');
|
62 |
out.flush(); |
63 |
} |
64 |
} catch (Exception e) { |
65 |
txtStatus.setText("Could not send data");
|
66 |
} |
67 |
if (source == btnConnect)
|
68 |
doSocket(); |
69 |
|
70 |
} |
71 |
|
72 |
private void doSocket () { |
73 |
txtStatus.setText("Connecting ...");
|
74 |
int port = 0; |
75 |
try {
|
76 |
port = Integer.parseInt(txtPort.getText());
|
77 |
} catch (Exception e) { |
78 |
txtStatus.setText("Invalid port");
|
79 |
return;
|
80 |
} |
81 |
try {
|
82 |
socket = new Socket(txtHost.getText(), port); |
83 |
} catch (UnknownHostException e) { |
84 |
txtStatus.setText("Unknown Host Exception");
|
85 |
return;
|
86 |
} catch (IOException e) { |
87 |
txtStatus.setText("IO Exception");
|
88 |
return;
|
89 |
} |
90 |
txtStatus.setText("Connected.");
|
91 |
try {
|
92 |
out = new OutputStreamWriter(socket.getOutputStream()); |
93 |
} catch (IOException e) { |
94 |
txtStatus.setText("Could not get OutputStream");
|
95 |
} |
96 |
|
97 |
} |
98 |
|
99 |
private void msg (String text) { |
100 |
JOptionPane.showMessageDialog(null, text); |
101 |
} |
102 |
|
103 |
} |