stuck with code

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 1933 Views - Last Post: 04 June 2008 - 10:37 PM Rate Topic: -----

#1 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

stuck with code

Posted 31 May 2008 - 03:37 PM

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


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;}
}



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;}
}




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;
		}
			} 
	}
}












Is This A Good Question/Topic? 0
  • +

Replies To: stuck with code

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: stuck with code

Posted 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

class pla extends JFrame implements ActionListener

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



and then in your main

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


Was This Post Helpful? 0
  • +
  • -

#3 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

Re: stuck with code

Posted 31 May 2008 - 04:43 PM

View Postpbl, on 31 May, 2008 - 04:10 PM, said:

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

class pla extends JFrame implements ActionListener

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



and then in your main

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
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: stuck with code

Posted 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:

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

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);
	}


Was This Post Helpful? 0
  • +
  • -

#5 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

Re: stuck with code

Posted 31 May 2008 - 05:26 PM

View Postpbl, on 31 May, 2008 - 04:57 PM, said:

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:

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

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

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;}
}


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.
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 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: stuck with code

Posted 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

Or this will be an HORRIBLE design

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 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

#7 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

Re: stuck with code

Posted 31 May 2008 - 05:55 PM

View Postpbl, on 31 May, 2008 - 05:40 PM, said:

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
.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: stuck with code

Posted 31 May 2008 - 06:03 PM

Look I have edited my previous post but is is NOT a good solution
Was This Post Helpful? 0
  • +
  • -

#9 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

Re: stuck with code

Posted 31 May 2008 - 06:14 PM

View Postpbl, on 31 May, 2008 - 06:03 PM, said:

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 .
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: stuck with code

Posted 31 May 2008 - 07:18 PM

OK you want the Horrible solution

This is the shortest I could make it


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);
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

#11 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

Re: stuck with code

Posted 04 June 2008 - 03:11 PM

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 .

 

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);}
				}
}
}   

 

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;}
}
}



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=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  BufferedWriter OBW = null;
private  BufferedReader IBR = null;
private Socket soc = null;
Socket Csoc;
ServerSocket Ssoc;
int terrain=-1;
// int r;
//final_class fc = new final_class();
public pla(String text,int a){
	//if a=0->cliente		   a=1 -> server
	super(text);
	getContentPane().add(x);
	setSize(300,300);
	setVisible(true);

	{   if(a==0)
	{String server = new String("");
	try 
	{
	 // etablissement de la connexion cote client sur le port 20000/establishing connection with the server at port 20000
	 System.out.print("Etablissement connexion avec le serveur ...");//establishing connection with the server
	 soc = new Socket(server,20000);
	 System.out.println(" Le serveur " + server + " nous a reconnu");//the server have recognised us 

	 // phase de communication avec le serveur ://phase of communication with the server
	 // creation des objets de type InputStream et OutputStream//creation of object of type input and output streme
	 // permettant d'echanger des informations avec le serveur//that allows us to communicate the information with the server
	 InputStream IS = soc.getInputStream();
	 OutputStream OS = soc.getOutputStream();

	
	 IBR = new BufferedReader(new InputStreamReader(IS));
	 OBW = new BufferedWriter(new OutputStreamWriter(OS));
	}
	catch(IOException e)
	  { System.out.println("I/O exception error "); }
   
	}
	else if(a==1)
	{

		String reponse;
		int nbrcar,figure;
		 try 
			{ 
			 // etablissement du service cote serveur sur le port 20000// establishing port at 20000
			 // creation du socket serveur que l'on attache au port 20000// creation of server socket that we have attached at port 20000
			 System.out.print("generating the server at port 20000 ...");
			 Ssoc = new ServerSocket(20000, 180);
			 System.out.println(" OK");
			// phase d'attente d'une connexion avec un client// waiting for a connection with cliente
			System.out.print("waiting for connection with cliente ...");
			Csoc = Ssoc.accept();   
			System.out.println("connection with cliente acheived");

			InputStream IS = Csoc.getInputStream();
			OutputStream OS = Csoc.getOutputStream();

			 IBR = new BufferedReader(new InputStreamReader(IS));
			 OBW = new BufferedWriter(new OutputStreamWriter(OS));
			 
			 reponse = new String(IBR.readLine()); // reading string
			 System.out.println("the client replied that: " + reponse);
			 // i dont think the last thing with the response matter a lot i dont know why i used it 
			}
		 catch(UnknownHostException e) { }
		 catch(IOException e)
			  { System.out.println("Erreur entree/sortie pendant la connexion"); }
	}
	}
	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);
	b1.setVisible(true);b2.setVisible(true);b3.setVisible(true);b4.setVisible(true);b5.setVisible(true);t.setVisible(true);
	
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
}
public void actionPerformed(ActionEvent ev){
	if(ev.getSource()==b1){t.setText("1");terrain=1;}
	if(ev.getSource()==b2){t.setText("2");terrain=2;}
	if(ev.getSource()==b3){t.setText("3");terrain=3;}
	if(ev.getSource()==b4){t.setText("4");terrain=4;}
	if(ev.getSource()==b5){t.setText("5");terrain=5;}
	//fc.passVart(terrain);
	switch(terrain){
	case 1: x.remove(b1);b1.setVisible(false);break;
	case 2: x.remove(b2);b2.setVisible(false);break;
	case 3:x.remove(b3);b3.setVisible(false);break;
	case 4: x.remove(b4);b4.setVisible(false);break;
	case 5: x.remove(b5);b5.setVisible(false);break;
	
	default: break;}
	}

public void closeserver(){
	System.out.print("Enfin, on ferme la communication avec le client ...");//we close the communication with the client
   try{ Csoc.close();}catch(IOException ea){};
	System.out.println(" C'est fait, au revoir le client ...\n");
	System.out.print("\nPuis on arrete le service sur le port 20000 ...");//we close the service at 20000
	try{Ssoc.close();}
   catch(Exception ea){ System.out.println("Erreur d'entree/sortie"); }//io error
}
public void closeclient()
{
	System.out.print("Enfin, on ferme la communication ...");//we close the communication
try{	soc.close();}	
catch(UnknownHostException eaa)
  { System.out.println("le nom du serveur est inconnu"); }//the server name is not recognised
catch(IOException eaa)
  { System.out.println("Erreur entree/sortie pendant la connexion"); }//i/o error during the connection
System.out.println(" OK");

}
}




ublic class mxpro extends Thread{
	int res1,res2;
 mxpro(int t,int f)
 {
switch(t)
{
case 1: mx1 r1=new mx1();			
		r1.setVisible(true);while(r1.win == -1) {  
			System.out.println("We wait because one of the factor still == -1");  
			try {  
				Thread.sleep(1000L);   // sleep 1 second  
			}  
			catch (Exception e) {}  
			if(r1.win==1){res1=r1.win*500*f;res2=0;}else if(r1.win==0){res2=f*500;res1=0;}
		   
		} r1.setVisible(false); break;
case 2: mx2 r2=new mx2();			
		r2.setVisible(true);while(r2.win == -1) {  
			System.out.println("We wait because one of the factor still == -1");  
			try {  
				Thread.sleep(1000L);   // sleep 1 second  
			}  
			catch (Exception e) {}  
			if(r2.win==1){res1=r2.win*400*f;res2=0;}else if(r2.win==0){res2=f*400;res1=0;}
		}r2.setVisible(false);break;
case 3: mx3 r3=new mx3();			
		r3.setVisible(true);while(r3.win == -1) {  
			System.out.println("We wait because one of the factor still == -1");  
			try {  
				Thread.sleep(1000L);   // sleep 1 second  
			}  
			catch (Exception e) {}  
			if(r3.win==1){res1=r3.win*300*f;res2=0;}else if(r3.win==0){res2=f*300;res1=0;}
		}r3.setVisible(false);break;
case 4: mx4 r4=new mx4();			
		r4.setVisible(true);while(r4.win == -1) {  
			System.out.println("We wait because one of the factor still == -1");  
			try {  
				Thread.sleep(1000L);   // sleep 1 second  
			}  
			catch (Exception e) {}  
			if(r4.win==1){res1=r4.win*200*f;res2=0;}else if(r4.win==0){res2=f*200;res1=0;}
		}r4.setVisible(false);break;
case 5: mx5 r5=new mx5();			
		r5.setVisible(true);while(r5.win == -1) {  
			System.out.println("We wait because one of the factor still == -1");  
			try {  
				Thread.sleep(1000L);   // sleep 1 second  
			}  
			catch (Exception e) {}  
			if(r5.win==1){res1=r5.win*100*f;res2=0;}else if(r5.win==0){res2=f*100;res1=0;}
		}r5.setVisible(false);break;
} 	 
 System.out.println("res="+res1);
 }

}


This post has been edited by killer_beast: 04 June 2008 - 04:49 PM

Was This Post Helpful? 0
  • +
  • -

#12 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

Re: stuck with code

Posted 04 June 2008 - 04:58 PM

i have edited my last post to make it more clear what problem i am facing , ca anybody tell me how can i test this server client program , because executing it 2 times in eclipse and choosing one as server and the other as client is not helping .
so i guess either this is not the way or either i have a fatal error in my code .

plz help i am really desperate now to finish with this code, i am working on it for some days and its now getting on my nerves.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: stuck with code

Posted 04 June 2008 - 05:05 PM

I say it is not a good solution because:

a good design fire up and event and the code respond to the event
in that case you write a Thread that periodically check (burning CPU) if a state has changed

It is like if instead of having in your car an accelerator pedal that throws more and less fuel to the engine you'll have an engine that runs at a certain speed and checks periodically if the accelerator pedal has changed its position and react accordingly

Wont drive this kind of car in a race when you want the accelerator to react in a fraction of second

This post has been edited by pbl: 04 June 2008 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#14 killer_beast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-May 08

Re: stuck with code

Posted 04 June 2008 - 05:34 PM

View Postpbl, on 4 Jun, 2008 - 05:05 PM, said:

I say it is not a good solution because:

a good design fire up and event and the code respond to the event
in that case you write a Thread that periodically check (burning CPU) if a state has changed

It is like if instead of having in your car an accelerator pedal that throws more and less fuel to the engine you'll have an engine that runs at a certain speed and checks periodically if the accelerator pedal has changed its position and react accordingly

Wont drive this kind of car in a race when you want the accelerator to react in a fraction of second



ok i understand what you are saying, but right now i am willing to move in any type of car that can help me reach my destination :D , i know i have taken lots of help from you but can i ask for some more , can you plz check whether what i have done is good or not , i have mentioned what are the problems i am facing in the last two posts .
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: stuck with code

Posted 04 June 2008 - 05:43 PM

View Postkiller_beast, on 4 Jun, 2008 - 05:34 PM, said:

ok i understand what you are saying, but right now i am willing to move in any type of car that can help me reach my destination :D , i know i have taken lots of help from you but can i ask for some more , can you plz check whether what i have done is good or not , i have mentioned what are the problems i am facing in the last two posts .


Is your code posted the latest version ?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3