Results 1 to 9 of 9
  1. #1
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Question Update Method inside a GUI?

    Hey all, this question seems so simple yet I haven't done anything like this before and the answer is currently eluding me... I have built a game of Texas Holdem in the command line and am now trying to apply this to a GUI (my first). I've made a simple GUI with the buttons, text fields, etc and I am in the middle of trying to move everything over to action events from these.

    But before this, I realize that I will need an update method that runs even when there is no action being done by the user to show what other players are doing... I'm just not sure how to create an update method in the same thread as the GUI without bringing the GUI to a dead stop. If anything is unclear, please just let me know and I'll try to clarify. Thanks in advance for the help.

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

    Default

    I'm not clear on what you're trying to do. If you want the computer to play, but want to slow down it's play so that it appears that another human is playing, then you might want to use a Swing Timer. Regardless, you'll want to be sure to separate out your program's logic from the GUI portion as much as possible -- look up MVC or Model View Controller pattern for more suggestions on how to do this.

  3. #3
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Sorry, I'm having trouble finding the right words...

    It seems to me that all of the swing elements run once and then in the background listen for action. This means that my GUI class is called once and then it doesn't run again. Next, I have a class called 'Texas Holdem' which has all the logic for the game. There are two things I am currently trying to do with this:
    1. Continuously update the screen (IE if another player bets, the 'to call' amount changes, or if there is a bet, the button says 'call' rather than 'check')
    2. Run the game.

    I know that I can do something like:
    do{ texasHoldem.Play() }(while !quit); to go through the logic of the game but then my GUI never updates, its just a white screen because its stuck in the above loop. So my question is, how can I update my GUI while playing the game without having 2 threads?

    I say without 2 threads because, while I haven't worked with threading before, it seems a. complicated, and b. it seems to reduce my ability to pass variables. I need to be able to send the player actions from the GUI back into my game.

    I hope this makes things more clear? Thanks again.

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

    Default

    I'm still trying to get a better grasp of your program and I think that I'd at least still be helped by knowing more about your game's architecture: Is this a one-person game? Multiple players? If multiple, are they all on the same computer? Or are they playing on a network? Is the computer taking the place of one or more players? What is a typical game play? What is the general appearance of the GUI? How should it respond when it's the player's turn? When not the player's turn?

    Sorry to keep asking questions, but I find that the better any of us can understand a problem, usually the better solutions we can offer.

  5. #5
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Not a problem, I appreciate the help.

    It is currently a single player game playing against computers. If you don't know, Texas Holdem is a form of poker so the turns are sequential.

    My basic layout thus far is this:

    CardGames.java - the main class, currently starts the GUI
    ComputerPlayer.java - computer AI for its turns
    DeckBuilder.java - constructs the deck of cards
    GUI.java - where I have created my user interface... As of earlier today, this was purely the GUI, now I am attempting to integrate the game into it.
    HandRanking.java - methods that determine the hand quality/ranking
    HumanPlayer.java - this is where I was previously getting player input with an InputStreamReader
    Player.java - superclass of HumanPlayer and ComputerPlayer, contains methods and primitives common to both
    Shuffle.java - two methods that shuffle the deck
    TexasHoldem.java - where most of my logic takes place -this keeps track of bets, player and community cards, and more. Before creating my GUI, this class was run from the main class and created multiple players and each of the other classes listed above to run the game. The biggest part of this are two methods - Play() which initializes everything and then calls Run() which is in an infinite loop until the player chooses to quit that executes the shuffling, dealing, betting, etc.

    So what I am trying to do now is merge TexasHoldem.java, which contains a loop for player interaction, with my GUI that executes once.

    (I don't know if this will help or be more confusing but..) What I'm doing right now is:
    GUI----->

    ---->TexasHoldem----->
    | |
    <----------------------

    where each acts independently
    What I want to do is

    --------->GUI-------->
    | |
    <----TexasHoldem-----

    where I update the GUI and pass any events to TexasHoldem which then processes these events and plays the computers turns before giving control back to GUI to update the screen for changes until the player makes an action again that needs to go to TexasHoldem.


    Right now there are multiple computer players with a basic GUI that has:
    3 buttons: check(call)/bet(raise)/fold
    1 JFormattedTextField to input the bet(raise) amount

    1 JLabel Icon acting as a table - future plans to add cards as well

    1 JTextArea with a scroll bar that the action will currently be displayed in addition to (future, if it gets that far) player chat


    When it is the players turn, it needs to listen for any of the buttons to be pressed
    When it isn't the players turn, it needs to update the actions of each of the computers and present all of that information in the JTextArea

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

    Default

    So if there is one human player and 3 computer players, I imagine that once the human plays his turn, the 3 computer players will perform their turns very quickly, almost instantaneously, and then it will be the human player's turn again. If not, if the processing required by the computer to do its turns takes a bit of time, then this absolutely must be done in a background thread, such as a SwingWorker. If it is very fast, but you want to give the impression of other human's playing, and thus will require a delay in each computer player's turn, then you won't need to do background threading directly, but rather can simply use a Swing Timer for the delay (which uses a background thread itself -- but you don't have to worry about that aspect). In the mean while, I think that it's always a good idea to disable any buttons or components that should not be interacted with, and enable them only when it is time for the user to interact. So you might want to attach a listener to your model in some way to notify the GUI when a turn occurs, and when it's the human user's turn. So if you haven't done so, you might want to look at the Observer Design Pattern for a general idea of this, and perhaps a PropertyChangeListener for a specific way to handle this via a PropertyChangeSupport field in the model.

    It sounds like a very interesting project and it looks like you've put some decent effort in thinking this out and in separating the functionality of your GUI and your model.

  7. #7
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks again. I've been looking through books and online on a way to get separate threads to talk to each other but I can't find a way to do so cleanly.

    Based on what you've said and I've been thinking, I will need separate threads - one for the GUI and one for the program logic. I just can't figure out a good way to pass information back and forth between the two threads. I know this is somewhat contrary to what I was trying to do earlier but it seems like this may be the best way.

    Thanks.

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

    Default

    I don't know that you really need separate threads, but this is just based on a superficial review of your description, so I could be wrong. I'm guessing that the computer will calculate computer turns very quickly. And per my understanding of threading (which is still a work in progress) you don't pass information between threads, you pass it between objects if that makes sense.

  9. #9
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    I may not but I can't figure out how to get around it... I have something that looks like

    do{
    TexasHoldem.play()
    while}(!quit);

    inside my GUI class (I have currently placed it inside to make it easy for the GUI to change information based on user input).

    But then my GUI never updates.

    Conversely, if I remove the do loop, then my GUI runs fine but my gameplay only runs once.

    What I guess I'm looking for is some sort of update method that will allow me to update everything.

Similar Threads

  1. pass value inside method A to method B
    By masokis in forum New To Java
    Replies: 5
    Last Post: 09-01-2010, 05:06 AM
  2. Unable to call paint() from inside method
    By Y. Progammer in forum New To Java
    Replies: 3
    Last Post: 03-01-2010, 01:47 PM
  3. Replies: 10
    Last Post: 02-28-2010, 02:52 PM
  4. Replies: 1
    Last Post: 02-27-2010, 01:35 AM
  5. Replies: 2
    Last Post: 04-03-2009, 06:04 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
  •