7 Replies - 777 Views - Last Post: 22 March 2011 - 07:13 AM Rate Topic: -----

#1 paulocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-February 10

Accessing swing components from refactored class

Posted 22 March 2011 - 04:18 AM

Hi there, I've recently finished a texas hold 'em poker game. Not being an experienced programmer I was delighted with the end result only to be told by my supervisor that it needed serious refactoring. My main class which essentially is my GUI had over 1600 lines of code ha.
So I've had to start refactoring the business side of things into their own classes etc. I've created a class called PokerHand which deals with the flow of a hand and extends my main class (PokerGame) my problem now is from Hand I call my method in PokerGame getPlayerBet which essentially just displays buttons for the user to make their decision but this now does not work I'm guessing cos the method is being called from a different class. (The method does actually get called just doesn't update the GUI).
I've had similar issue with this on other variables but was able to avoid it my making the variables static where needed. As my swing button components were built with the netbeans GUI builder my swing components are not editable to make them static (if indeed this would be the solution) so can anyone advise on a solution? I'm aware it may be a bad design flaw but all the refactoring alone at the mo is tedious enough with out a major overhaul, messy as it was originally it did work.

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing swing components from refactored class

#2 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Accessing swing components from refactored class

Posted 22 March 2011 - 05:14 AM

Do not use the Netbeans GUI builder as swing is simple enough to write by hand because the code looks messy and it's uneditable as I'm sure you noticed. Also, I'm not sure how your display works, but your main class should extend JFrame and other classes may extend other JComponents to fit your needs. The idea is to create objects which can be changed, thus getting rid of the need for static variables and the like as making everything static is ugly and ruins the point of OOP. For extending other JCompponents, you may want a class that extends JPanel that you can edit uniquely so you don't have to make calls to methods across classes which gets really messy.
Was This Post Helpful? 1
  • +
  • -

#3 paulocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-February 10

Re: Accessing swing components from refactored class

Posted 22 March 2011 - 06:07 AM

Thanks for the reply TF but forgive me for not quite understanding. Are you saying that I should have a class like UpdateGUI or something? I would still have the same problem trying to access my swing components from there I think. My main class does extend JFrame (it's too late in the day to be trying to do withour the netbeans GUI builder) and the idea is that the all the main class will do is drive the game on. Is my mistake in having my new class PokerHand extend PokerGame? That's why I made some variables static cos I was having two instances of some variables. I might not be explaining it very well so I'll give a quick overview of code....

public class Poker extends javax.swing.JFrame {

PokerHand newHand;

}

// later I have a method startGame which is called from a startGame button event

 private void startGame()
    {
        
        gameDealer = new PokerDealer();
        newHand = new PokerHand();
        updateGUIAtBeginningOfHand();
        newHand.setUpNewHand();
        newHand.postTheBlinds();
        upDateGUIAtBlindsPosting();
        theDealer.dealPlayerCards();
        upDateGUIAfterDealingPlayerCards();
        newHand.handleTheBlindBets();
        System.out.println("end of start a hand");
}

// Also in PokerGame class is the method for allowing the user to make their move
public void getPlayerBet()
    {
  
            callButton.setVisible(true);
            foldButton.setVisible(true);
            raiseButton.setVisible(true);
            checkButton.setVisible(false);        
            
     }


// the PokerHand class has methods that deals with the flow of a hand including...
public class Hand extends Poker{

void handleTheBlindBets() {


    
        if (player1.IsDealer())
        {
           getPlayer1Bet();
        }
            else
            {
              getPlayer2Bet();
            }
    
    }
  




Obviously this is a much abbreviated down version I'm only trying to show what might be relevant. My thinking is that PokerHand has to extend PokerGame or it won't be able to run in tandom, is this where I'm going wrong?
Was This Post Helpful? 0
  • +
  • -

#4 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Accessing swing components from refactored class

Posted 22 March 2011 - 06:33 AM

In simple terms, the class that holds the buttons should hold the method to show/change them and that should be seperate from your main class.
Why does PokerHand extend PokerGame (I believe this is your main class which extends JFrame according to your definition) as PokerHand should just manage cards, correct? Nothing should extend your main class as your main class is where everything will be put together and run.
What I meant by something extending JPanel is just simply for easier access and formatting. A class should extend JPanel and in that class are the buttons to be displayed, that way you, in your main class, can create a new object of that class which extended JPanel and can modify that JPanel object. The object can also be added directly to your main class as the object is a JComponent. The method to display the buttons should be in the class that extends JPanel also. You can then utilize anything from those buttons in the JPanel class in your main class along with anything else you use in your main class.
I hope that clears things up a little.
Was This Post Helpful? 1
  • +
  • -

#5 paulocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-February 10

Re: Accessing swing components from refactored class

Posted 22 March 2011 - 06:56 AM

Ok thanks TF I think I have a clearer picture now, what appears to be my problem is that PokerGame is my main class (another nasty side effect of using the netbeans GUI!) and I was extending it because this was where my objects such as Player, Dealer, etc. were instanstiated so I should keep the GUI seperate to everything else and just update it when need be? PokerHand doesn't just deal with cards it is the class that determines the flow of a game so does this sound like a more logical place for my main class? Or should I write another class specifically as my main class?
Was This Post Helpful? 0
  • +
  • -

#6 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Accessing swing components from refactored class

Posted 22 March 2011 - 07:00 AM

As it's set up, the main class should have 2 purposes, to extend JFrame and hold everything together. You want all of the work and processing to be done in other classes and then create objects which will be formatted together in the main class :). Or there's another way to do it which is do the processing in the main class, but still with objects of other classes which will each hold their own unique data. That's the basic idea.
Was This Post Helpful? 1
  • +
  • -

#7 paulocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-February 10

Re: Accessing swing components from refactored class

Posted 22 March 2011 - 07:09 AM

That's great TF thanks a mill, think the second way is more or less how I originally had it set up so I might try to replicate that again. Fair play to you and everyone else helping on this great site! :clap:
Was This Post Helpful? 0
  • +
  • -

#8 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Accessing swing components from refactored class

Posted 22 March 2011 - 07:13 AM

Glad to be of help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1