import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Chat extends Frame {
public static void main(String[] args){
new Chat();
}
/**
*
*/
private static final long serialVersionUID = 1L;
protected static final int SHAKE = -1;
private Button sendBtn;
private Button saveBtn;
private Button clearBtn;
private Button shakeBtn;
private TextArea view;
private TextArea sendText;
private TextField ip;
private DatagramSocket s;
public Chat() {
init();
south();
centerPanel();
event();
}
private void event() {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
s.close();
System.exit(0);
}
});
sendBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
send();
}
});
sendText.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER && e.isShiftDown()) {
send();
}
}
});
saveBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(
"chat.txt", true)));
String viewText = view.getText();
bw.write(viewText);
bw.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
clearBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
view.setText("");
}
});
shakeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
send(SHAKE);
}
});
}
private void send(int shake) {
String address = ip.getText();
try {
if (shake == SHAKE) {
DatagramPacket packet = new DatagramPacket(new byte[] { -1 },
1, InetAddress.getByName(address), 6666);
s.send(packet);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
private void send() {
String text = sendText.getText();
String address = ip.getText();
if (text.length() > 0 && address.length() > 0)
try {
DatagramPacket packet = new DatagramPacket(text.getBytes(),
text.getBytes().length, InetAddress.getByName(address),
6666);
s.send(packet);
view.append("我对 " + address + "说的话:\r\n" + text + "\r\n\r\n");
sendText.setText("");
} catch (Exception e1) {
e1.printStackTrace();
}
}
private void shake() throws Exception {
int x = this.getLocation().x;
int y = this.getLocation().y;
for (int i = 0; i < 20; i++) {
this.setLocation(x + 20, y + 20);
Thread.sleep(20);
this.setLocation(x - 20, y + 20);
Thread.sleep(20);
this.setLocation(x - 20, y - 20);
Thread.sleep(20);
this.setLocation(x, y);
Thread.sleep(20);
}
}
private void south() {
Panel south = new Panel();
ip = new TextField(20);
ip.setText("127.0.0.1");
south.add(ip);
sendBtn = new Button("send");
saveBtn = new Button("save");
clearBtn = new Button("clear");
shakeBtn = new Button("shake");
south.add(sendBtn);
south.add(saveBtn);
south.add(saveBtn);
south.add(clearBtn);
south.add(shakeBtn);
this.add(south, BorderLayout.SOUTH);
}
private void centerPanel() {
Panel center = new Panel();
center.setLayout(new BorderLayout());
view = new TextArea();
view.setVisible(true);
view.setBackground(Color.WHITE);
view.setEditable(false);
center.add(view, BorderLayout.CENTER);
sendText = new TextArea(7, 1);
center.add(sendText, BorderLayout.SOUTH);
this.add(center, BorderLayout.CENTER);
}
private void init() {
setSize(500, 500);
setLocation(100, 100);
setVisible(true);
setFont(new Font("haha", Font.PLAIN, 15));
try {
s = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
receiver();
}
private void receiver() {
Thread th = new Thread() {
public void run() {
try {
DatagramSocket socket = new DatagramSocket(6666);
DatagramPacket dp = new DatagramPacket(new byte[8192], 8192);
while (true) {
socket.receive(dp);
byte[] arr = dp.getData();
int len = dp.getLength();
if (arr[0] == SHAKE && len == 1) {
shake();
} else {
view.append("来自" + dp.getAddress().getHostAddress()
+ "的消息:\r\n" + new String(arr, 0, len)
+ "\r\n\r\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
};
};
th.setDaemon(true);
th.start();
}
}
|
|