网络五子棋的开始部分,帮忙看看?

Tony2251 2007-05-19 11:13:31
怎么弄都不行,想的方法行,但是没效果。
还没写玩,网络通讯用一个chess类实现。帮忙看看:
服务器端:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
class chess implements Serializable
{
int x;
int y;
int c;

public chess(int x,int y,int z)
{
x=x;
y=y;
c=z;
}

}
class JPanels extends JPanel implements MouseListener
{
public chess c1;
public chess c2;
ObjectOutputStream out;
ObjectInputStream in;
Socket socket;
ServerSocket server;
public JPanels ()
{ }
public void startserver()
{
this.addMouseListener(this);
try
{
server=new ServerSocket(7777);
socket=server.accept();
in=new ObjectInputStream(socket.getInputStream());
out=new ObjectOutputStream(socket.getOutputStream());
getInfo();
}
catch(Exception e)
{
System.out.println(" 1");
}
/*while(true)
{
try
{
c2=(chess)in.readObject();
draw(c2.x,c2.y,c2.c);
}
catch(Exception e)
{
System.out.println("no client");
}

}*/

}

public void getInfo()
{
try{
while(true)
{
c2=(chess)in.readObject();
draw(c2.x,c2.y,c2.c);
}
}
catch(Exception e)
{
System.out.println("no client");
}


}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i=0;i<15;i++)
{
g.drawLine(20,(i+1)*20,300,(i+1)*20);
g.drawLine((i+1)*20,20,(i+1)*20,300);
}
}
public void draw(int x,int y,int z)
{
Graphics g1=this.getGraphics();
if(z==1)
g1.setColor(Color.BLUE);
else if(z==2)
g1.setColor(Color.red);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
g1.dispose();
}
public void mouseClicked(MouseEvent e)
{
int x=e.getX()/20;
int y=e.getY()/20;
draw(x*20,y*20,2);
c1=new chess(x,y,2);
try
{
out.writeObject(c1);
}
catch(Exception e1)
{
System.out.println(" no client");
}

}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
public class Netserver extends JFrame
{
JPanels p=new JPanels();
public Netserver()
{
getContentPane().add(p);
}
public static void main(String[] args)
{
Netserver net1=new Netserver();
net1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
net1.setTitle("Server");
net1.setSize(400, 400);
net1.setVisible(true);
net1.p.startserver();
}

}
客户端:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
class chess implements Serializable//表示坐标
{
int x;
int y;
int c;

public chess(int x,int y,int z)
{
x=x;
y=y;
c=z;
}

}
class JPanels extends JPanel implements MouseListener//
{
public chess c1;
public chess c2;
private ObjectOutputStream out;
private ObjectInputStream in;
private Socket socket;
public JPanels ()
{}
public void startserver()
{
this.addMouseListener(this);
try
{
socket=new Socket("127.0.1",7777);
in=new ObjectInputStream(socket.getInputStream());
out=new ObjectOutputStream(socket.getOutputStream());
}
catch(Exception e)
{
System.out.println("1");
}
}
public void getInfo()
{
try
{
while(true)
{
c2=(chess)in.readObject();
draw(c2.x,c2.y,c2.c);
}
}
catch(Exception e)
{
System.out.println("no server");
}
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i=0;i<15;i++)
{
g.drawLine(20,(i+1)*20,300,(i+1)*20);
g.drawLine((i+1)*20,20,(i+1)*20,300);
}
}
public void draw(int x,int y,int z)
{
Graphics g1=this.getGraphics();
if(z==1)
g1.setColor(Color.BLUE);
else if(z==2)
g1.setColor(Color.red);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
g1.dispose();
}
public void mouseClicked(MouseEvent e)
{
int x=e.getX()/20;
int y=e.getY()/20;
draw(x*20,y*20,1);
c1=new chess(x,y,1);
try
{
out.writeObject(c1);
}
catch(Exception e1)
{
System.out.println(" no server");
}

}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
public class Net extends JFrame
{
JPanels p=new JPanels();
public Net()
{
getContentPane().add(p);
}
public static void main(String[] args)
{
Net net1=new Net();
net1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
net1.setTitle("Net");
net1.setSize(400, 400);
net1.setVisible(true);
net1.p.startserver();
}

}
...全文
179 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tony2251 2007-05-20
  • 打赏
  • 举报
回复
帮忙看看啊~!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧