通话绘图板
电子白板程序,在网络中相互传送各种颜色的自定义图形,是聊天程序的一附加功能,利用的是UDP通信
package whiteboard;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.text.JTextComponent;
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
//窗体类
public class WBFrame extends JFrame{
// 设置窗口组件
JButton deliver;
JButton clear;
JButton clear1;
Canvas canvas,canvas1;
JPanel pane;
//BorderLayout border=new BorderLayout();
BoxLayout border=new BoxLayout(pane,BoxLayout.Y_AXIS);
String[] colorStrings={"黑色","红色","绿色","蓝色","灰色","黄色","白色"};
JComboBox colorlist;
static int coll=1;
static Color col=Color.black,col2=Color.black;
//设置接收端和发送端
client sendpacket;
server recpacket;
//接收端IP地址和接收端与发送端的端口
private int sport,rport;
private String IP;
//构造函数
//参数:rport--接收端口;sport--发送端口;IP--接受端IP
public WBFrame(int rport,int sport,String IP) {
super("白板程序");
setSize(256,380);
this.sport=sport;
this.rport=rport;
this.IP=IP;
pane=new JPanel();
deliver=new JButton("发送");
clear=new JButton("清除发送窗口");
clear1=new JButton("清除接收窗口");
//buttonPanel=new JPanel();
//buttonPanel.setLayout(border1);
clear1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
recpacket.canvas_clear1(e);
}
});
deliver.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendpacket.deliver_actionPerformed(e);
}
});
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendpacket.clear_actionPerformed(e);
}
});
//canvasPanel=new JPanel();
canvas1=new Canvas();
canvas1.setBackground(Color.ORANGE);
canvas1.setBounds(1,130,256,130);
canvas=new Canvas();
canvas.setBackground(Color.PINK);
canvas.setBounds(1,1,256,130);
pane.add(canvas);
pane.add(canvas1);
canvas.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
sendpacket.canvas_mousePressed(e);
}
public void mouseReleased(MouseEvent e){
sendpacket.canvas_mouseReleased(e);
}
});
canvas.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
sendpacket.canvas_mouseDragged(e);
}
});
colorlist=new JComboBox(colorStrings);
colorlist.setSelectedIndex(0);
colorlist.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox cb=(JComboBox)e.getSource();
String color=(String)cb.getSelectedItem();
col=WBFrame.selectcolor(color);
}
});
pane.add(colorlist);
pane.add(deliver);
pane.add(clear1);
pane.add(clear);
sendpacket=new client(canvas,IP,sport);
recpacket=new server(rport,canvas1);
getContentPane().add(pane);
//while(true)
//recpacket.rec();
}
//收包函数
public void recei(){
recpacket.rec();
}
//颜色选择
public static Color selectcolor(String color){
//JComboBox cb=(JComboBox)e.getSource();
//String color=(String)cb.getSelectedItem();
if(color=="红色")
{col=Color.red;
WBFrame.coll=0;}
if(color=="黑色")
{col=Color.black;
WBFrame.coll=1;}
if(color=="绿色")
{col=Color.green;
WBFrame.coll=2;}
if(color=="黄色")
{col=Color.yellow;
WBFrame.coll=3;}
if(color=="蓝色")
{col=Color.blue;
WBFrame.coll=4;}
if(color=="白色")
{col=Color.white;
WBFrame.coll=5;}
if(color=="灰色")
{ col=Color.gray;
WBFrame.coll=6;}
return col;
}
public static Color findcolor(int color){
if(color==0)
col2=Color.red;
if(color==1)
col2=Color.black;
if(color==2)
col2=Color.green;
if(color==3)
col2=Color.yellow;
if(color==4)
col2=Color.blue;
if(color==5)
col2=Color.white;
if(color==6)
col2=Color.gray;
return col2;
}
//主函数
public static void main(String []args){
WBFrame No1;
No1=new WBFrame(5000,6000,"127.0.0.1");
No1.setVisible(true);
while(true)
{
//System.out.print("before 1\n");
No1.recei();
}
}
}
//发送类
class client{
DatagramPacket packet,packet1;
DatagramSocket socket;
int starx,stary,endx,endy;
int[] tmp=new int[10240];
byte[] buf=new byte[10240];
int[] colour=new int[1024];
byte[] colbuf=new byte[1024];
InetAddress address;
int zh=1;
int ch=1;
int i=1;
Canvas canvas;
private String IP;
private int port;
String color;
//构造函数
//参数:canvas--发送时用到的画布;IP--接收端的IP;port--接收端的端口
public client(Canvas canvas,String IP,int port){
this.canvas=canvas;
this.IP=IP;
this.port=port;
}
//清除发送画布
public void clear_actionPerformed(ActionEvent e){
canvas.setBackground(Color.BLACK);
canvas.setBackground(Color.pink);
//始清除掉但未发送的不再发送
zh=1;
}
//按下鼠标,记下起始点坐标
public void canvas_mousePressed(MouseEvent e){
starx=e.getX();
stary=e.getY();
}
//鼠标拖动,记下各点坐标并同时画图,将记下的坐标存入一整型数组
public void canvas_mouseDragged(MouseEvent e){
Graphics g=canvas.getGraphics();
//System.out.print("hlello\n");
endx=e.getX();
endy=e.getY();
tmp[zh++]=starx;
tmp[zh++]=stary;
tmp[zh++]=endx;
tmp[zh++]=endy;
g.setColor(WBFrame.col);
g.drawLine(starx,stary,endx,endy);
starx=endx;
stary=endy;
}
//抬起鼠标按键,划出本笔画最后一点
public void canvas_mouseReleased(MouseEvent e){
endx=e.getX();
endy=e.getY();
Graphics g=canvas.getGraphics();
g.setColor(WBFrame.col);
g.drawLine(starx,stary,endx,endy);
tmp[zh++]=starx;
tmp[zh++]=stary;
tmp[zh++]=endx;
tmp[zh++]=endy;
colour[ch++]=zh;
colour[ch++]=WBFrame.coll;
//System.out.println(colour[ch-2]+colour[ch-1]);
//colbuf[i]=(byte)zh;
//colbuf[i++]=(byte)(zh/256);
//colbuf[i++]=(byte)WBFrame.coll;
}
//发送包
//点击发送按钮,发送所绘图形的各点坐标(byte型数组)
public void deliver_actionPerformed(ActionEvent e){
try{
int j;
for(int i=1;i<zh;i++){
buf[i+1]=(byte)tmp[i];
// System.out.println(tmp[i]);
}
tmp[0]=zh;
buf[0]=(byte)tmp[0];
buf[1]=(byte)(zh/256);
//System.out.println(tmp[0]);
zh=1;
colour[0]=ch;
colbuf[0]=(byte)colour[0];
for(i=1,j=1;j<ch;i=i+3,j=j+2){
//System.out.println(colour[i]);
colbuf[i]=(byte)colour[j];
colbuf[i+1]=(byte)(colour[j]/256);
colbuf[i+2]=(byte)colour[j+1];
}
ch=1;
}catch(Exception el){
System.out.println("Error:1");
}
try{
address=InetAddress.getByName(IP);
socket=new DatagramSocket();
}catch(Exception el){
el.printStackTrace();
System.out.println("Error:2");
}
try{
packet=new DatagramPacket(buf,buf.length,address,port);
packet1=new DatagramPacket(colbuf,colbuf.length,address,port);
socket.send(packet);
socket.send(packet1);
socket.close();
}catch(Exception el){
el.printStackTrace();
System.out.println("Error:3");
}
}
}
//接收类
class server{
DatagramSocket socket;
DatagramPacket packet,packet1;
byte[] buf=new byte[10240];
int[] data=new int[10240];
byte[] colbuf=new byte[512];
int[] colour=new int[512];
int port;
Canvas canvas;
//清除接收画布
public void canvas_clear1(ActionEvent e){
canvas.setBackground(Color.BLACK);
canvas.setBackground(Color.ORANGE);
}
//构造函数
//参数:port--接受端口,canvas--接收图形画布
public server(int port,Canvas canvas){
this.port=port;
this.canvas=canvas;
}
//接收函数
//将接收的数据解包,类型转换并重绘图形
public void rec(){
int stx=0,sty=0,enx=0,eny=0;
int len=0;
int low,high;
try{
socket=new DatagramSocket(port);
packet=new DatagramPacket(buf,buf.length);
packet1=new DatagramPacket(colbuf,colbuf.length);
socket.receive(packet);
socket.receive(packet1);
System.out.print("haha shou dao le\n");
low=buf[0];
high=buf[1];
if(low<0)
low+=256;
if(high<0)
high+=256;
len=low+high*256;
//System.out.println("len="+len);
for(int i=1;i<len;i++)
{
data[i]=(int)buf[i+1];
//System.out.println(data[i]);
}
for(int i=1,j=1;j<(int)colbuf[0];i=i+3,j=j+2)
{ colour[j]=colbuf[i]+colbuf[i+1]*256;
colour[j+1]=(int)colbuf[i+2];
//System.out.println(colour[i]);
}
//for(int i=1;i<=(int)(colbuf[0]-1)/3;i++)
//System.out.println(colour[i]);
}catch(IOException e){
e.printStackTrace();
System.out.println("wrong1");
}
int a=1,b=1;
while(a<(len-1)){
stx=data[a];
a++;
sty=data[a];
a++;
enx=data[a];
a++;
eny=data[a];
a++;
if(stx<0)
stx+=256;
if(sty<0)
sty+=256;
if(enx<0)
enx+=256;
if(eny<0)
eny+=256;
Graphics g=canvas.getGraphics();
if(a<=colour[b]){
g.setColor(WBFrame.findcolor(colour[b+1]));
g.drawLine(stx, sty, enx, eny);
//System.out.print(stx+","+sty+","+enx+","+eny+"\n");
}
else
{b=b+2;
g.setColor(WBFrame.findcolor(colour[b+1]));
g.drawLine(stx, sty, enx, eny);
}
}
//Graphics g=canvas.getGraphics();
//g.drawLine(stx,sty,enx,eny);
//System.out.print(stx+","+sty+","+enx+","+eny+"\n");
socket.close();
}
}
浙公网安备 33010602011771号