I’m trying to come up with a type of ‘menu screen’ system, but really have no idea how to go about it. What I have so far is a JPanel that receives user information, then hides itself when it has done so.
The idea is to hide and reveal JPanels as they are needed.
Also in the game I’m having trouble figuring where is the best place to create objects, especially the user and puzzle objects. Should I design a specific class that does all object creation? Or should I just create them on the fly?
Class design and structure has definitely been my Achilles heel in learning OO programming! :’(
Game Frame
public class GameFrame extends JFrame {
public static final int GAMEWIDTH = 640;
public static final int GAMEHEIGHT = 480;
public GameFrame() {
super("Sudoku || Calkuro");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(GAMEWIDTH, GAMEHEIGHT);
setResizable(false);
addPanel(new TitlePanel(), BorderLayout.CENTER);
}
public void addPanel(GamePanel panel, String layout) {
add(panel, layout);
panel.createPanel();
setVisible(true);
}
}
Game Panel
public class TitlePanel extends GamePanel implements ActionListener {
private static final long serialVersionUID = 1L;
public static final int PANEL_WIDTH;
public static final int PANEL_HEIGHT;
public InputManager input;
private JPanel dataInput;
private JLabel titleLabel, userLabel, puzzleLabel, difficultyLabel;
private JTextField userTextField, puzzleTextField;
private JButton userLoginButton;
private JComboBox puzzlesBox, difficultiesBox;
static {
PANEL_WIDTH = GameFrame.GAMEWIDTH;
PANEL_HEIGHT = GameFrame.GAMEHEIGHT;
}
public TitlePanel(){
String[] puzzles = {"Sudoku", "Calkuro"};
String[] difficulties = {"Easy", "Medium", "Hard"};
puzzlesBox = new JComboBox(puzzles);
difficultiesBox = new JComboBox(difficulties);
dataInput = new JPanel();
input = new InputManager();
titleLabel = new JLabel();
userLabel = new JLabel();
puzzleLabel = new JLabel();
difficultyLabel = new JLabel();
puzzleTextField = new JTextField();
userTextField = new JTextField(" ");
userLoginButton = new JButton("Login");
}
public void createPanel() {
titleLabel.setFont(new Font("arial", Font.BOLD, 50));
titleLabel.setForeground(Color.white);
titleLabel.setText("SUDOKU || CALKURO");
userLabel.setText("Username: ");
userLabel.setForeground(Color.white);
puzzleLabel.setText("Puzzle:");
puzzleLabel.setForeground(Color.white);
difficultyLabel.setText("Difficulty:");
difficultyLabel.setForeground(Color.white);
userLoginButton.addActionListener(this);
this.setSize(PANEL_WIDTH, PANEL_HEIGHT);
this.setBackground(Color.DARK_GRAY);
this.add(titleLabel);
dataInput.setBackground(Color.DARK_GRAY);
dataInput.setLayout(new GridLayout(5, 5));
dataInput.add(userLabel);
dataInput.add(userTextField);
dataInput.add(puzzleLabel);
dataInput.add(puzzlesBox);
dataInput.add(difficultyLabel);
dataInput.add(difficultiesBox);
dataInput.add(userLoginButton);
this.add(dataInput);
}
@Override
public void actionPerformed(ActionEvent e) {
Loader.createUser(userTextField.getText());
Loader.createPuzzle(puzzlesBox.getSelectedItem().toString(), difficultiesBox.getSelectedItem().toString());
this.setVisible(false);
}
@Override
public void update() {}
@Override
public void handleInput() {}
}
The “Loader” Class - (Is this even logical?)
public class Loader {
private static Puzzle sudokuPuzzle;
private static Puzzle calkuroPuzzle;
private User user;
public static void createPuzzle(String puzzleName, String difficulty) {
if (puzzleName.equals("Sudoku"))
sudokuPuzzle = new SudokuPuzzle(difficulty);
if (puzzleName.equals("Calkuro"))
calkuroPuzzle = new CalkuroPuzzle(difficulty);
}
public static void createUser(String userName) {
User user = new User(userName);
}
}