7 Replies - 6612 Views - Last Post: 12 October 2011 - 07:10 AM Rate Topic: -----

#1 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Main menu and few questions.

Posted 12 October 2011 - 04:57 AM

I have created the basics of my game, however I would like to add a main menu (Play games, options, high scores etc).

Lets say I want a menu directing me to the game, high scores, and options. I was thinking that I could do this by having 3 JPanels open and bringing the appropriate one to the front when I need it (can I do that?) or is there another way that would give better results?

My issue with that thought is that going back to the main, I would need to restart the game. However I think I can fix that by resetting current variables to default and then just repainting the game as necessary. But to do this, I need to know how to destroy classes as such (JPanels, monster class etc) or is setting them to 'null' essentially achieving the same thing? Then I just re-instantiate them

-----------------------------------

My other 2 questions are:

If I assign something like this:

public void foo() {
   Block[] tiles = new Block[];
   tiles[1] = new blockpiece(); // How do I reference new block()  //blockpiece extends block
   if (tiles[1] == ???) {      // <-- in here, since it doesn't have an instance name?
     ...                       // I want to check if tiles[1] is a blockpiece
   }
}



---------------------------------------------

and Lastly:

How do I call a variable/method in the Main class of my application. (Ignore syntax).
Class Main { 
   public int x;     // How do I call this from another class?

   public static void main(... {
   }
}



Sorry if this was too much, I don't like to make a lot of threads.

This post has been edited by Mylo: 12 October 2011 - 05:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Main menu and few questions.

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Main menu and few questions.

Posted 12 October 2011 - 06:20 AM

Three panels in a card layout sounds like it would work for you. To destroy a JPanel, you have to remove all references to the object. This includes setting any of your variables that point to it to null, removing it from your JFrame, if it also implements event listener interfaces, remove them too.
It's also good practice to call dispose() on the panel too.

You might find it easier to reset the panel rather than destroy it.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Main menu and few questions.

Posted 12 October 2011 - 06:32 AM

If you separate your program state and GUI, this shouldn't be necessary to kill off JComponents. If you are just using JButtons in your grid, just set their Icons back to the default and reset your data/state side of things. Handling GUI cleanup by removing JComponents, disposing them, etc., yourself is almost always more work than it's worth. :)
Was This Post Helpful? 1
  • +
  • -

#4 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Main menu and few questions.

Posted 12 October 2011 - 06:38 AM

@cfoley - I've never used that layout, but it does look useful. I'll give it a go and see how it works out. If resetting is easier, I'll do that. You opened me a new range of functions. I do not see dispose() on my panels list, but I'll find it :) Thanks for your help.

@macosxnerd - I'm not quite sure what you mean by seperating program state and GUI, could you perhaps elaborate? and yes, I think resetting values would probably be easier.

This post has been edited by Mylo: 12 October 2011 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Main menu and few questions.

Posted 12 October 2011 - 06:41 AM

Quote

I do not see dispose() on my panels list

There is no real reason to dispose() any JComponents here. Avoid trying to dispose() on your JPanels. :)

Quote

@macosxnerd - I'm not quite sure what you mean by seperating program state and GUI, could you perhaps elaborate? and yes, I think resetting values would probably be easier.

So your Minesweeper program has certain state/data elements like where the mines are located, the number of surrounding mines, the high score, the size of the grid, whether or not the user has won/lost, etc. This should all be in its own class, separate from the GUI. I would design a singleton StateManager or DataManager class to store this information.

The GUI should just get/set the appropriate data from the StateManager or DataManager object and display the results. It shouldn't be directly managing this information.
Was This Post Helpful? 1
  • +
  • -

#6 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Main menu and few questions.

Posted 12 October 2011 - 06:53 AM

That makes sense, thanks. Stuff like this was much easier in visual studio with the visual aid. But I just have to learn more java. I think I'll give it a go, but if I can't manage it, I'll leave it for a later time. Thanks :)

Any suggestions regarding problem #2? Don't worry about #3, I have no need for it, I couldn't remove it because my edit option was removed.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Main menu and few questions.

Posted 12 October 2011 - 07:04 AM

For the second problem, you should look into the keywords, instance of. When you are extending this it is a common thing to check, which object is what. So you can compare it to instance of blockPiece
Was This Post Helpful? 1
  • +
  • -

#8 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Main menu and few questions.

Posted 12 October 2011 - 07:10 AM

View PostFuzzyness, on 12 October 2011 - 07:04 AM, said:

For the second problem, you should look into the keywords, instance of. When you are extending this it is a common thing to check, which object is what. So you can compare it to instance of blockPiece


That worked perfectly :) Thank you very much.

Thank you everyone who helped. I have learned a lot of things just from the few questions I have asked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1