I work on a GUI Blackjack game in java.
I took some ideas from this site, from Dogstopper blog about game state machines.
this is how I implement my abstaract class for the game state:
package blackjack;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
public abstract class BlackJackState implements ActionListener{
protected BlackJackPanel root;
protected JPanel controlPanel;
protected BlackJackState(BlackJackPanel root){
this.root = root;
this.controlPanel = new JPanel();
this.root.add(controlPanel, BorderLayout.SOUTH);
this.root.setBackground(new Color(100, 150, 60));
this.controlPanel.setBackground(this.root.getBackground());
}
public abstract void draw(Graphics g);
public void clearCurrentControlPanel(){
this.controlPanel.setVisible(false);
}
}
now, my question considers the controlPanel Object I create.
you see, for every state in the game I'll have different JComponents to add (different buttons, different sliders etc.)
now, I create the controlPanel inside each of the state classes, and add it to the "root" Object I get as parameter.
then, I add the components in the state classes.
when I switch between the states i use the setVisible(false) on the current controlPanel and add the new one.
Now this approach works fine. My question However is: what do you think on such design? is it safe? is it a good design?
Or should I somehow create the controlPanel only inside the BlackJackPanel class?
I'll be happy to hear any ideas! Thanks!

New Topic/Question
Reply
MultiQuote






|