-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
262 lines (208 loc) · 6.84 KB
/
App.java
File metadata and controls
262 lines (208 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.codebin;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
/**
* This was written on jCrete 2015 on a LeJOS session by Steven Chin.
*
* We were given the task to build a lego and do something cool with it.
* We built a pretty large tank and remote-controlled it via wi-fi with
* simple socket commands.
*
* For more info, go to https://mihail.stoynov.com/?p=2909
*
* This is a mock server for testing if you don't have a lego at hand.
*
* @author Nayden Gochev, jug.bg
* @author Mihail Stoynov, jug.bg
*
*/
public class App {
/*private static final long serialVersionUID = -8402983606638099877L;
private JButton buttonSubmit;
private JPanel panelMain;
private JTextArea systemInfo;
private JTextField systemCommand;
/*private final JButton left;
private final JButton right;
private final JButton up;
private final JButton down;
private final JButton stop;*/
/*private BufferedWriter pw; */
/* public static void main(String[] args) {
public App() {
buttonSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
}
/*
public static void main(String[] args) {
JFrame frame = new JFrame("App");
frame.setContentPane(new App().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
*/
/*public static void main(String[] args) {
App remote = new App();
remote.setSize(500, 500);
remote.setVisible(true);
}
public App() {
try {
Socket socket = new Socket("172.16.42.26", 19231);
// Socket socket = new Socket("127.0.0.1", 19231);//for mocking
pw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setLayout(new BorderLayout());
left = new JButton("LEFT");
this.getContentPane().add(left, BorderLayout.WEST);
left.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
leftRelease();
// System.out.println("LEFT-RELEASE\n");pw.flush();
}
@Override
public void mousePressed(MouseEvent e) {
leftPress();
// System.out.println("LEFT-PRESS\n");pw.flush();
}
});
right = new JButton("RIGHT");
this.getContentPane().add(right, BorderLayout.EAST);
right.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
rightRelease();
// System.out.println("RIGHT-RELEASE\n");pw.flush();
}
@Override
public void mousePressed(MouseEvent e) {
rightPress();
// System.out.println("RIGHT-PRESS\n");pw.flush();
}
});
up = new JButton("UP");
this.getContentPane().add(up, BorderLayout.NORTH);
up.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
upRelease();
// System.out.println("UP-RELEASE\n");pw.flush();
}
@Override
public void mousePressed(MouseEvent e) {
upPress();
// System.out.println("UP-PRESS\n");pw.flush();
}
});
down = new JButton("DOWN");
this.getContentPane().add(down, BorderLayout.SOUTH);
down.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
downRelease();
// System.out.println("DOWN-RELEASE\n");
}
@Override
public void mousePressed(MouseEvent e) {
downPress();
// System.out.println("DOWN-PRESS\n");
}
});
stop = new JButton("STOP\n");
this.getContentPane().add(stop, BorderLayout.CENTER);
stop.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
sendCommand("STOP");
}
});
//keypad arrows work only if the focus is on the stop button
//lame but that's life
stop.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent event) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent event) {
if(KeyEvent.VK_UP == event.getKeyCode()){
upRelease();
} else if(KeyEvent.VK_DOWN == event.getKeyCode()){
downRelease();
} else if(KeyEvent.VK_LEFT == event.getKeyCode()){
leftRelease();
} else if(KeyEvent.VK_RIGHT == event.getKeyCode()){
rightRelease();
}
}
@Override
public void keyPressed(KeyEvent event) {
if(KeyEvent.VK_UP == event.getKeyCode()){
upPress();
} else if(KeyEvent.VK_DOWN == event.getKeyCode()){
downPress();
} else if(KeyEvent.VK_LEFT == event.getKeyCode()){
leftPress();
} else if(KeyEvent.VK_RIGHT == event.getKeyCode()){
rightPress();
}
}
});
pack();
}
private void downPress() {
sendCommand("DOWN-PRESS");
}
private void sendCommand(String command) {
try {
pw.write(command+"\n");pw.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void downRelease() {
sendCommand("DOWN-RELEASE");
}
private void leftPress() {
sendCommand("LEFT-PRESS");
}
private void upRelease() {
sendCommand("UP-RELEASE");
}
private void leftRelease() {
sendCommand("LEFT-RELEASE");
}
private void rightRelease() {
sendCommand("RIGHT-RELEASE");
}
private void upPress() {
sendCommand("UP-PRESS");
}
private void rightPress() {
sendCommand("RIGHT-PRESS");
} */
}