Solved

How to restart Java GUI application?

Posted on 2009-04-07
7
771 Views
Last Modified: 2013-11-23
So I ran into this problem while creating a game.  When the user loses he's given the option to play again.  If he says yes, I have to set everything as a new game.

How do I go about doing this?

I mean, is there a simple way to do it?  I have like 100 object and it'd be ridiculous to make new objects.
0
Comment
Question by:ubuntuguy
  • 4
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
hadesflames5855 earned 500 total points
ID: 24093209
try putting it in a while loop. when the game is over, the user is asked to start again, if the user says yes, you start the loop over, else you end the loop and the program is over.
while(a){
//game code
String x = JOptionPane.showInputDialog(null,"Would you like to play again?");
if(x.equalsIgnoreCase("no")
   a = false;
}
JOptionPane.showMessageDialog("Thank you for playing!");

Open in new window

0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 24093218
that solution seems to complicated for what i have.  got a game with like 3000 lines of code.  it'll just confuse me.
0
 
LVL 1

Expert Comment

by:hadesflames5855
ID: 24093265
not really, you would put the while loop in the main method, and set the frame visiable inside the loop, along with the rest of the game code and pretty much everything.

also instead of the above code also, do this:
while(a){
//game code
Object[] x = {"Yes","No"};
		int y = JOptionPane.showOptionDialog(null, "Would you like to play again?", "My Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, x, x[0]);
if(y == 1)
  a = false;
}
JOptionPane.showMessageDialog("Thank you for playing!");

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:ubuntuguy
ID: 24093305
In my main method?

This is my main method.... I ask the user if they want to keep playing in the game class... so not sure how I would do it inside the main method.


 
public class BattleshipApp {
 
 
 
	public static void main(String[] args) {
		
			
			GameBoard g = new GameBoard();
			g.setVisible(true);
			g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
		
			
		}
 
	}

Open in new window

0
 
LVL 1

Expert Comment

by:hadesflames5855
ID: 24093320
try this and tell me if it works:


while(a){
GameBoard g = new GameBoard();
g.setVisible(true);                  g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
Object[] x = {"Yes","No"};
int y = JOptionPane.showOptionDialog(null, "Would you like to play again?", "My Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, x, x[0]);
if(y == 1)
  a = false;
}
JOptionPane.showMessageDialog("Thank you for playing!");

Open in new window

0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 24093544
Thanks for your help. You code kept showing the would you like to play again message in an infinite loop.  I had to turn in my project already so I just sent what I had.  Thanks for your help again.
0
 
LVL 1

Expert Comment

by:hadesflames5855
ID: 24093884
well, did you have a declared as boolean a = true; before the while? I assumed you would do so yourself...I tested this code myself and does not lead to an infinite loop....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 64
tomcat administrtor 12 61
runtime exception 2 41
passing enum to a method 4 20
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question