package com.pipi.client;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.*;
public class MainWnd extends Thread implements ActionListener,MouseListener{ //用于创建客户端窗口和开始客户端线程
DrawingArea drawArea=new DrawingArea();
JFrame painter=new JFrame();
JToolBar jtb=new JToolBar("工具栏");
JPanel chatBoard=new JPanel(new BorderLayout());
Color selectedColor;
JPanel logPart=new JPanel();
JPanel chatPart=new JPanel(new BorderLayout());
ImageIcon line=new ImageIcon("直线.jpg"); //画直线按钮加载的图片
ImageIcon rect=new ImageIcon("矩形.jpg");
ImageIcon ellipse=new ImageIcon("椭圆.jpg");
ImageIcon curly=new ImageIcon("pencil.jpg");
ImageIcon r=new ImageIcon("红.jpg"); //选择颜色按钮加载的图片
ImageIcon b=new ImageIcon("蓝.jpg");
ImageIcon g=new ImageIcon("绿.jpg");
ImageIcon bk=new ImageIcon("黑.jpg");
ImageIcon y=new ImageIcon("黄.jpg");
ImageIcon edit=new ImageIcon("editcolor.jpg");
JButton lin=new JButton(line); //点击该按钮画直线
JButton rec=new JButton(rect);
JButton ell=new JButton(ellipse);
JButton cur=new JButton(curly);
JButton clearRecord= new JButton("清屏"); //清空聊天记录
JButton clear= new JButton("清空");
JButton red=new JButton(r);
JButton green=new JButton(g);
JButton yellow=new JButton(y);
JButton blue=new JButton(b);
JButton log=new JButton("登陆"); //与服务器建立或断开连接
JButton curColor=new JButton("当前色"); //显示当前使用的颜色
JButton more=new JButton(edit); //显示更多颜色
JButton bla = new JButton(bk);
JLabel portLabel=new JLabel("port:");
JLabel ipLabel=new JLabel("IP:");
JLabel nameJLabel=new JLabel("用户名:");
JLabel logLabel=new JLabel("请在此处登录╮(╯▽╰)╭ ");
JLabel tip=new JLabel("提示:按回车Enter键发送");
JTextField port=new JTextField("8000"); //在此输入服务器端口号
JTextField ip=new JTextField("127.0.0.1"); //输入服务器IP
JTextField name=new JTextField();
JTextField chatMsg=new JTextField();
static JTextArea chatRecord =new JTextArea(20,20); //存放聊天记录
boolean send=false;
JScrollPane scrollBar;
static String message=null;
Socket mySocket;
BufferedReader in;
int logNum=1;
boolean isConnect=false;
Thread thread;
public JPanel creatClient(){ //创建客户端窗口
JPanel allpan=new JPanel(new BorderLayout());
jtb.setBorderPainted(true);
jtb.setLayout(new GridLayout(6,2));
jtb.add(lin);
jtb.add(rec);
jtb.add(ell);
jtb.add(cur);
jtb.add(red);
jtb.add(green);
jtb.add(blue);
jtb.add(yellow);
jtb.add(bla);
jtb.add(more);
jtb.add(curColor);
curColor.setBackground(Color.black);
jtb.add(clear);
scrollBar=new JScrollPane(chatRecord);
chatRecord.setBorder(BorderFactory.createTitledBorder("聊天记录"));
chatRecord.setEditable(false);
chatRecord.setLineWrap(true);
chatRecord.setColumns(18);
chatMsg.setColumns(15);
chatPart.add(chatMsg,BorderLayout.CENTER);
chatPart.add(clearRecord,BorderLayout.EAST);
chatPart.add(tip,BorderLayout.SOUTH);
chatBoard.add(scrollBar,BorderLayout.CENTER);
chatBoard.add(chatPart,BorderLayout.PAGE_END);
logPart.add(logLabel);
logPart.add(portLabel);
logPart.add(name);
logPart.add(portLabel);
logPart.add(port);
logPart.add(ipLabel);
logPart.add(ip);
logPart.add(log);
port.setColumns(10);
ip.setColumns(10);
name.setColumns(10);
allpan.add(jtb,BorderLayout.WEST);
allpan.add(chatBoard,BorderLayout.EAST);
allpan.add(logPart,BorderLayout.NORTH);
allpan.add(drawArea,BorderLayout.CENTER);
lin.addActionListener(this);
rec.addActionListener(this);
ell.addActionListener(this);
cur.addActionListener(this);
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
yellow.addActionListener(this);
clear.addActionListener(this);
bla.addActionListener(this);
more.addActionListener(this);
log.addActionListener(this);
chatMsg.addActionListener(this);
clearRecord.addActionListener(this);
drawArea.addMouseListener(this);
return allpan;
}
private void mesReceiveAndHandle(String s){ //处理收到的消息
String[] info=s.split(":"); //“:”作为分隔符,第一个冒号之前的内容为消息的类型
String str;
if(info[0].equals("log")){
if(info[1].equals(name.getText()))
chatRecord.append("您已成功登录!\n");
else {
chatRecord.append(info[1]+" 登录成功!\n");
}
}
else if(info[0].equals("quit")){
chatRecord.append(info[1]+" 已断开连接!\n");
}
else if(info[0].equals("clear")){
drawArea.drawClear();
}
else if(info[0].equals("chat")){
if(info[1].equals(name.getText())) {
chatRecord.append("我说:"+info[2]+"\n");
}
else {
chatRecord.append(info[1]+"说:"+info[2]+"\n");
}
}
else{
int x1=Integer.valueOf(info[1]); //存放坐标
int y1=Integer.valueOf(info[2]);
int x2=Integer.valueOf(info[3]);
int y2=Integer.valueOf(info[4]);
str=info[5]; //存放颜色类型
if(info[0].equals("line")) //类型为line则画直线
DrawingArea.type=1;
else if(info[0].equals("rect"))
DrawingArea.type=2;
else if(info[0].equals("ell"))
DrawingArea.type=3;
else if(info[0].equals("cur"))
DrawingArea.type=4;
if(str.equals("red")){ //颜色类型为red时设置画笔颜色为红色,并把当前颜色显示为红色
curColor.setBackground(Color.red);
DrawingArea.color=Color.red;
}
else if(str.equals("green")){
curColor.setBackground(Color.green);
DrawingArea.color=Color.green;
}
else if(str.equals("yellow")){
curColor.setBackground(Color.yellow);
DrawingArea.color=Color.yellow;
}
else if(str.equals("blue")){
curColor.setBackground(Color.blue);
DrawingArea.color=Color.blue;
}
else if(str.equals("bla")){
curColor.setBackground(Color.black);
DrawingArea.color=Color.black;
}
else if(str.equals("more")){
curColor.setBackground(new Color(selectedColor.getRGB()));
DrawingArea.color = selectedColor;
}
drawArea.drawReceive(x1, y1, x2, y2); //调用drawReceive方法
}
}
public void run(){ //用于接收服务器发来的消息
try{
String line=" ";
while(isConnect&&line!=null){
line=in.readLine();
if(line!=null){
mesReceiveAndHandle(line);
}
}
} catch (IOException e){
System.err.println(e);
}
}
public boolean connect(){ //用于和服务器建立连接
String ports=port.getText(); //获得服务器端口号
String ips=ip.getText(); //获得ip
if(mySocket!=null)
return false;
int por=Integer.valueOf(ports);
try{
if(!name.getText().equals("")&&!ip.getText().equals("")&&!port.getText().equals("")){
mySocket=new Socket(ips,por);
in=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
drawArea.sendToServer=new PrintWriter(new OutputStreamWriter(mySocket.getOutputStream()),true);
isConnect=true;
thread=new Thread(this); //连接建立成功后,线程开启
thread.start();
DrawingArea.isLine=true;
}
else {
return false;
}
}
catch (Exception e) {
isConnect=false;
return false;
}
return true;
}
public void disconnect(){ //断开和服务器的连接
if(mySocket!=null){
try{
isConnect=false;
mySocket.close();
} catch (Exception e){
System.err.println(e);
}