Results 1 to 10 of 10
  1. #1
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default Using Event handlers functions to change attributes used to test conditions

    hey all. this question is probably a little bit stupid because i only started using swing/awt yesterday but here it goes:
    i'm programming a card game and in it i have a package for the user interface which basicaly uses a class called MainFrame and has the following attributes:

    Java Code:
    public class MainFrame extends JFrame implements MouseListener{
    
    	private MainPanel _mainPanel=new MainPanel();
    	private ArrayList<CardPanel> _panels=new ArrayList<CardPanel>();
    	private ArrayList<CardPanel> _trick=new ArrayList<CardPanel>();
    	private String _playerName=null;
    	private Arbiter _arbiter;
    	private int _id=-1;
    	private Player _realPlayer;
    	private boolean _takeALook=true;
    the important ones for this issue are the _takeALook and _id ones because i have the following method in this class:

    Java Code:
    private void play(){
    		for(int i=0;i<4;++i){
    			if(((_arbiter.getActivePlayer()+i)%4)==_arbiter.getRealPlayer()){
    				_realPlayer=_arbiter.getPlayer((_arbiter.getActivePlayer()+i)%4);
    				while(_id==-1){}//important
    				_arbiter.getPlayer(0).playCard(_id);
    				_trick.get(0).setCard(_id);
    				_id=-1;
    			}
    			else{
    				Card c=_arbiter.getPlayer((_arbiter.getActivePlayer()+i)%4).playCard();
    				_trick.get((_arbiter.getActivePlayer()+i)%4).setCard(c.getId());
    			}
    		}
    		while(_takeALook){}//important
    		for(CardPanel panel:_trick){
    			panel.removeCard();
    		}
    		_takeALook=true;
    	}
    the important parts are those while cycles that seem infinite because those attributes don't change in this method. BUT, that was my intention because i only want those whiles to stop when those attributes change due to a MouseEvent. Here is the method to deal with it:

    Java Code:
    public void mouseClicked(MouseEvent e){
    		Component c=e.getComponent();
    		CardPanel cp=(CardPanel) c;
    		for(CardPanel panel:_panels){
    			if(cp.equals(panel)){
    				_id=cp.getId();
    				return;
    			}
    		}
    		for(CardPanel panel:_trick){
    			if(cp.equals(panel)){
    				_takeALook=false;
    			}
    		}
    	}
    (again don't try to understand the whole code). the point is those attributes are changed only when some event occurs (that's what i wanted).

    so, here is the problem: when i run the app it doesn't stop when it reaches the first while... :( (obviously without the MouseEvent happening or I wouldn't have this problem).

    what am i doin wrong?

    ps:the cardPanels where it is supposed to click with the mouse are not even showing up, so they never get clicked (another problem i got to solve (: )...
    Last edited by Sneaky Fox; 01-20-2011 at 12:52 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Sneaky Fox View Post
    (again don't try to understand the whole code)....
    Myself, I'd be better able to help if I understood your code better.


    the point is those attributes are changed only when some event occurs (that's what i wanted).
    And this is the whole point of event-driven programming, but your code isn't written in an event-driven way. A better way to do this is to use listeners, such as a MouseListener or if a JButton an ActionListener, and have your program react to the user in the listener, and nature of this reaction will depend on the state of the program (the values held by the an object's fields).


    so, here is the problem: when i run the app it doesn't stop when it reaches the first while... :( (obviously without the MouseEvent happening or I wouldn't have this problem).

    what am i doin wrong?
    You're calling an infinite loop on the EDT or event dispatch thread, Swing's main thread, effectively blocking the thread and preventing Swing from painting the GUI or interacting with the user. For more on the EDT, please look here: Concurrency in Swing

    For more specific help, please give us more information on your problem and your code, and perhaps an SSCCE.

    Much luck!
    Last edited by Fubarable; 01-20-2011 at 05:28 AM.

  3. #3
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    i'll post here the all the gui code so that you can understand it better. if you see any reference to Arbiter,Player,Card objects, those are objects from the application's core and their meaning is pretty obvious except for the Arbiter class.
    the Arbiter class is similar to a Facade of the core of the program,meaning that almost everything the gui does to interact with the core goes through the Arbiter.

    like i said before the program has other issues such as not printing the CardPanels etc. so everything you see i'm doing wrong please explain it to me.
    thank you in advance
    the code is in a zip file attached to this post
    Attached Files Attached Files

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Sneaky Fox View Post
    i'll post here the all the gui code so that you can understand it better.
    Sorry to be blunt, but I don't have the time to wade through a whole bunch of code, and I doubt any one else here will either. Again, you're far better off creating and posting an SSCCE and a detailed explanation of the problem than you are posting the whole shebang.

    Luck.

  5. #5
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    i understand, though it's only 5 simples classes but thx anyway

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again, your problem is your coding in a non-event driven very linear way and freezing the swing thread with your while (true) loops. You need to not try to create a linear game loop, but rather have your program respond to events such as mouse clicks or button presses. Use your class's fields to modify how the program behaves when an event occurs.
    Last edited by Fubarable; 01-20-2011 at 03:17 PM.

  7. #7
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    nevermind already solved the while cycle problem...

  8. #8
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    my problem now is another one i'll post in a different topic

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Sneaky Fox View Post
    nevermind already solved the while cycle problem...
    Please let everyone know how you solved it as your solution could help someone else in a similar situation who searches for and finds this thread.

  10. #10
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    the solution was simple. i was stupid cause i didin't notice that the initial value of the string that the mainFrame was asking form the mainPanel wasn't null (and i thought it was).that's why the cycle wasn't infinite as i wanted to.
    just lack of attention by myself

Similar Threads

  1. while and proper conditions for if
    By Saletra in forum New To Java
    Replies: 11
    Last Post: 08-25-2010, 10:37 AM
  2. Help with event to change output value in a division
    By benjibabs in forum New To Java
    Replies: 4
    Last Post: 05-07-2010, 11:29 AM
  3. Replies: 0
    Last Post: 03-26-2009, 01:23 AM
  4. Datepicker Calender - month change event - jquery
    By rmpatel101 in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2008, 06:42 PM
  5. Help with Event Handlers
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •