实现功能,传入颜色,能画多种图形 其实,通信的本质基本就是
Socket st = new Socket ( "127.0.0.1" , 4455 ) ;
ServerSocket sst = new ServerSocket ( 4455 ) ;
sst. accept ( ) ;
只要确保以上代码,那么客户端和服务器端基本就是可以连通的,剩下的基本就是传数据的问题,怎么传,怎么接收的问题。比如本次我是要画一个直线和一个矩形,其实本质就是怎么将直线和矩形的坐标传到客户端而已。 客户端代码 首先,我们要有一个图形界面,其上面至少有三个按钮,直线,矩形,和蓝色背景的按钮,点击按钮之后是要画出对应图形的,所以要给按钮加动作监听器,给界面加鼠标监听器,
public void showUI ( ) {
JFrame jf = new JFrame ( ) ;
jf. setSize ( 600 , 800 ) ;
jf. setTitle ( "Cli Meetting by gzh4869--0.1" ) ;
jf. setLocationRelativeTo ( null ) ;
jf. setDefaultCloseOperation ( 3 ) ;
FlowLayout fl = new FlowLayout ( ) ;
jf. setLayout ( fl) ;
JButton jub = new JButton ( "直线" ) ;
jf. add ( jub) ;
JButton jub1 = new JButton ( "矩形" ) ;
jf. add ( jub1) ;
Color color = Color. BLUE;
JButton jub2 = new JButton ( ) ;
jub2. setBackground ( color) ;
jub2. setPreferredSize ( new Dimension ( 30 , 30 ) ) ;
jf. add ( jub2) ;
jf. setVisible ( true ) ;
MyClient mc = new MyClient ( ) ;
if ( mc. conn2Server ( ) ) {
Graphics g = jf. getGraphics ( ) ;
MousePic mp = new MousePic ( g, mc) ;
jub. addActionListener ( mp) ;
jub1. addActionListener ( mp) ;
jub2. addActionListener ( mp) ;
jf. addMouseListener ( mp) ;
}
}
public static void main ( String[ ] args) {
DrawPic dp = new DrawPic ( ) ;
dp. showUI ( ) ;
}
}
public class MousePic implements MouseListener, ActionListener {
String name;
MyClient mc;
Graphics g;
int x1, y1, x2, y2;
public MousePic ( Graphics g, MyClient mc) {
this . g = g;
this . mc = mc;
}
@Override
public void actionPerformed ( ActionEvent e) {
if ( "" . equals ( e. getActionCommand ( ) ) ) {
JButton jbu = ( JButton) e. getSource ( ) ;
Color color = jbu. getBackground ( ) ;
g. setColor ( color) ;
try {
mc. dos. writeByte ( 4 ) ;
} catch ( IOException e1) {
e1. printStackTrace ( ) ;
}
} else {
name = e. getActionCommand ( ) ;
}
}
@Override
public void mouseClicked ( MouseEvent e) {
}
@Override
public void mousePressed ( MouseEvent e) {
x1 = e. getX ( ) ;
y1 = e. getY ( ) ;
}
@Override
public void mouseReleased ( MouseEvent e) {
try {
x2 = e. getX ( ) ;
y2 = e. getY ( ) ;
if ( "直线" . equals ( name) ) {
g. drawLine ( x1, y1, x2, y2) ;
mc. sendLine ( x1, y1, x2, y2) ;
mc. dos. writeByte ( 1 ) ;
}
if ( "矩形" . equals ( name) ) {
g. drawRect ( x1, y1, Math. abs ( x2 - x1) , Math. abs ( y2 - y1) ) ;
mc. sendLine ( x1, y1, x2, y2) ;
mc. dos. writeByte ( 2 ) ;
}
} catch ( Exception ef) {
ef. printStackTrace ( ) ;
}
}
@Override
public void mouseEntered ( MouseEvent e) {
}
@Override
public void mouseExited ( MouseEvent e) {
}
}
public class MyClient {
OutputStream ops;
DataOutputStream dos;
public void sendLine ( int x1, int y1, int x2, int y2) {
try {
dos. writeInt ( x1) ;
System. out . println ( "写入第1个数据 " + x1) ;
dos. writeInt ( y1) ;
System. out . println ( "写入第2个数据 " + y1) ;
dos. writeInt ( x2) ;
System. out . println ( "写入第3个数据 " + x2) ;
dos. writeInt ( y2) ;
System. out . println ( "写入第4个数据 " + y2) ;
} catch ( Exception ef) {
ef. printStackTrace ( ) ;
}
}
public boolean conn2Server ( ) {
try {
Socket st = new Socket ( "127.0.0.1" , 4455 ) ;
System. out . println ( "连接成功" ) ;
ops = st. getOutputStream ( ) ;
dos= new DataOutputStream ( ops) ;
return true ;
} catch ( Exception ef) {
ef. printStackTrace ( ) ;
}
return false ;
}
public static void main ( String[ ] args) {
MyClient mc = new MyClient ( ) ;
mc. conn2Server ( ) ;
}
}
public class DrawServer {
public static void main ( String[ ] args) {
try {
ServerSocket sst = new ServerSocket ( 4455 ) ;
Socket st = sst. accept ( ) ;
InputStream ips = st. getInputStream ( ) ;
DataInputStream dis = new DataInputStream ( ips) ;
JFrame jf = new JFrame ( ) ;
jf. setSize ( 600 , 800 ) ;
jf. setTitle ( "Ser Meetting by gzh4869--0.1" ) ;
jf. setLocationRelativeTo ( null ) ;
jf. setDefaultCloseOperation ( 3 ) ;
jf. setVisible ( true ) ;
Graphics g = jf. getGraphics ( ) ;
byte v1 = dis. readByte ( ) ;
if ( v1 == 4 ) {
g. setColor ( new Color ( 0 , 128 , 255 ) ) ;
}
while ( true ) {
System. out . println ( "等待客户机发来的数据..." ) ;
int x1 = dis. readInt ( ) ;
System. out . println ( "*****服务器读第1个数据 " + x1) ;
int y1 = dis. readInt ( ) ;
System. out . println ( "*****服务器读第2个数据 " + y1) ;
int x2 = dis. readInt ( ) ;
System. out . println ( "*****服务器读第3个数据 " + x2) ;
int y2 = dis. readInt ( ) ;
System. out . println ( "*****服务器读第4个数据 " + y2) ;
byte v = dis. readByte ( ) ;
if ( v == 1 ) {
g. drawLine ( x1, y1, x2, y2) ;
} else if ( v == 2 ) {
g. drawRect ( x1, y1, Math. abs ( x2 - x1) , Math. abs ( y2 - y1) ) ;
}
System. out . println ( "收到坐标数据 " + x1 + " y1: " + y1 + " x2 " + x2 + " y2 " + y2) ;
}
} catch ( Exception ef) {
ef. printStackTrace ( ) ;
}
}
}
通过以上代码,我们知道了,其实只要客户端和服务器端连上了,剩下的就是传数据的问题。 接下来可以弄一个你画我猜的小游戏。