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!
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(){
} 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
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"); ....
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
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); }
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
/** * @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
// 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){
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
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 .
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); } } }
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.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; }