Results 1 to 10 of 10
- 01-20-2011, 12:50 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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:
the important ones for this issue are the _takeALook and _id ones because i have the following method in this class: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 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: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; }
(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).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; } } }
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.
-
Myself, I'd be better able to help if I understood your code better.
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).the point is those attributes are changed only when some event occurs (that's what i wanted).
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 Swingso, 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?
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.
- 01-20-2011, 01:48 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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
-
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.
- 01-20-2011, 03:03 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
i understand, though it's only 5 simples classes but thx anyway
-
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.
- 01-20-2011, 03:33 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
nevermind already solved the while cycle problem...
- 01-20-2011, 03:34 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
my problem now is another one i'll post in a different topic
-
- 01-20-2011, 04:01 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
while and proper conditions for if
By Saletra in forum New To JavaReplies: 11Last Post: 08-25-2010, 10:37 AM -
Help with event to change output value in a division
By benjibabs in forum New To JavaReplies: 4Last Post: 05-07-2010, 11:29 AM -
[SOLVED] Change text colour on conditions in netbeans
By dbashby in forum New To JavaReplies: 0Last Post: 03-26-2009, 01:23 AM -
Datepicker Calender - month change event - jquery
By rmpatel101 in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 06:42 PM -
Help with Event Handlers
By Daniel in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks