Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implement showOptionDialog - create new game  RSS feed

 
peter meyer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am working on a game Minesweeper.
In there I have implemented a showMessageDialog and I would like to replace it with a showOptionDialog.
The problem is, to define the correct action after clicking Yes or No...

Thats how it is now with the showMessageDialog:


How do I have to implement the showOptionDialog to create a new game when the user clicks Yes and quit the application when the user clicks No?


I appreciate any advise and help...
I can post the whole section of the class if the code extract is not enough (was not allowed to add it as attachment).

Thank you
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the API JOptionPane#showConfirmDialog returns an int which indicates the user choice. The available choices are defined as static constants in the JOptionPane class itself. e.g. YES_OPTION, CANCEL_OPTION
Check the value of the choice and proceed accordingly.

Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
 
peter meyer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thank you Maneesh...
I already read the recommended oracle article. Because of that I was able to implement a little bit of the showOptionDialog.

When I say No or cancel the dialog the application is closing; so far so good.
I struggle to rebuild the application if I say Yes on the dialogue. The application quits as well when I say Yes (play again)...
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
peter meyer wrote:I struggle to rebuild the application if I say Yes on the dialogue. The application quits as well when I say Yes (play again)...


Must be something to do with the changes you made, then. If you're still stuck would you care to post that part of the new code?
 
peter meyer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul taking time for this. Here is the code:


(don't get confused with some German expressions or names in the code).

Thanks
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (explodiert==1)
That looks as if you were writing C code where you use 0 for false and non‑zero for true. You don't in Java┬«. You either
  • 1: Make explodiert a boolean in which case you would write if (explodiert)...     or
  • 2: You create an enum to match explodiert so you might write if (explodiert == SEVERELY)... or if (explodiert == SLIGHTY)... or if (explodiert == NOT_AT_ALL)...
  • Note you should put spaces around the == operators.
    Another thing I can see is that your method is much much too long. Your if statements are difficult to follow and you will be unable to understand any of them in six months. That method should be divided into much smaller methods. Or you should think of a more object‑oriented solution. I am afraid I can't think of one at present, but you can do all sorts of things. You can cover the screen with multiple buttons, each representing a particular location. I presume you have Mine and Minesweeper classes. Think of how to match a Mine to its location. Do you have any method for recording location on your screen?

    More about ==.
    Never write == true and == false which are poor style and error‑prone because you might write = instead by mistake.
    Never
    if (b == true)...
    Always
    if (b)...
    Never
    if (b == false)...
    Always
    if (!b)...
    Notice the bang sign ! in the last statement.
     
    peter meyer
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Richie...
    Well it seems I will go back to an earlier version I was working on...
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!