14 Replies - 796 Views - Last Post: 01 June 2008 - 06:39 AM Rate Topic: -----

#1 killer_beast  Icon User is offline

  • New D.I.C Head

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

help with Jbuttons type

Posted 31 May 2008 - 05:18 AM

well this is my query , i dont know how to pass the value provided by clicking a button to the main program. well whenever i run the program it puts the result "factor= 0" before even me clicking the button and if i click the button it does nothing in the executable program. well this is a part of a bigger program . i have to pass the value of factor to another class which will perform other functions. so i basically need to pass that value to the executable class.


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

 class final_class {

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

}



This post has been edited by killer_beast: 31 May 2008 - 07:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: help with Jbuttons type

#2 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: help with Jbuttons type

Posted 31 May 2008 - 06:51 AM

[/quote]


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





[/quote]

The main method is the first thing that will load up in a JAVA application.. and you call the System.out.println factor thus printing Immediately the "Facto = 0" output.. well thats fine..

Quote


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;}
	   System.out.println("Fac " + factor);
}



you should add a System.out.println method in actionPerformed event

This post has been edited by mensahero: 31 May 2008 - 07:15 AM

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: help with Jbuttons type

Posted 31 May 2008 - 07:47 AM

well thx for help mensahero but thats not what i was looking for , i have modified the initial post to make the problem more clear. maybe there s some way to make the computer wait until it receives a value for factor . but i have no idea how to do it .

This post has been edited by killer_beast: 31 May 2008 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: help with Jbuttons type

Posted 31 May 2008 - 08:21 AM

View Postkiller_beast, on 31 May, 2008 - 07:47 AM, said:

well thx for help mensahero but thats not what i was looking for , i have modified the initial post to make the problem more clear. maybe there s some way to make the computer wait until it receives a value for factor . but i have no idea how to do it .


well I thought you are actually asking for something simple.. well first you may want to make your executable class or whatever you call it Public.. and declaring a public method that will pass the variable..

for example.. heres a revision of your code assuming that you Final_class is the main executable class.. I'm new to Java so correct me if this is a wrong sample...


Final_class should be in a separate file..because it is a public class..
public class final_class {

	/**
	 * @param args
	 *
	 */

	int factor; //Final Class version of Factor variable
	
   // no argument constructor
	public void final_class(){
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int factor;
		pla2 p2=new pla2();
		p2.setVisible(true);
	factor=p2.choix(); System.out.println("fac "+factor);

	}

	 //sample public method for passing variables into a class
	 public void passVar(int f){
		 factor = f;
		 System.out.println(factor);
	 }


}






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;

final_class fc = new final_class(); //declare an instance of the 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.passVar(factor); // public method of final_class
	
}
public int choix(){return factor;}
}






hope that helps.. goodluck..
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: help with Jbuttons type

Posted 31 May 2008 - 03:08 PM

well the trick you have shown me surely works , is a very nice one indeed . but my problem have become surely more complicated with it , i am trying to make a two player game because of it ,i have one more class like factor and it returns me a same kind of value . and these two combined evoke a new class which calculates the score depending on these two numbers i received and also whio have won or not .
Was This Post Helpful? 0
  • +
  • -

#6 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: help with Jbuttons type

Posted 31 May 2008 - 11:57 PM

View Postkiller_beast, on 31 May, 2008 - 03:08 PM, said:

well the trick you have shown me surely works , is a very nice one indeed . but my problem have become surely more complicated with it , i am trying to make a two player game because of it ,i have one more class like factor and it returns me a same kind of value . and these two combined evoke a new class which calculates the score depending on these two numbers i received and also whio have won or not .


:P :P :^: Thanks..


well.. IMO thats not very hard.. if you want a value that can be shared by two instances of a class.. just declare that value as static variable...

example:

static factor

just add "static".. well correct me if I'm wrong.. I'm new to JAVA.. :blink:
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: help with Jbuttons type

Posted 01 June 2008 - 01:29 AM

View Postmensahero, on 31 May, 2008 - 11:57 PM, said:

:P :P :^: Thanks..


well.. IMO thats not very hard.. if you want a value that can be shared by two instances of a class.. just declare that value as static variable...

example:

static factor

just add "static".. well correct me if I'm wrong.. I'm new to JAVA.. :blink:


hi well i understand what you are saying but it does not work in this case you might like to see the whole problem and solution that i received in my other post of the same problem http://www.dreaminco...wtopic53490.htm

you will see the reason s there as to why declaring it to static doesnt work ;)
Was This Post Helpful? 0
  • +
  • -

#8 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: help with Jbuttons type

Posted 01 June 2008 - 02:01 AM

well.. i'm referring to my Example.. if you actually make it static it will work..
well here a new revision of your code in this thread..

final class
public class final_class {

	/**
	 * @param args
	 *
	 */

	static int factor;

	public void final_class(){

	}

	public static void  main(String[] args) {
		// TODO Auto-generated method stub
		int factor;
		pla2 p2=new pla2();
		p2.setVisible(true);
	factor=p2.choix(); System.out.println("fac "+factor);

		pla2 p1 = new pla2();
		p1.setLocation(500,0);
		p1.setVisible(true);

		

	}


	 public void passVar(int f){
		 factor += f;
		 System.out.println(factor);
	 }

	 public void setFactor(int f){
		 factor = f;
	 }
}



player class
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;

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

	System.out.println("a");
}
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.passVar(factor);
	
}
public int choix(){return factor;}
}





well that will create two GUI for p1 and p2.. if you press the button in p1 it will increment the variable factor.. and if you press the butotn in p2 it will increment the previously increment variable factor..

meaning 2 classes are actually referencing a value from a single static variable.. That code is not meant to work in your project for its only purpose is to give you an IDEA on how to achieve such a task..

I'm reviewing your code in your posted thread link and I can see that your actually suffering from a bad program design..

a bad program design is very complicated to develop.. so IMO you should re structure your design.. insisting on your current design will only make your program more complicated..

a good program design is VERY EASY TO DEVELOP and never COMPLICATED.. and thats a fact..

lastly.. when I said redesign I don't mean start a new project.. just re structure your current code.. object by object..


..WOOT..!!! I've read that you actually want to create a multiplayer network game that will be played in two different PC connect via LAN.. well.. IMO you're very far away from achieving that.. but it's not impossible.. hmm.. just wondering did you code all of that? your codes in the other thread link..

This post has been edited by mensahero: 01 June 2008 - 02:20 AM

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: help with Jbuttons type

Posted 01 June 2008 - 02:42 AM

well i already told you that your example works really fine . there is no doubt in it . well about the bad program structure if you have seen the other thread carefully , i really cant think of a better way for the moment as i have three objects that return value and i have to have them all in the main program but i think the last program with threads and sleep would work .
and yeah i did program all that myself
Was This Post Helpful? 0
  • +
  • -

#10 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: help with Jbuttons type

Posted 01 June 2008 - 03:21 AM

View Postkiller_beast, on 1 Jun, 2008 - 02:42 AM, said:

well i already told you that your example works really fine . there is no doubt in it . well about the bad program structure if you have seen the other thread carefully , i really cant think of a better way for the moment as i have three objects that return value and i have to have them all in the main program but i think the last program with threads and sleep would work .
and yeah i did program all that myself



well.. I'm referring to the mx1 Class... I just cant imagine how you possibly came up with that Class without even testing it if it works.. a typical nOOb like me would possibly do it by trial and error.. testing it with a little lines of code and if works.. then develop it by adding some lines of codes until I came up with that kind of class.. your mx1 type of class..

thats the reason why I ask if you actually wrote it.. you wrote that class but you don't know how to use it.. I just don't get it.. no offense meant..

this proves it..

Quote

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

just making a point..

and by actually looking at your code.. factor is not actually a variable.. it is the name of player 1 while terrain is player 2.. so basically your code which I quoted doesn't make any sense.. :blink:

well I'm kind of interested in what exactly that mx1 class could do.. so I'm playing with you're code and want to see it for myself..

well I hope I didn't offend you or something.. and I would notify you for any progress.. I stop my lesson on concurrency just because I got interested in that mx1 class.. lmao.. curiosity.. :blink:

well heres a fast update.. I really don't know what you are trying to accomplish. but this revision worked.. because I finally saw what mx1 class could do.. and I'm very disappointed..

import javax.swing.*;

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

static int factor;
final_class fc = new final_class();

public pla(){
	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;}

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

		}

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

		pla p1=new pla();
		pla p2=new pla();
		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

//

	}
}


 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) {

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


	}}



well.. if you could explain what the mx1 class should be doing.. then I might still be interested in it..

This post has been edited by mensahero: 01 June 2008 - 03:28 AM

Was This Post Helpful? 0
  • +
  • -

#11 killer_beast  Icon User is offline

  • New D.I.C Head

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

Re: help with Jbuttons type

Posted 01 June 2008 - 03:59 AM

View Postmensahero, on 1 Jun, 2008 - 03:21 AM, said:

View Postkiller_beast, on 1 Jun, 2008 - 02:42 AM, said:

well i already told you that your example works really fine . there is no doubt in it . well about the bad program structure if you have seen the other thread carefully , i really cant think of a better way for the moment as i have three objects that return value and i have to have them all in the main program but i think the last program with threads and sleep would work .
and yeah i did program all that myself



well.. I'm referring to the mx1 Class... I just cant imagine how you possibly came up with that Class without even testing it if it works.. a typical nOOb like me would possibly do it by trial and error.. testing it with a little lines of code and if works.. then develop it by adding some lines of codes until I came up with that kind of class.. your mx1 type of class..

thats the reason why I ask if you actually wrote it.. you wrote that class but you don't know how to use it.. I just don't get it.. no offense meant..

this proves it..

Quote

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

just making a point..

and by actually looking at your code.. factor is not actually a variable.. it is the name of player 1 while terrain is player 2.. so basically your code which I quoted doesn't make any sense.. :blink:

well I'm kind of interested in what exactly that mx1 class could do.. so I'm playing with you're code and want to see it for myself..

well I hope I didn't offend you or something.. and I would notify you for any progress.. I stop my lesson on concurrency just because I got interested in that mx1 class.. lmao.. curiosity.. :blink:

well heres a fast update.. I really don't know what you are trying to accomplish. but this revision worked.. because I finally saw what mx1 class could do.. and I'm very disappointed..

import javax.swing.*;

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

static int factor;
final_class fc = new final_class();

public pla(){
	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;}

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

		}

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

		pla p1=new pla();
		pla p2=new pla();
		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

//

	}
}


 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) {

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


	}}



well.. if you could explain what the mx1 class should be doing.. then I might still be interested in it..


well the mx1 class is like that when you click start you load the terrain here you have to reach the small rectangle in the lower part with the mouse without touching any bar, the rectangles , i didnt program the part for it to return the value of win to tell the user whether i have won or not , thats why it is making no sense now .i have 5 terrain like this depending on the level of difficulty selected using the value of terrain. the whole idea is like this one player selects the terrain , for a difficult terrain you have maximum score and the other player selects the factor which will be multiplied to the score to give the new score if he wins he get that much score added if he loses he gets score deducted .well i have tested it ,the one who have higher score wins; what were you thinking i will take such a risk . this is the test class i made earlier .:)
and if you look carefullt factor and terrain are definately variables in class pla1 and pla2
import javax.swing.*;

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

class mx 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("you won ");private int win=1;
	private JTextField zz1=new JTextField("you lost");
	public mx()
	{	
		setBounds(100,100,900,500);
	setBackground(Color.black);
	setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//getContentPane().setLayout(null);
		getContentPane().add(x);
		//x.setLayout(null);
		x.add(start);x.add(fin);
		//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,180,650,5);/*g2.drawRect(101,210,660,5);g2.drawRect(150,240,650,5);g2.drawRect(101,270,660,5);
				g2.drawRect(150,300,650,5);g2.drawRect(101,330,660,5);
				g2.drawRect(150,350,650,5);//g2.drawRect(101,350,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]>=180 && b[i]<=185){System.out.println("you lose");score--;System.out.println(score);}
				/*	if(a[i]>=101 && a[i]<=761 && b[i]>=210 && b[i]<=215){System.out.println("you lose");score--;System.out.println(score);}
					if(a[i]>=150 && a[i]<=800 && b[i]>=240 && b[i]<=245){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]>=300 && b[i]<=305){System.out.println("you lose");score--;System.out.println(score);}
					if(a[i]>=101 && a[i]<=761 && b[i]>=330 && b[i]<=335){System.out.println("you lose");score--;System.out.println(score);}
					if(a[i]>=150 && a[i]<=800 && b[i]>=350 && b[i]<=355){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]>=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);
			}
	if(e.getSource()==fin){if(win==1) {x.add(zz);System.out.println("you have won");} else if(win==0) {System.out.println("you have losT");x.add(zz1);}}

	}
	public void score(){if(win==1) {x.add(zz);System.out.println("you have won");} else if(win==0) {System.out.println("you have losT");x.add(zz1);} }	
}

public class sma {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
int x;
		mx r=new mx();
			
	r.setVisible(true);
	r.score();
	}

}



Was This Post Helpful? 0
  • +
  • -

#12 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: help with Jbuttons type

Posted 01 June 2008 - 05:40 AM

yes they are variables that represent player 1 and player 2.. and PBL was right you can actually create a single class pla and make two instances of it..


IMO.. the game is not actually that complicated.. its actually the way you design your program that makes it very complicated.. well yes it runs.. and I kind of play with it for I while.. its really hard not to touch those borders.. lmao..

well ill see what I can hopefully do.. well I'm very new to JAVA so please don't put to much hope in me.. if ever.. thanks.. and it wont possibly be a LAN game.. so stick with the two player game that run on a SINGLE PC..

tip: IMO in your type of program you don't actually need to pass anything to the main class.. and your program is event driven so processess can actually happen anywhere. IMO you can even put your score processing in the mx1 class..


why are there 1-5 buttons in the first JFrame.. ? whats its purpose? since the game is actually played using a mouse moving along the borders without touching it.. so why the buttons?

and how does the game exactly works.. step by step..

ex: the Game start with two JFrame... terrain and factor.. (well since there is only one mouse they have to play alternately) so terrain chooses a number from 1-5.... then so on.. explain it briefly.. so that it could be easily understood..

This post has been edited by mensahero: 01 June 2008 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

#13 killer_beast  Icon User is offline

  • New D.I.C Head

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

Re: help with Jbuttons type

Posted 01 June 2008 - 06:20 AM

View Postmensahero, on 1 Jun, 2008 - 05:40 AM, said:

yes they are variables that represent player 1 and player 2.. and PBL was right you can actually create a single class pla and make two instances of it..


IMO.. the game is not actually that complicated.. its actually the way you design your program that makes it very complicated.. well yes it runs.. and I kind of play with it for I while.. its really hard not to touch those borders.. lmao..

well ill see what I can hopefully do.. well I'm very new to JAVA so please don't put to much hope in me.. if ever.. thanks.. and it wont possibly be a LAN game.. so stick with the two player game that run on a SINGLE PC..

tip: IMO in your type of program you don't actually need to pass anything to the main class.. and your program is event driven so processess can actually happen anywhere. IMO you can even put your score processing in the mx1 class..


why are there 1-5 buttons in the first JFrame.. ? whats its purpose? since the game is actually played using a mouse moving along the borders without touching it.. so why the buttons?

and how does the game exactly works.. step by step..

ex: the Game start with two JFrame... terrain and factor.. (well since there is only one mouse they have to play alternately) so terrain chooses a number from 1-5.... then so on.. explain it briefly.. so that it could be easily understood..


hey i want to tell you its not completed in the main class i will add serversocket and other functions like that i cant tell you a lot as i have to myself know how to use it . and i have to make a lan connection as it is for my compuetr project and the main idea of that project is to make a program to have connection between two computers so that they can exchange information. i dont think you understood that exactly , i have 5 classe Mx1, Mx2..mx5 each represent a level of difficulty.so if i choose terrain 1,2..5 i choose a different class mx1..mx5.thats why there are so many buttonS and i think i will need to do something so that the user uses all the buttons and have the score generated well if you dont want dont spend that much time on it , because i am not sure of certain things myself ,i think i have enough info to finish it . well if i finish it or have any query i will post it here.:)

This post has been edited by killer_beast: 01 June 2008 - 06:22 AM

Was This Post Helpful? 0
  • +
  • -

#14 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: help with Jbuttons type

Posted 01 June 2008 - 06:38 AM

:D :D :^: :^: good luck :^: :^: :D :D

well thats really a big project.. and I bet its really hard.. :^: :^:
:D :D well back to my own project.. :D :D
takecare..
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: help with Jbuttons type

Posted 01 June 2008 - 06:39 AM

View Postkiller_beast, on 1 Jun, 2008 - 06:20 AM, said:

have 5 classe Mx1, Mx2..mx5 each represent a level of difficulty.so if i choose terrain 1,2..5 i choose a

Again not a good idea... why not havong a class MX which receives as parameter the level of difficulty (1-5) ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1