帮忙看看这个五子棋的颜色怎么搞的?

Tony2251 2007-06-06 04:18:14
Server
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
class JPanels extends JPanel implements MouseListener
{

private PrintWriter out;
private int[][] map=new int[15][15];

public JPanels ()
{
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
map[i][j]=0;
this.addMouseListener(this);

}
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);
}
for(int m=0;m<15;m++)
for(int n=0;n<15;n++)
{
if(map[m][n]==1)
this.drawBlue(m*20,n*20,g);
else if(map[m][n]==-1)
this.drawRed(m*20,n*20,g);

}

}
public void setWriter(PrintWriter wr)
{
this.out=wr;
}
public void set(int x,int y,int z)
{
map[x][y]=z;
this.repaint();
}
public void drawBlue(int x,int y,Graphics g1)
{
g1.setColor(Color.blue);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
}
public void drawRed(int x,int y,Graphics g1)
{
g1.setColor(Color.red);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);

}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
int x=(int)Math.round(e.getX()/(double)20);
int y=(int)Math.round(e.getY()/(double)20);
if(x>0&&x<15&&y>0&&y<15)
{
map[x][y]=-1;
out.println("[STONE]"+x+" "+y);
this.repaint();
}
else return;

}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
public class Netserver extends JFrame implements Runnable
{
private JPanels p1=new JPanels();
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
private ServerSocket server;
public Netserver()
{
getContentPane().add(p1);
}
public void run()
{
String msg;
try
{
while((msg=reader.readLine())!=null)
{
if(msg.startsWith("[STONE]"))
{
String temp=msg.substring(7);
int x=Integer.parseInt(temp.substring(0,temp.indexOf(" ")));
int y=Integer.parseInt(temp.substring(temp.indexOf(" ")+1));
p1.set(x,y,1);
}
}
}
catch(IOException ie)
{
System.out.println("mistake");
}
}
public void connect()
{
try
{
server=new ServerSocket(7777);
socket=server.accept();
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(), true);
p1.setWriter(writer);
new Thread(this).start();
}
catch(Exception e)
{
System.out.println("no client");
}
}
public static void main(String[] args)
{
Netserver net1=new Netserver();
net1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
net1.setTitle("NetServer");
net1.setSize(400, 400);
net1.setVisible(true);
net1.connect();
}

}
Client
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
class JPanels extends JPanel implements MouseListener
{

private PrintWriter out;
private int[][] map=new int[15][15];

public JPanels ()
{
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
map[i][j]=0;
this.addMouseListener(this);

}
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);
}
for(int m=0;m<15;m++)
for(int n=0;n<15;n++)
{
if(map[m][n]==1)
this.drawBlue(m*20,n*20,g);
else if(map[m][n]==-1)
this.drawRed(m*20,n*20,g);

}

}
public void setWriter(PrintWriter wr)
{
this.out=wr;
}
public void set(int x,int y,int z)
{
map[x][y]=z;
this.repaint();
}
public void drawBlue(int x,int y,Graphics g1)
{
g1.setColor(Color.blue);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
}
public void drawRed(int x,int y,Graphics g1)
{
g1.setColor(Color.red);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);

}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
int x=(int)Math.round(e.getX()/(double)20);
int y=(int)Math.round(e.getY()/(double)20);
if(x>0&&x<15&&y>0&&y<15)
{
map[x][y]=1;
out.println("[STONE]"+x+" "+y);
this.repaint();
}
else return;

}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
public class Net extends JFrame implements Runnable
{
private JPanels p=new JPanels();
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
public Net()
{
getContentPane().add(p);
}
public void run()
{
String msg;
try
{
while((msg=reader.readLine())!=null)
{
if(msg.startsWith("[STONE]"))
{
String temp=msg.substring(7);
int x=Integer.parseInt(temp.substring(0,temp.indexOf(" ")));
int y=Integer.parseInt(temp.substring(temp.indexOf(" ")+1));
p.set(x,y,-1);
}
}
}
catch(IOException ie)
{
System.out.println("mistake");
}
}
public void connect()
{
try
{
socket=new Socket("127.0.0.1", 7777);
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(), true);
p.setWriter(writer);
new Thread(this).start();
}
catch(Exception e)
{
System.out.println("no server");
}
}
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.connect();
}

}
...全文
394 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tony2251 2007-06-09
  • 打赏
  • 举报
回复
无奈~~
MON_KEY2007 2007-06-08
  • 打赏
  • 举报
回复
回楼主。你的这个程序是来自一本《JAVA编程基础,运用与实例》书吧?
这本书上面的五子棋程序我运行过了。有些问题。
但我没具体调试。关于五子棋的棋子颜色,你可以自己定义颜色的。关键是棋盘面板如何处理。
Tony2251 2007-06-08
  • 打赏
  • 举报
回复
老兄,我得把颜色弄对,然后判赢,接着往下做阿?
Tony2251 2007-06-07
  • 打赏
  • 举报
回复
什么?
Tony2251 2007-06-06
  • 打赏
  • 举报
回复
先运行服务器端,然后客户端,有一个总是一种颜色~
dyw31415926 2007-06-06
  • 打赏
  • 举报
回复
mark玩起来不错,
Supernpc 2007-06-06
  • 打赏
  • 举报
回复
写清楚问题..

62,614

社区成员

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

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