Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Is there a better way to do this?  (Read 739 times)
0 Members and 1 Guest are viewing this topic.
Offline Eyesackery

Junior Devvie


Medals: 1



« Posted 2012-10-15 06:50:16 »

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! Cry

Game Frame
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
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?)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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);
     
   }
}
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #1 - Posted 2012-10-15 08:50:19 »

Have you looked at CardPanel?

As for instantiating game state objects I actually have a class called GameState that holds everything I need regarding the game.  Then I instantiate it and hold a reference within my main frame instance.  Thus it's accessible wherever I need it.

Naturally the GameState class doesn't include GUI code, loading/saving, etc... just the raw data that represents the guts of the game itself.

My golden rule is "get it working first".  Then refactor if it smells really bad or if you think of a better way to do it later on.
Offline Eyesackery

Junior Devvie


Medals: 1



« Reply #2 - Posted 2012-10-15 13:16:20 »

Have you looked at CardPanel?

As for instantiating game state objects I actually have a class called GameState that holds everything I need regarding the game.  Then I instantiate it and hold a reference within my main frame instance.  Thus it's accessible wherever I need it.

Naturally the GameState class doesn't include GUI code, loading/saving, etc... just the raw data that represents the guts of the game itself.

My golden rule is "get it working first".  Then refactor if it smells really bad or if you think of a better way to do it later on.

Cheers for the response Loom! Smiley I have had a look at the CardPanel layout manager earlier tonight, but I think after working on this for like 10 hours straight my ability to retain any sort of information is very limited! haha

I manage to get everything working, then look at the code, and re-do the code to make it more readable and cleaner, which is a endless cycle.

I think I have to study more good examples of class structure? (Am I even making sense?) haha, I think it's bed time.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (16 views)
2014-12-28 10:39:27

CopyableCougar4 (20 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (116 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!