Welcome to Dream.In.Code
Become a Java Expert!

Join 150,035 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,589 people online right now. Registration is fast and FREE... Join Now!




stuck with code

4 Pages V  1 2 3 > »   
Reply to this topicStart new topic

stuck with code

killer_beast
31 May, 2008 - 02:37 PM
Post #1

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
well i need help with this code i have two classes pla1 and pla2 which are passing a value to the final class .but in the final class it understands them seperately(the two values ) i want it to invoke the other class mx1,mx2...mx5 only after it had received the two values


CODE

class pla1 extends JFrame implements ActionListener

{
private JPanel x=new JPanel();
private JButton b1=new JButton("1");
private JButton b2=new JButton("2");
private JButton b3=new JButton("3");
private JButton b4=new JButton("4");
private JButton b5=new JButton("5");
private JTextField t=new JTextField(1);
private int terrain;
private int r;
final_class fc = new final_class();
public pla1(){
    
    super("terrain");
    getContentPane().add(x);
    setSize(300,300);
//    r=choi();

    x.add(b1);    b1.addActionListener((ActionListener) this);
    x.add(b2);    b2.addActionListener((ActionListener) this);
    x.add(b3);    b3.addActionListener((ActionListener) this);
    x.add(b4);    b4.addActionListener((ActionListener) this);
    x.add(b5);    b5.addActionListener((ActionListener) this);
    x.add(t);
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
}
public void actionPerformed(ActionEvent ev){
    if(ev.getSource()==b1){t.setText("5");terrain=5;}
    if(ev.getSource()==b2){t.setText("4");terrain=4;}
    if(ev.getSource()==b3){t.setText("3");terrain=3;}
    if(ev.getSource()==b4){t.setText("2");terrain=2;}
    if(ev.getSource()==b5){t.setText("1");terrain=1;}
    fc.passVart(terrain);
    switch(terrain){
    case 1: x.remove(b5);break;
    case 2: x.remove(b4);break;
    case 3:x.remove(b3);break;
    case 4: x.remove(b2);break;
    case 5: x.remove(b1);break;
    default: break;}
    //terrain=(int)Double.parseDouble(t.getText());
}
//public int choi(){if(terrain>0)return terrain;}
//public int choix(){return terrain;}
}


CODE

mport javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class pla2 extends JFrame implements ActionListener
{
private JPanel x=new JPanel();
private JButton b1=new JButton("1");
private JButton b2=new JButton("2");
private JButton b3=new JButton("3");
private JButton b4=new JButton("4");
private JButton b5=new JButton("5");
private int factor;
final_class fc = new final_class();
public pla2(){
    super("factor");
    getContentPane().add(x);
    setSize(300,300);
    x.add(b1);    b1.addActionListener((ActionListener) this);
    x.add(b2);    b2.addActionListener((ActionListener) this);
    x.add(b3);    b3.addActionListener((ActionListener) this);
    x.add(b4);    b4.addActionListener((ActionListener) this);
    x.add(b5);    b5.addActionListener((ActionListener) this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ev){
    if(ev.getSource()==b1){factor=1;}
    if(ev.getSource()==b2){factor=2;}
    if(ev.getSource()==b3){factor=3;}
    if(ev.getSource()==b4){factor=4;}
    if(ev.getSource()==b5){factor=5;}
    fc.passVarf(factor);
}
public int choix(){return factor;}
}



CODE

public class final_class {

    /**
     * @param args
     */
    private int factor;private int terrain;
    private static int ter;
    public void final_class(){}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        
        pla1 p1=new pla1();
        pla2 p2=new pla2();
    p1.setVisible(true);
    p2.setVisible(true);
    
// factor=p2.choix();System.out.println("fac "+factor);
    //terrain=p1.choix();System.out.println("terrain "+terrain);
    //System.out.println("terrain "+terrain);

    }
    public void passVarf(int f){
        factor = f;
        System.out.println(factor);
    }
    public void passVart(int t){
        
        terrain = t;
        System.out.println("factor"+factor);
        
//if(factor>0)§§§§    i cant get this thing to work i need both terrain and factor to have a value before they call thes e classes
            
{switch(terrain)
        {
        case 1: mx1 r1=new mx1();            
                r1.setVisible(true);break;
        case 2: mx2 r2=new mx2();            
                r2.setVisible(true);break;
        case 3: mx3 r3=new mx3();            
                r3.setVisible(true);break;
        case 4: mx4 r4=new mx4();            
                r4.setVisible(true);break;
        case 5: mx5 r5=new mx5();            
                r5.setVisible(true);break;
        }
            }
    }
}











User is offlineProfile CardPM
+Quote Post

pbl
RE: Stuck With Code
31 May, 2008 - 03:10 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
As the only difference between the classes pla1 and pla2 is the name of the "title"
I strongly that you only made 1 class pla to the constructor of which you pass the title

CODE

class pla extends JFrame implements ActionListener

   pla(String title) {
      super(title);
      .....
      .....
    }


and then in your main

CODE

public static void main(String[] arg) {
    pla pla1 = new pla("terrain");
    pla pla2 = new pla("factor");
    ....


User is online!Profile CardPM
+Quote Post

killer_beast
RE: Stuck With Code
31 May, 2008 - 03:43 PM
Post #3

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
QUOTE(pbl @ 31 May, 2008 - 04:10 PM) *

As the only difference between the classes pla1 and pla2 is the name of the "title"
I strongly that you only made 1 class pla to the constructor of which you pass the title

CODE

class pla extends JFrame implements ActionListener

   pla(String title) {
      super(title);
      .....
      .....
    }


and then in your main

CODE

public static void main(String[] arg) {
    pla pla1 = new pla("terrain");
    pla pla2 = new pla("factor");
    ....




yeah i did it but it involves a greater problem ; as i have said in the previous post i need to get these two values terrain and factor at the same time and after i get these two values i have to call the classes mx1,..mx5 depending on the choice .i basically need to know from both the users their choices and the factor decides how many fold the score should be multiplied if the person wins or loses. the classes mx1..mx5 contains a small type of game
User is offlineProfile CardPM
+Quote Post

pbl
RE: Stuck With Code
31 May, 2008 - 03:57 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Wont be abble to do this from a
public void static main() method

You will have to make a class that holds pla1 and pla2 and which have call back methods for the anwers back:

CODE

class Driver {
    int nbBack = 0;
    pla pla1, pla2;
    Driver() {
       pla1 = new pla("terrain", this);   // create the 2 pla
       pla2 = new pla("factor", this);    // passing myself as parameter
   }

    // I am call back by a pla
    void callBackWithValue(pla pla, int factor) {
       // test which one
        if(pla == pla1)
           System.out.println("pla1 calls me back with: " + factor);
        else
           System.out.println("pla2 calls me back with: " + factor);

    }


Now your pla class has to know who call back

CODE

class pla {
    Driver driverToCalledMe;

    // constructor
    pla(String title, Driver driver) {
         super(title);
         // remember who to call back
         driverToCallBack = driver;
    }

    public void actionPerformed(ActioneEvent e) {
       .....
       // now call back my creator with the factor value
       // and myself as first parameter so it will be able to identify who I am
       driverToCallBack.callBackWithValue(this, factor);
    }


User is online!Profile CardPM
+Quote Post

killer_beast
RE: Stuck With Code
31 May, 2008 - 04:26 PM
Post #5

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
QUOTE(pbl @ 31 May, 2008 - 04:57 PM) *

Wont be abble to do this from a
public void static main() method

You will have to make a class that holds pla1 and pla2 and which have call back methods for the anwers back:

CODE

class Driver {
    int nbBack = 0;
    pla pla1, pla2;
    Driver() {
       pla1 = new pla("terrain", this);   // create the 2 pla
       pla2 = new pla("factor", this);    // passing myself as parameter
   }

    // I am call back by a pla
    void callBackWithValue(pla pla, int factor) {
       // test which one
        if(pla == pla1)
           System.out.println("pla1 calls me back with: " + factor);
        else
           System.out.println("pla2 calls me back with: " + factor);

    }


Now your pla class has to know who call back

CODE

class pla {
    Driver driverToCalledMe;

    // constructor
    pla(String title, Driver driver) {
         super(title);
         // remember who to call back
         driverToCallBack = driver;
    }

    public void actionPerformed(ActioneEvent e) {
       .....
       // now call back my creator with the factor value
       // and myself as first parameter so it will be able to identify who I am
       driverToCallBack.callBackWithValue(this, factor);
    }




well it s getting a little bit complicated as i am not sure how i am going to embed this in the further program . well i will like to ask a question that will make thinks look little less complicated i have to get two values factor and terrain from these two classes these two values i will pass to executable program which would sent it to class score class score which will print the score for the two players, it is kind of a game to be played by two different computers

CODE

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class pla2 extends JFrame implements ActionListener
{
private JPanel x=new JPanel();
private JButton b1=new JButton("1");
private JButton b2=new JButton("2");
private JButton b3=new JButton("3");
private JButton b4=new JButton("4");
private JButton b5=new JButton("5");
private int factor;
public pla2(){
    super("factor");
    getContentPane().add(x);
    setSize(300,300);
    x.add(b1);    b1.addActionListener((ActionListener) this);
    x.add(b2);    b2.addActionListener((ActionListener) this);
    x.add(b3);    b3.addActionListener((ActionListener) this);
    x.add(b4);    b4.addActionListener((ActionListener) this);
    x.add(b5);    b5.addActionListener((ActionListener) this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ev){
    if(ev.getSource()==b1){factor=1;}
    if(ev.getSource()==b2){factor=2;}
    if(ev.getSource()==b3){factor=3;}
    if(ev.getSource()==b4){factor=4;}
    if(ev.getSource()==b5){factor=5;}
    
}
public int choix(){return factor;}
}

CODE
public class final_class {

    /**
     * @param args
     */
    private int factor;private int terrain;
    private static int ter;
    public void final_class(){}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        
        pla p1=new pla1("terrain");
        pla p2=new pla2("factor");
    p1.setVisible(true);
    p2.setVisible(true);
    
//
here i have to get two values factor and terrain from these two classes these two values i will pass to another class score which will print the score for the two players, it is kind of a game to be played by two different computers  

//    
   if(factor>0)
            {switch(terrain)
        {
        case 1: mx1 r1=new mx1();            
                r1.setVisible(true);break;
        case 2: mx2 r2=new mx2();            
                r2.setVisible(true);break;
        case 3: mx3 r3=new mx3();            
                r3.setVisible(true);break;
        case 4: mx4 r4=new mx4();            
                r4.setVisible(true);break;
        case 5: mx5 r5=new mx5();            
                r5.setVisible(true);break;
        }
            
}
    }
}

the class MX1 looks like this , well actually it is also supposed to return to the main class whether the player have won or not there is a variable inside it win which gets the value 0 if i lose and 1 if i win . i am laking the same trick in this part also , i dont know how to pass the value to the executable program where i can pass its value to class score to let it know if i won or not.
CODE

class mx1 extends JFrame implements ActionListener
{
    private JPanel x=new JPanel();
    private JButton start=new JButton("start");
    private JButton fin=new JButton("finish");
    private int a[]=new int[10000];
    private int b[]=new int[10000];private int i=0;private int score=1;
    private JTextField zz=new JTextField("        ");private int win;
    
    public mx1()
    {    setBounds(100,100,900,500);
    setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //getContentPane().setLayout(null);
        getContentPane().add(x);
        //x.setLayout(null);
        x.add(start);//x.add(zz);
        //start.setBounds(54,130,30,10);
        //fin.setVisible(false);x.add(fin);
        start.addActionListener((ActionListener) this);
        if(win==1){zz.setText("you win");} else if (win==0) zz.setText("you lose");
        //start.setLocation(new Point(230,150));//fin.setLocation(new Point(300,50));//x.add(fin);
    }
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==start){
                    
            {//fin.setLocation(new Point(600,350));
            //start.setLocation(new Point(100,150));
            //x.add(fin);
            }
            Graphics g2=x.getGraphics();
            {g2.setColor(Color.RED);
                //g2.drawRect(50,100,700,300);
                g2.drawRect(500,1,5,100);
                g2.drawRect(400,1,5,100);
                g2.drawRect(100,100,300,5);
                g2.drawRect(500,100,300,5);
                g2.drawRect(800,100,5,300);
                g2.drawRect(100,100,5,300);
                g2.drawRect(100,400,700,5);g2.drawRect(600,360,65,25);
                
                
                g2.drawRect(101,150,630,5);g2.drawRect(150,170,650,5);g2.drawRect(101,190,660,5);g2.drawRect(150,210,650,5);g2.drawRect(101,230,660,5);
                g2.drawRect(150,250,650,5);g2.drawRect(101,270,660,5);
                g2.drawRect(150,290,650,5);g2.drawRect(101,310,660,5);
                g2.drawRect(150,330,650,5);g2.drawRect(101,350,660,5);
                
                
                g2.dispose();}
            x.addMouseMotionListener(new MouseMotionListener(){
                public void mouseMoved(MouseEvent e)
                {
                    a[i]=e.getX();b[i]=e.getY();
                    System.out.println("x="+a[i]+"y="+b[i]);
                    if(a[i]>=101 && a[i]<=731 && b[i]>=150 && b[i]<=155){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=150 && a[i]<=800 && b[i]>=170 && b[i]<=175){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=101 && a[i]<=761 && b[i]>=190 && b[i]<=195){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=150 && a[i]<=800 && b[i]>=210 && b[i]<=215){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=101 && a[i]<=761 && b[i]>=230 && b[i]<=235){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=150 && a[i]<=800 && b[i]>=250 && b[i]<=255){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=101 && a[i]<=761 && b[i]>=270 && b[i]<=275){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=150 && a[i]<=800 && b[i]>=290 && b[i]<=295){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=101 && a[i]<=761 && b[i]>=310 && b[i]<=315){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=150 && a[i]<=800 && b[i]>=330 && b[i]<=335){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=101 && a[i]<=761 && b[i]>=350 && b[i]<=355){System.out.println("you lose");score--;System.out.println(score);}
                    
                    
                    if(a[i]>=100 && a[i]<=800 && b[i]>=400 && b[i]<=405){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=100 && a[i]<=105 && b[i]>=100 && b[i]<=400){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=800 && a[i]<=805 && b[i]>=100 && b[i]<=400){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=500 && a[i]<=800 && b[i]>=100 && b[i]<=105){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=100 && a[i]<=400 && b[i]>=100 && b[i]<=105){System.out.println("you lose");score--;System.out.println(score);}
                    if(a[i]>=600 && a[i]<=665 && b[i]>=360 && b[i]<=385){if(score>0){win=1;} else if(score<=0){win=0;}System.out.println("win"+win);}
                }
                public void mouseDragged(MouseEvent e){
                }    
            });
            fin.setBackground(Color.RED);
            fin.setVisible(true);}
    

    }}


This post has been edited by killer_beast: 31 May, 2008 - 04:28 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Stuck With Code
31 May, 2008 - 04:40 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
You just can't

You create your pla1 and pla2 which are GUI interface
you cannot in the same thread method get the values from fields populated by these GUI. You have to wait for the user to interact with the GUI.

So you cannot:
- create GUI 1
- create GUI 2
- pickup value from GUI 1 and GUI 2

You have to:

- create something to call back when the user interfere with the GUI
- create GUI 1 and tell it something to call when a button is pressed
- create GUI 2 and tell it something to call when a button is pressed

Or this will be an HORRIBLE design

CODE

public static void main(String arg[]) {
    pla pla1, pla2;
    // create a thread to create pla1, pla2
    ....
   // let make sure the thread finishes
   try(Thread.sleep(1000L);} catch(Exception e) {}
   // wait till both user input a factor
   while(pla1.factor == 0 || pla2.factor == 0)
       try(Thread.sleep(1000L);} catch(Exception e) {}
   // ok both users have input something
   int fact1 = pla1.factor;
   int fact2 = pla2.factor;
   ....


}

This post has been edited by pbl: 31 May, 2008 - 05:01 PM
User is online!Profile CardPM
+Quote Post

killer_beast
RE: Stuck With Code
31 May, 2008 - 04:55 PM
Post #7

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
QUOTE(pbl @ 31 May, 2008 - 05:40 PM) *

You just can't

You create your pla1 and pla2 which are GUI interface
you cannot in the same thread method get the values from fields populated by these GUI. You have to wait for the user to interact with the GUI.

So you cannot:
- create GUI 1
- create GUI 2
- pickup value from GUI 1 and GUI 2

You have to:

- create something to call back when the user interfere with the GUI
- create GUI 1 and tell it something to call when a button is pressed
- create GUI 2 and tell it something to call when a button is pressed



by this are you reffering that i should make a driver class as you suggested in your previous comment ,ok even if i do it but how i can get the value from class mx1 to know whether i win or not . since i have to deduct the score if i lose and increase it if i win
.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Stuck With Code
31 May, 2008 - 05:03 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Look I have edited my previous post but is is NOT a good solution
User is online!Profile CardPM
+Quote Post

killer_beast
RE: Stuck With Code
31 May, 2008 - 05:14 PM
Post #9

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
QUOTE(pbl @ 31 May, 2008 - 06:03 PM) *

Look I have edited my previous post but is is NOT a good solution


how do we create a thread and what is it ? sorry for this question but i have no idea about it .
User is offlineProfile CardPM
+Quote Post

pbl
RE: Stuck With Code
31 May, 2008 - 06:18 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
OK you want the Horrible solution

This is the shortest I could make it

java


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Horrible extends JFrame implements ActionListener {

int factor = -1;

// A JFrame with one button
Horrible(String title) {
super(title);
JButton button = new JButton("Button");
add(button);
button.addActionListener(this);
setSize(200, 100);
setVisible(true);
}
// button pressed changes factor
public void actionPerformed(ActionEvent arg0) {
System.out.println("Button click in " + getTitle());
factor = 10;
}

// the 2 JFrame static so that my main method can access them
static Horrible horrible1, horrible2;

public static void main(String[] arg) {
// A thread to create the GUI 2 frames
Thread t = new CreateGui();
// start the thread
t.start();
// wait until both are created (horrible2 is created second so lets wait for it)
while(horrible2 == null) {
try {
Thread.sleep(1000L);
}
catch (Exception e) {}
}
// ok the 2 frames where created
// now we can wait on both factor
while(horrible1.factor == -1 || horrible2.factor == -1) {
System.out.println("We wait because one of the factor still == -1");
try {
Thread.sleep(1000L); // sleep 1 second
}
catch (Exception e) {}
}
// OK both factor do not equals -1 anymore
System.out.println("Ready to process: factor1 = " + horrible1.factor + " factor2 = " + horrible2.factor);
}

// a class that will create in a thread the 2 JFrame
// class is static because called by the main method
static class CreateGui extends Thread {
public void run() {
horrible1 = new Horrible("Terrain");
horrible1.setLocation(10, 10);
horrible2 = new Horrible("Factor");
horrible2.setLocation(10, 120);
}
}
}


User is online!Profile CardPM
+Quote Post

killer_beast
RE: Stuck With Code
4 Jun, 2008 - 02:11 PM
Post #11

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
well thanks a lot for the last code it actually solved most of the problems i faced so far in the project , i still dont understand why you said it was the horrible solution . it seemed to work fasted and in a less complicated way than anything i used .

well i have actually finished this part of the project , the main purpose of the program was to actually create a server and a client program . i know its not much like it as i started with kind of bad idea to implement this type of methodology . well i have designed the server and client program buit have not tested it exactly as i dont actually know how i am supposed to test it on the same machine .

well i will need some more help, if you can , can you analyse that wheteher this code would actually work for the client server program.
this is the first time i have used server client program , so i am pretty unsure about everything ,,,i just followed the program given by our professor .
i am actually not sure about the pla class where i have defined the server client program , actually the doubt is
in the if else statement , i have generated the class after the if else statement , shall i include the defination and the implementations of all the buttons( creation and the value they provide) in both the if else statement s ,i.e have it created individually for both server and client or let it remain the way it is . it seems actually the same to me , , just in case ...and the other question is in my main program , i take value from server and client individually , and then invoke a class using those value , will it work like this or not ?.
well i know the question are a bit strange because i am not able to test the program ., well i tried to run this class 2 times in eclipse and at one place i used server and at other i used client , but it didnt work like that .

CODE


import java.io.*;
import java.net.*;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class final_class {

    /**
     * @param args
     */
    private static int factor;
private static int terrain;
    private static int score1=0,score2=0;
    private static int[] t1,t2;
    private static int i,r1;
    static pla p1,p2;
    public void final_class(){}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        t1=new int[5];t2=new int[5];
        for(i=0;i<5;i++){t1[i]=0;t2[i]=0;}
        
        Thread t=new CreateGui();
    t.start();
    for(i=0;i<5;i++)
      {while(p2 == null) {  
                     try {  
                         Thread.sleep(1000L);  
                     }  
                     catch (Exception e) {}  
                 }  
                 // ok the 2 frames where created  
                 // now we can wait on both factor  
                 while(p1.terrain == -1 || p2.terrain == -1) {  
                     System.out.println("We wait because one of the factor still == -1");  
                     try {  
                         Thread.sleep(1000L);   // sleep 1 second  
                     }  
                     catch (Exception e) {}  
                 }  
                 // OK both factor do not equals -1 anymore  
                 System.out.println("Ready to process: terrain = " + p1.terrain + " factor = " + p2.terrain);  
                 if(t1[p1.terrain-1]==1 || t2[p2.terrain-1]==1)
                 {
                System.out.println("wrong choice, cant choose same parameters again ");    i=i-1;
                 }
                 else{
                     mxpro r=new mxpro(p1.terrain,p2.terrain);
                     score1=score1+r.res1;
                     score2=score2+r.res2;
                 t1[p1.terrain-1]=1;t2[p2.terrain-1]=1;
                 }
                
                
}
    score sc;
     sc=new score(score1,score2);sc.setVisible(true);
    
     p2.closeclient();
     p1.closeserver();
         }
          static class CreateGui extends Thread{
                public void run(){
                    identi rr=new identi();
                    rr.setVisible(true);
                    while(rr.x==-1){try {  
                         Thread.sleep(1000L);   // sleep 1 second  
                     }  
                     catch (Exception e) {}  
                 }  
                if(rr.x==1){p1=new pla("terrain",rr.x);
                p1.setVisible(true);}
                else if(rr.x==0)
                {
                p2=new pla("factor",rr.x);p2.setVisible(true);}
                }
}
}  


CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class identi extends JFrame implements ActionListener{
    private JPanel xx=new JPanel();
private JButton b1=new JButton("server");
private JButton b2=new JButton("cliente");
int x=-1;
public identi(){
    super("choose between server et cliente");
    setBounds(220,200,500,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setContentPane(xx);
    xx.setVisible(true);
    xx.add(b1);
    xx.add(b2);
    b1.setVisible(true);
    b2.setVisible(true);
    b1.addActionListener((ActionListener)this);
    b2.addActionListener((ActionListener)this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1){x=1;}
if(e.getSource()==b2){x=0;}
}
}


CODE

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

class pla extends JFrame implements ActionListener
{
private JPanel x=new JPanel();
private JButton b1=new JButton("1");
private JButton b2=n