首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

帮忙看看这个五子棋的颜色如何搞的

2011-12-18 
帮忙看看这个五子棋的颜色怎么搞的?Serverimportjava.awt.*importjava.awt.event.*importjavax.swing.*

帮忙看看这个五子棋的颜色怎么搞的?
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];
 
  publicJPanels   ()
  {    
  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];
 
  publicJPanels   ()
  {    
  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)
{
        Netnet1=new   Net();
        net1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                net1.setTitle( "Net ");
                net1.setSize(400,   400);
                net1.setVisible(true);
                net1.connect();
        }

}

[解决办法]
回楼主。你的这个程序是来自一本《JAVA编程基础,运用与实例》书吧?
这本书上面的五子棋程序我运行过了。有些问题。
但我没具体调试。关于五子棋的棋子颜色,你可以自己定义颜色的。关键是棋盘面板如何处理。

热点排行