|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
/**
*
* @author LS
*/
public class Main extends JFrame {
private final int HDRLEN = 50;
private final int SZOFS = 29;
private JLabel m_label = null;
Socket connection;
InputStream in;
private JPanel jPanel1;
private JPanel jPanel2;
JButton w = new JButton("前进");
JButton s = new JButton("后退");
JButton a = new JButton("左转");
JButton d = new JButton("右转");
private Socket socket;
private PrintWriter ps;
private String sip = "10.0.0.10";
private int port = 12580;
private int vport = 7070;
public Main() {
getContentPane().setLayout(null);
setSize(800, 600);
setLocation(200, 100);
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel1.setLayout(null);
jPanel2.setLayout(new GridLayout(3, 3));
jPanel1.setSize(400, 400);
jPanel1.setBackground(new Color(0, 255, 0));
jPanel1.setLocation(10, 10);
m_label = new JLabel("123");
m_label.setSize(400, 400);
add(m_label);
jPanel2.setSize(200, 200);
jPanel2.setLocation(500, 100);
jPanel2.add(w, BorderLayout.NORTH);
jPanel2.add(s, BorderLayout.SOUTH);
jPanel2.add(a, BorderLayout.WEST);
jPanel2.add(d, BorderLayout.EAST);
getContentPane().add(jPanel2);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
w.addActionListener(new Listener());
s.addActionListener(new Listener());
a.addActionListener(new Listener());
d.addActionListener(new Listener());
try {
socket = new Socket(sip, port);
ps = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
} catch (IOException e) {
}
setVisible(true);
setResizable(false);
setTitle("Control");
}
class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("前进")) {
ps.print("w");
ps.flush();
}
if (e.getActionCommand().equals("后退")) {
ps.print("ww");
ps.flush();
}
if (e.getActionCommand().equals("左转")) {
ps.print("www");
ps.flush();
}
if (e.getActionCommand().equals("右转")) {
ps.print("w");
ps.flush();
}
}
}
public static void main(String[] args) {
Main m = new Main();
m.run();
}
public void run() {
OutputStream out = null;
try {
connection = new Socket(sip, vport);
in = connection.getInputStream();
out = connection.getOutputStream();
System.out.println(in);
} catch (IOException e) {
System.out.println("socket conn filed");
}
byte[] buffer = new byte[512 * 1024];
byte[] b = { 'O', 'K', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
while (true) {
int n = 0;
int siz = 0;
try {
out.write(b);
n = in.read(buffer, 0, HDRLEN);
} catch (Exception e) {
}
if (n < HDRLEN) {
System.out.println("n < HDRLEN");
continue;
} else {
System.out.println("n >HDRLEN");
int ssz = SZOFS;
siz += unsignedByteToInt(buffer[ssz + 3]) << 24;
siz += unsignedByteToInt(buffer[ssz + 2]) << 16;
siz += unsignedByteToInt(buffer[ssz + 1]) << 8;
siz += unsignedByteToInt(buffer[ssz]);
n = HDRLEN;
if (buffer[0] != 'S' || buffer[1] != 'P' || buffer[2] != 'C'
|| buffer[3] != 'A') {
continue;
} else if (siz <= 0 || siz > (512 * 1024)) {
siz = 0;
try {
in.read(buffer, n, buffer.length);
} catch (Exception e) {
}
continue;
} else {
do {
int r = 0;
try {
r = in.read(buffer, n, buffer.length - n);
} catch (Exception e) {
e.printStackTrace();
}
n += r;
} while (n < (siz + HDRLEN));
}
}
byte[] buffer2 = new byte[n];
for (int i = 0; i < n; i++)
buffer2[i] = buffer[i + HDRLEN];
try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(
buffer2));
ImageIcon ii = new ImageIcon(image);
// ImageIcon ii = new
// ImageIcon(Toolkit.getDefaultToolkit().createImage(buffer2));
m_label.setIcon(ii);
} catch (IOException e) {
}
}// while
}
public static int unsignedByteToInt(byte b) {
return (int) b & 0xff;
}
}
|
|