Friday, May 13, 2011

Client Server MineSweeper source code in Java with remote method invocation

First Create a project with these 3 class.
1.game.java
2.RmiClient.java
3.ReceiveMessageInterface.java


/**
 * @(#)game.java

 *

 *

 * @author sharma

 * @version 1.00 2011/5/5

 */

 import java.rmi.*;

import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.JTabbedPane;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JButton;

import javax.swing.SwingConstants;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.Transparency;

import javax.swing.event.*;

import java.sql.*;

import java.util.*;

import java.net.URL;

import javax.imageio.ImageIO;

import java.lang.*;





import java.io.*;

import javax.swing.BoxLayout;



public class game extends JFrame {

    

    RmiClient rm;

    JLabel name = new JLabel();

    JPanel jPanel1 = new JPanel();

    JLabel jLabel4 = new JLabel();

    JLabel jLabel5 = new JLabel();

    JLabel jLabel6 = new JLabel();

    JLabel jLabel7 = new JLabel();

    JLabel jLabel8 = new JLabel();

    JTextField takip = new JTextField();

    JTextField scor = new JTextField();

    

    JButton sig = new JButton();

    JButton close = new JButton();

//    JButton l[] = new JButton(100);

    JButton l1 = new JButton();

    JButton l2 = new JButton();

    JButton l3 = new JButton();

    JButton l4 = new JButton();

    JButton l5 = new JButton();

    JButton l6 = new JButton();

    JButton l7 = new JButton();

    JButton l8 = new JButton();

    JButton l9 = new JButton();

    JButton l10 = new JButton();

    JButton l11 = new JButton();

    JButton l12 = new JButton();

    JButton l13 = new JButton();

    JButton l14 = new JButton();

    JButton l15 = new JButton();

    JButton l16 = new JButton();

    JButton l17 = new JButton();

    JButton l18 = new JButton();

    JButton l19 = new JButton();

    JButton l20 = new JButton();

    JButton l21 = new JButton();

    JButton l22 = new JButton();

    JButton l23 = new JButton();

    JButton l24 = new JButton();

    JButton l25 = new JButton();

    JButton l26 = new JButton();

    JButton l27 = new JButton();

    JButton l28 = new JButton();

    JButton l29 = new JButton();

    JButton l30 = new JButton();

    JButton l31 = new JButton();

    JButton l32 = new JButton();

    int real = 0;

    public game() {

        

        setTitle("Game");

        setSize(600,453);

        setLocation(220,100);

        

        setUndecorated(true);

        getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

        setVisible(true);

        jbinit();

        

    }

    

    void jbinit()

    {

         this.setResizable(false);

         this.getContentPane().setLayout(null);

         

         takip.setText("");

         takip.setFont(new java.awt.Font("Dialog", 1, 14));

         takip.setBounds(new Rectangle(8, 18,184, 21));

        

         sig.setText("Start");

         sig.setBounds(new Rectangle(258, 18,106, 21));

         sig.addActionListener(new st_actionAdapter(this)); 

         

         close.setText("Reset");

         close.setBounds(new Rectangle(408, 18,106, 21));

         close.addActionListener(new cl_actionAdapter(this)); 

         

         name.setText("Score :");

         name.setFont(new java.awt.Font("Dialog", 1, 14));

         name.setBounds(new Rectangle(258, 68,184, 21));

         

         scor.setText("");

         scor.setFont(new java.awt.Font("Dialog", 1, 14));

         scor.setBounds(new Rectangle(320, 68,184, 21));

         

         l1.setText("a");

         l1.setFont(new java.awt.Font("Dialog", 1, 12));

         l1.setBounds(new Rectangle(8,48,50,28));

         l1.addActionListener(new but_actionAdapter(this));

         

         l2.setText("b");

         l2.setFont(new java.awt.Font("Dialog", 1, 12));

         l2.setBounds(new Rectangle(58,48,50,28));

         l2.addActionListener(new but_actionAdapter(this));

         

         l3.setText("c");

         l3.setFont(new java.awt.Font("Dialog", 1, 12));

         l3.setBounds(new Rectangle(108,48,50,28));

         l3.addActionListener(new but_actionAdapter(this));

         

         l4.setText("d");

         l4.setFont(new java.awt.Font("Dialog", 1, 12));

         l4.setBounds(new Rectangle(158,48,50,28));

         l4.addActionListener(new but_actionAdapter(this));

         

         l5.setText("e");

         l5.setFont(new java.awt.Font("Dialog", 1, 12));

         l5.setBounds(new Rectangle(8,78,50,28));

         l5.addActionListener(new but_actionAdapter(this));

  

         l6.setText("f");

         l6.setFont(new java.awt.Font("Dialog", 1, 12));

         l6.setBounds(new Rectangle(58,78,50,28)); 

         l6.addActionListener(new but_actionAdapter(this));

         

         l7.setText("g");

         l7.setFont(new java.awt.Font("Dialog", 1, 12));

         l7.setBounds(new Rectangle(108,78,50,28));

         l7.addActionListener(new but_actionAdapter(this));

         

         l8.setText("h");

         l8.setFont(new java.awt.Font("Dialog", 1, 12));

         l8.setBounds(new Rectangle(158,78,50,28));            

         l8.addActionListener(new but_actionAdapter(this));

         

         l9.setText("i");

         l9.setFont(new java.awt.Font("Dialog", 1, 12));

         l9.setBounds(new Rectangle(8,108,50,28));

         l9.addActionListener(new but_actionAdapter(this));

         

         l10.setText("j");

         l10.setFont(new java.awt.Font("Dialog", 1, 12));

         l10.setBounds(new Rectangle(58,108,50,28));

         l10.addActionListener(new but_actionAdapter(this));

         

         l11.setText("k");

         l11.setFont(new java.awt.Font("Dialog", 1, 12));

         l11.setBounds(new Rectangle(108,108,50,28));

         l11.addActionListener(new but_actionAdapter(this));

         

         l12.setText("l");

         l12.setFont(new java.awt.Font("Dialog", 1, 12));

         l12.setBounds(new Rectangle(158,108,50,28));

         l12.addActionListener(new but_actionAdapter(this));

         

         l13.setText("m");

         l13.setFont(new java.awt.Font("Dialog", 1, 12));

         l13.setBounds(new Rectangle(8,138,50,28));

         l13.addActionListener(new but_actionAdapter(this));

         

         l14.setText("n");

         l14.setFont(new java.awt.Font("Dialog", 1, 12));

         l14.setBounds(new Rectangle(58,138,50,28));

         l14.addActionListener(new but_actionAdapter(this));

         

         l15.setText("o");

         l15.setFont(new java.awt.Font("Dialog", 1, 12));

         l15.setBounds(new Rectangle(108,138,50,28));

         l15.addActionListener(new but_actionAdapter(this));

         

         l16.setText("p");

         l16.setFont(new java.awt.Font("Dialog", 1, 12));

         l16.setBounds(new Rectangle(158,138,50,28));

         l16.addActionListener(new but_actionAdapter(this));

         

         l17.setText("q");

         l17.setFont(new java.awt.Font("Dialog", 1, 12));

         l17.setBounds(new Rectangle(8,168,50,28));

         l17.addActionListener(new but_actionAdapter(this));

         

         l18.setText("r");

         l18.setFont(new java.awt.Font("Dialog", 1, 12));

         l18.setBounds(new Rectangle(58,168,50,28));

         l18.addActionListener(new but_actionAdapter(this));

         

         l19.setText("s");

         l19.setFont(new java.awt.Font("Dialog", 1, 12));

         l19.setBounds(new Rectangle(108,168,50,28));

         l19.addActionListener(new but_actionAdapter(this));

         

         l20.setText("t");

         l20.setFont(new java.awt.Font("Dialog", 1, 12));

         l20.setBounds(new Rectangle(158,168,50,28));

         l20.addActionListener(new but_actionAdapter(this));

         

         l21.setText("u");

         l21.setFont(new java.awt.Font("Dialog", 1, 12));

         l21.setBounds(new Rectangle(8,198,50,28));

           l21.addActionListener(new but_actionAdapter(this));

          

         l22.setText("v");

         l22.setFont(new java.awt.Font("Dialog", 1, 12));

         l22.setBounds(new Rectangle(58,198,50,28)); 

         l22.addActionListener(new but_actionAdapter(this));

         

         l23.setText("w");

         l23.setFont(new java.awt.Font("Dialog", 1, 12));

         l23.setBounds(new Rectangle(108,198,50,28));

         l23.addActionListener(new but_actionAdapter(this));

         

         l24.setText("x");

         l24.setFont(new java.awt.Font("Dialog", 1, 12));

         l24.setBounds(new Rectangle(158,198,50,28));            

         l24.addActionListener(new but_actionAdapter(this));

         

         l25.setText("y");

         l25.setFont(new java.awt.Font("Dialog", 1, 12));

         l25.setBounds(new Rectangle(8,228,50,28));

         l25.addActionListener(new but_actionAdapter(this));

         

         l26.setText("z");

         l26.setFont(new java.awt.Font("Dialog", 1, 12));

         l26.setBounds(new Rectangle(58,228,50,28));

         l26.addActionListener(new but_actionAdapter(this));

         

         l27.setText("A");

         l27.setFont(new java.awt.Font("Dialog", 1, 12));

         l27.setBounds(new Rectangle(108,228,50,28));

         l27.addActionListener(new but_actionAdapter(this));

         

         l28.setText("B");

         l28.setFont(new java.awt.Font("Dialog", 1, 12));

         l28.setBounds(new Rectangle(158,228,50,28));

         l28.addActionListener(new but_actionAdapter(this));

         

         l29.setText("C");

         l29.setFont(new java.awt.Font("Dialog", 1, 12));

         l29.setBounds(new Rectangle(8,258,50,28));

         l29.addActionListener(new but_actionAdapter(this));

         

         l30.setText("D");

         l30.setFont(new java.awt.Font("Dialog", 1, 12));

         l30.setBounds(new Rectangle(58,258,50,28));

         l30.addActionListener(new but_actionAdapter(this));

         

         l31.setText("E");

         l31.setFont(new java.awt.Font("Dialog", 1, 12));

         l31.setBounds(new Rectangle(108,258,50,28));

         l31.addActionListener(new but_actionAdapter(this));

         

         l32.setText("F");

         l32.setFont(new java.awt.Font("Dialog", 1, 12));

         l32.setBounds(new Rectangle(158,258,50,28));

         l32.addActionListener(new but_actionAdapter(this));

         

         this.getContentPane().add(scor,null);

         this.getContentPane().add(name,null);

         this.getContentPane().add(takip,null);

         this.getContentPane().add(sig,null);

         this.getContentPane().add(close,null);

         this.getContentPane().add(l1,null);

         this.getContentPane().add(l2,null);

         this.getContentPane().add(l3,null);

         this.getContentPane().add(l4,null);

         this.getContentPane().add(l5,null);

         this.getContentPane().add(l6,null);

         this.getContentPane().add(l7,null);

         this.getContentPane().add(l8,null);

         this.getContentPane().add(l9,null);

         this.getContentPane().add(l10,null);

         this.getContentPane().add(l11,null);

         this.getContentPane().add(l12,null);

         this.getContentPane().add(l13,null);

         this.getContentPane().add(l14,null);

         this.getContentPane().add(l15,null);

         this.getContentPane().add(l16,null);

         this.getContentPane().add(l17,null);

         this.getContentPane().add(l18,null);

         this.getContentPane().add(l19,null);

         this.getContentPane().add(l20,null);

         this.getContentPane().add(l21,null);

         this.getContentPane().add(l22,null);

         this.getContentPane().add(l23,null);

         this.getContentPane().add(l24,null);

         this.getContentPane().add(l25,null);

         this.getContentPane().add(l26,null);

         this.getContentPane().add(l27,null);

         this.getContentPane().add(l28,null);

         this.getContentPane().add(l29,null);

         this.getContentPane().add(l30,null);

         this.getContentPane().add(l31,null);

         this.getContentPane().add(l32,null);

             

    }

    

    public static void main (String[] args) {

        

        game gm = new game();

}



void clik_action(ActionEvent e)

{

    System.out.println(""+e.getActionCommand());

    //rm.all(e.getActionCommand());

  

    String pc="",rp="",mp="",gp="";

    int i,j=0,l=0;

    char c;

    mp += e.getActionCommand();

    

    for(i=97;i<=122;i++)

    {

       rp = "";

       c = (char)i;

       rp += c;

       l = i - 96;

       if(rp.equals(mp))

       {

           j = 1;

           System.out.println(l+" "+rp);

           break;

       }

       

    }

    

    if(j != 1)

    for(i=65;i<=70;i++)

    {

       rp = "";

       c = (char)i;

       rp += c;

       l = i - 38;

       if(rp.equals(mp))

       {

           System.out.println(l+" "+rp);

           break;

       }

       

    }

    

        

    pc = Integer.toString(l);

    gp = rm.all(pc);

    

    if(gp.equals(":)"))

    {

    

        l1.setText(rm.all("1"));l2.setText(rm.all("2"));l3.setText(rm.all("3"));l4.setText(rm.all("4"));

        l5.setText(rm.all("5"));l6.setText(rm.all("6"));l7.setText(rm.all("7"));l8.setText(rm.all("8"));

        l9.setText(rm.all("9"));l10.setText(rm.all("10"));l11.setText(rm.all("11"));l12.setText(rm.all("12"));

        l13.setText(rm.all("13"));l14.setText(rm.all("14"));l15.setText(rm.all("15"));l16.setText(rm.all("16"));

        l17.setText(rm.all("17"));l18.setText(rm.all("18"));l19.setText(rm.all("19"));l20.setText(rm.all("20"));

        l21.setText(rm.all("21"));l22.setText(rm.all("22"));l23.setText(rm.all("23"));l24.setText(rm.all("24"));

        l25.setText(rm.all("25"));l26.setText(rm.all("26"));l27.setText(rm.all("27"));l28.setText(rm.all("28"));

        l29.setText(rm.all("29"));l30.setText(rm.all("30"));l31.setText(rm.all("31"));l32.setText(rm.all("32"));

    }

    if(l == 1){l1.setBackground(Color.cyan);l1.setText(gp);}if(l == 17){l17.setText(gp);setBackground(Color.cyan);}

    if(l == 2){l2.setBackground(Color.cyan);l2.setText(gp);}if(l == 18){l18.setBackground(Color.cyan);l18.setText(gp);}

    if(l == 3){l3.setBackground(Color.cyan);l3.setText(gp);}if(l == 19){l19.setText(gp);l19.setBackground(Color.cyan);}

    if(l == 4){l4.setBackground(Color.cyan);l4.setText(gp);}if(l == 20){l20.setBackground(Color.cyan);l20.setText(gp);}

    if(l == 5){l5.setBackground(Color.cyan);l5.setText(gp);}if(l == 21){l21.setBackground(Color.cyan);l21.setText(gp);}

    if(l == 6)l6.setText(gp);if(l == 22)l22.setText(gp);

    if(l == 7)l7.setText(gp);if(l == 23)l23.setText(gp);

    if(l == 8)l8.setText(gp);if(l == 24)l24.setText(gp);

    if(l == 9)l9.setText(gp);if(l == 25)l25.setText(gp);

    if(l == 10)l10.setText(gp);if(l == 26)l26.setText(gp);

    if(l == 11)l11.setText(gp);if(l == 27)l27.setText(gp);

    if(l == 12)l12.setText(gp);if(l == 28)l28.setText(gp);

    if(l == 13)l13.setText(gp);if(l == 29)l29.setText(gp);

    if(l == 14)l14.setText(gp);if(l == 30)l30.setText(gp);

    if(l == 15)l15.setText(gp);if(l == 31)l31.setText(gp);

    if(l == 16)l16.setText(gp);if(l == 32)l32.setText(gp);

    

    if(!gp.equals(":)"))

    {

     int m = Integer.parseInt(gp); 

     String iq ="";

     real += m;

     iq = Integer.toString(real);

     scor.setText(iq);

    }

    if(gp.equals(":)"))

    {

        for(i=1;i<=32;i++)

        {

        //    if()

        }

        scor.setText("You Lose!");

    }

   

    

    //System.out.println(""+sp);

    

}

    

void st_action(ActionEvent e)

{

   String st = "";

   st = takip.getText();

   System.out.println(""+st);    

    rm = new RmiClient(st);

}   



void cl_action(ActionEvent e)

{

    

    try

    {

    rm.rmiServer.receiveMessage("Connect");

    }

    catch(RemoteException ee)

    {

    }

    

    real = 0;

    l1.setText("a");l17.setText("q");

    l2.setText("b");l18.setText("r");

    l3.setText("c");l19.setText("s");

    l4.setText("d");l20.setText("t");

    l5.setText("e");l21.setText("u");

    l6.setText("f");l22.setText("v");

    l7.setText("g");l23.setText("w");

    l8.setText("h");l24.setText("x");

    l9.setText("i");l25.setText("y");

    l10.setText("j");l26.setText("z");

    l11.setText("k");l27.setText("A");

    l12.setText("l");l28.setText("B");

    l13.setText("m");l29.setText("C");

    l14.setText("n");l30.setText("D");

    l15.setText("o");l31.setText("E");

    l16.setText("p");l32.setText("F");

    scor.setText("");

}    

    

}





class but_actionAdapter implements java.awt.event.ActionListener {

  game adaptee;



  but_actionAdapter(game adaptee) {

    this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent e) {

    adaptee.clik_action(e);

  }

}



class st_actionAdapter implements java.awt.event.ActionListener {

  game adaptee;



    st_actionAdapter(game adaptee) {

    this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent e) {

    adaptee.st_action(e);

  }

}



class cl_actionAdapter implements java.awt.event.ActionListener {

  game adaptee;



  cl_actionAdapter(game adaptee) {

    this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent e) {

    adaptee.cl_action(e);

  }

}



***********************************************************************************
import java.rmi.*;

import java.rmi.registry.*;

import java.net.*;

import java.io.*;

import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.JTabbedPane;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JButton;

import javax.swing.SwingConstants;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.Transparency;

import javax.swing.event.*;

import java.sql.*;

import java.util.*;

import java.net.URL;

import javax.imageio.ImageIO;

import java.lang.*;

import javax.swing.BoxLayout;



public class RmiClient

{

       ReceiveMessageInterface rmiServer;

       Registry registry;

       String sp="";

       String serverPort="8000";

       String line="some thing";

   

    public RmiClient(String serverAddress)

    {

      

         try

         {

           // get the “registry”

           registry=LocateRegistry.getRegistry(serverAddress,(new Integer(serverPort)).intValue());

           // look up the remote object

           rmiServer =(ReceiveMessageInterface)(registry.lookup("rmiServer"));

           rmiServer.receiveMessage("Connect");

           System.out.println("Connected to :"+serverAddress);

           

         }

            catch(RemoteException e){

           e.printStackTrace();

       }

          catch(NotBoundException e){

           e.printStackTrace();

       }



         

     }

     

     public String all(String gp)

     {

         try{

         

           // call the remote method

           rmiServer.receiveMessage(gp);

           sp = rmiServer.sendMessage();

           

           System.out.println("Sending :"+gp);

           System.out.println("From Server "+sp);

     

       }

       catch(RemoteException e){

           e.printStackTrace();

       }

       return sp;

    }

}


***********************************************************************************
import java.rmi.*;



public interface ReceiveMessageInterface extends Remote

{



   void receiveMessage(String x) throws RemoteException;

    public String sendMessage() throws RemoteException;



}


***********************************************************************************

For server side.....
Create project with 2 java class
1.ReceiveMessageInterface.java
2.RmiServer.java


import java.rmi.*;



public interface ReceiveMessageInterface extends Remote

{



   void receiveMessage(String x) throws RemoteException;

    public String sendMessage() throws RemoteException;



}




***********************************************************************************
import java.rmi.*;

import java.rmi.registry.*;

import java.rmi.server.*;

import java.net.*;

import java.io.*;

import java.util.Random;





public class RmiServer extends java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface

{



    int thisPort,c = 0,p=0;

    String thisAddress,st="";

    Registry registry;    // rmi registry for lookup the remote objects.

    char[][] input = new char[100][100];

    int[][] val = new int[100][100];

    Random generator = new Random();



    // This method is called from the remote client by the RMI.



    // This is the implementation of the “ReceiveMessageInterface”.



    public void receiveMessage(String x) throws RemoteException

    {

        st = ""; 

        System.out.println(x);

        if(x.equals("Connect"))

        {

            int i,j,k,l=0,mo,r=0,g;

            k = 2;

             mo = generator.nextInt( 7 );

            for(i=0;i<8;i++)

            {

                ++r;

                g = mo % r;

                System.out.print(""+r);

                for(j=0;j<4;j++)

                {

                    if(l == k+g)

                    {

                        l = 0;

                        input[i][j] = '*';

                    }

                    else

                    {

                        input[i][j] = '.';

                    }    

                        ++l;

                }

            }

            

            // calculate

            int p,xx,yy,v;

            int px[] = {-1,-1,-1,0,0,1,1,1};

            int py[] = {0,1,-1,-1,1,-1,0,1};

            for(i=0;i<8;i++)

            {

                for(j=0;j<4;j++)

                {

                    if(input[i][j] == '.')

                    {

                        v = 0;

                        for(p=0;p<8;p++)

                        {

                            xx = i + px[p];

                            yy = j + py[p];

                            if(xx >=0 && xx <= 8 && yy >=0 && yy <= 3)

                            {

                                if(input[xx][yy] == '*')

                                {

                                    ++v;

                                }    

                            }

                        }

                        val[i][j] = v;

                    }

                    if(input[i][j] == '*')

                    {

                        val[i][j] = -1;

                    }

                }

            }

            

        }

        st += x;



    }

    

   

    public String sendMessage() throws RemoteException

    {

  

       

       int i,j,k;

       k = Integer.parseInt(st);

       k = k-1;

       j = k % 4;

       i = k/4;

       

       if(val[i][j] == -1)

       {

              st = ":)";

       }

       else

       {

              st = Integer.toString(val[i][j]);

       }

       return st;

    }







    public RmiServer() throws RemoteException

    {



        try{



            // get the address of this host.



            thisAddress= (InetAddress.getLocalHost()).toString();



        }



        catch(Exception e){



            throw new RemoteException("can't get inet address.");



        }



              thisPort=8000;  // this port(registry’s port)



        System.out.println("this address="+thisAddress+",port="+thisPort);



        try{



        // create the registry and bind the name and object.



            registry = LocateRegistry.createRegistry( thisPort );



            registry.rebind("rmiServer", this);



        }



        catch(RemoteException e){



        throw e;



        }



    }



   



    static public void main(String args[])

    {



        try

        {



        RmiServer s=new RmiServer();



       }



    catch (Exception e) {



           e.printStackTrace();



           System.exit(1);



    }



     }



}



***********************************************************************************




No comments:

Post a Comment

How to Generate and use the ssh key on Gerrit, github.io, gitlab, and bitbucket.

 Details can be found here -