how do i reset the applet?
i thought that it would work if i call the method itself

thanks

private class ButtonListener1 implements ActionListener{
		public void actionPerformed(ActionEvent e){
			myButton1.setBackground(Color.black);
			int answer = JOptionPane.YES_OPTION;
			answer = JOptionPane.showConfirmDialog(null,
					"BOOOoooM!!! Would you like to play again?", "", JOptionPane.YES_NO_OPTION);
			if(answer == JOptionPane.YES_OPTION){				
				LoanApplet reset = new LoanApplet();
				reset.reset();
			}
			else{
				//p1.setVisible(false);
			}
		
		}
	}
public void reset(){
        
		JPanel p1 = new JPanel(new GridLayout(3, 3));
		int randomNumber = (int)(Math.random()*9)+1;
		
		if(randomNumber == 1){
			p1.add(myButton1);
			p1.add(myButton2);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		if(randomNumber == 2){
			p1.add(myButton2);
			p1.add(myButton1);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		add(p1, BorderLayout.CENTER);
		
		myButton1.addActionListener(new ButtonListener1());
		myButton2.addActionListener(new ButtonListener2());
}

Recommended Answers

All 18 Replies

Hi there, I think you will need to write your own reset method:

public void reset(){
//reset code
//in here write code to reset applet to its orginal form
}

i dont quite understand what kind of codes do i need to reset applet

could you tell me more?

Well, I presume that at some point in your code your applet is initialised and all the variables are set up. You need your Reset function to re - set these variables to their initial states, without knowing what they are from the above code I cannot help you more - supply some more code and I will try to assist.

i'm not sure if i did this right

/**
 * @author Noradila Nordin
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoanApplet extends JApplet {
	
	JButton myButton1 = new JButton("1");
	JButton myButton2 = new JButton("2");
	JButton myButton3 = new JButton("3");
	JButton myButton4 = new JButton("4");
	JButton myButton5 = new JButton("5");
	JButton myButton6 = new JButton("6");
	JButton myButton7 = new JButton("7");
	JButton myButton8 = new JButton("8");
	JButton myButton9 = new JButton("9");
	
	boolean state1 = false;
	boolean state2 = false;
	boolean state3 = false;
	boolean state4 = false;
	boolean state5 = false;
	boolean state6 = false;
	boolean state7 = false;
	boolean state8 = false;

	public void init(){
		
		JOptionPane.showMessageDialog(null, "Welcome to Mini-MiniSweeper!\n" +
				"The object of the game is to click on all\n" +
				"the squares EXCEPT the one with the bomb.\n" +
				"(There is only one bomb). To choose a square\n" +
				"to display please simply click on the square.", "Message", JOptionPane.INFORMATION_MESSAGE);

		JPanel p1 = new JPanel(new GridLayout(3, 3));
		int randomNumber = (int)(Math.random()*9)+1;
		
		if(randomNumber == 1){
			p1.add(myButton1);
			p1.add(myButton2);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		if(randomNumber == 2){
			p1.add(myButton2);
			p1.add(myButton1);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		add(p1, BorderLayout.CENTER);
		
		myButton1.addActionListener(new ButtonListener1());
		myButton2.addActionListener(new ButtonListener2());
	
	}
	
	
	public class ButtonListener1 implements ActionListener{
		public void actionPerformed(ActionEvent e){
			myButton1.setBackground(Color.black);
			int answer = JOptionPane.YES_OPTION;
			answer = JOptionPane.showConfirmDialog(null,
					"BOOOoooM!!! Would you like to play again?", "", JOptionPane.YES_NO_OPTION);
			if(answer == JOptionPane.YES_OPTION){				
				LoanApplet reset = new LoanApplet();
				
				reset.reset();
				state1 = false;
				state2 = false;
				state3 = false;
				state4 = false;
				state5 = false;
				state6 = false;
				state7 = false;
				state8 = false;
			}
			else{
				System.exit(0);
			}
		
		}
	}
	
	public class ButtonListener2 implements ActionListener{
		public void actionPerformed(ActionEvent e){
			myButton2.setBackground(Color.white);
			state1 = true;
			if(state2 == true && state3 == true && state4 == true && state5 == true && state6 == true && state7 == true && state8 == true){
					
				int answer = JOptionPane.YES_OPTION;
				answer = JOptionPane.showConfirmDialog(null, "You win! Play again?", "", JOptionPane.YES_NO_OPTION);
			}
			
		}
	}
	
	public void reset(){
        
		JPanel p1 = new JPanel(new GridLayout(3, 3));
		int randomNumber = (int)(Math.random()*9)+1;
		
		if(randomNumber == 1){
			p1.add(myButton1);
			p1.add(myButton2);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		if(randomNumber == 2){
			p1.add(myButton2);
			p1.add(myButton1);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		add(p1, BorderLayout.CENTER);
		
		myButton1.addActionListener(new ButtonListener1());
		myButton2.addActionListener(new ButtonListener2());

	}
}

Does the grid show when you compile and run it on your machine ?

The buttons I mean?

buttons from the init() method? yes
reset? no

it doesn't call reset

I am a bit stumped here buddy, when I run it, 9 out ten times the buttons don't show?

All of this portion could go into a method called setupGame()

JPanel p1 = new JPanel(new GridLayout(3, 3));
		int randomNumber = (int)(Math.random()*9)+1;
		
		if(randomNumber == 1){
			p1.add(myButton1);
			p1.add(myButton2);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		if(randomNumber == 2){
			p1.add(myButton2);
			p1.add(myButton1);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);
		}
		
		add(p1, BorderLayout.CENTER);
		
		myButton1.addActionListener(new ButtonListener1());
		myButton2.addActionListener(new ButtonListener2());

Keep an instance-level reference to the panel which is to contain the buttons. Before the statement that creates the panel, place a call to remove that panel first like so

if (gamePanel != null){
          remove(gamePanel);
      }
      gamePanel = new JPanel(new GridLayout(3, 3));

(Yes, I renamed the panel "gamePanel" because "p1" means absolutely nothing. Use meaningful names for variables so you remember what they are for.)

As for the button code, they really could share the same listener since they do the same thing. Also, you can just create an array of JButton[] to hold them and randomize the index selection to place them on the screen instead of hard-coding all of that.

commented: This guy is the best! +2

Ezzaral, you are a genius. I have been trying to figure this out for Adila for ages!

i cut out this part because it's too long lol that's why the buttons dont show up each time

if(randomNumber == 1){
			p1.add(myButton1);
			p1.add(myButton2);
			p1.add(myButton6);......

it should be
if(randomNumber == 1){
if(randomNumber == 2){ etc

as said.. i could redo it as Ezzaral said which is better

i'm not sure if i understand this correctly..

JPanel p1 = new JPanel();
public void init(){
		
		JOptionPane.showMessageDialog(null, "Welcome to Mini-MiniSweeper!\n" +
				"The object of the game is to click on all\n" +
				"the squares EXCEPT the one with the bomb.\n" +
				"(There is only one bomb). To choose a square\n" +
				"to display please simply click on the square.", "Message", JOptionPane.INFORMATION_MESSAGE);
		setupGame();

	}
	
	
	public class ButtonListener1 implements ActionListener{
		public void actionPerformed(ActionEvent e){
			myButton1.setBackground(Color.black);
			
			int answer = JOptionPane.YES_OPTION;
			answer = JOptionPane.showConfirmDialog(null,
					"BOOOoooM!!! Would you like to play again?", "", JOptionPane.YES_NO_OPTION);
			if(answer == JOptionPane.YES_OPTION){				
					setupGame();
			}
			else{
				System.exit(0);
			}
		
		}
	}
public void setupGame(){
		if (p1 != null){
			remove(p1);
		}
			p1 = new JPanel(new GridLayout(3, 3));
		//JPanel p1 = new JPanel(new GridLayout(3, 3));
		
		int randomNumber = (int)(Math.random()*9)+1;
		
		if(randomNumber == 1){
			p1.add(myButton1);
			p1.add(myButton2);
			p1.add(myButton3);
			p1.add(myButton4);
			p1.add(myButton5);
			p1.add(myButton6);
			p1.add(myButton7);
			p1.add(myButton8);
			p1.add(myButton9);

it doesn't reset..
which part did i misunderstood?

Ok, several small things. In the setupGame() method, after you add the panel to the container, you need to call validate() for it to update it's component layout (this will also cause a repaint so you don't have to worry with that)

add(p1, BorderLayout.CENTER);
validate();

Second, you are setting the background colors on the buttons to white or black in the ActionListeners, but you don't set them back in setupGame(). Those will need to be reset to their default color. Additionally, you add the button listeners to the buttons in setupGame(), which means they get added each time you call that method. If you call it three times you end up with three listeners, all doing the same thing, on the buttons. This means the action will fire three times for a single click - not what you want. Add the listeners to the buttons just once in some other method. Perhaps a setupButtons() method or in init() for the moment.

It might reflect the program flow a little better if you had a setupButtons() method to build the buttons and attach the listeners. You may want a resetButtons method that changes all the background colors back to the start color. Then rename setupGame() to startGame() (yes, I'm picky on naming methods according to exactly what they do). Remove the code that adds the listeners from that code and place a call to resetButtons() before you place the buttons on the panel.

As far as managing the entire game state, here are some things to consider that would make it much easier on you if you wish to tackle them.

1) Use an array for all of the buttons

JButton[] buttons;

Your setup code just needs to loop that array and create a new button for each element.

2) You only need one button listener for all of the buttons. The only part of the code that differs is the response for a bomb or a win.

public class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            boolean gameOver = false;
            JButton clickedButton = (JButton)e.getSource();
            if(clickedButton==bombButton) {
                // you got the bomb - see #3 on this
                ...
                gameOver = true;
            } else { // for you to figure out

The part for you figure out is how to tell a win. Hint: The number of plays (button clicks) will equal the number of buttons if you only count clicks on buttons that haven't been played before.

3) A single reference for the current bomb button. Since you have all of the buttons in an array, you can choose a random number against the array length and assign that button to a reference for that game

JButton bombButton;

On a new game, choose a new random array index and set the bombButton to that button.

bombButton = buttons[ yourRandomInt ];

If you do those things, you don't have to randomize the actual buttons at all. They all share the listener and one has been chosen to randomly be the bomb each game. This means you don't even need to remove the panel and rebuild it each game. Just reset the background colors and choose a new bomb.

Have fun.

commented: You are the main source of accurate help in the Java forum +6

If I stick to the way that I did, how could I not keep on adding the buttonlistener each time it repaints?

Move the code that creates the buttons into the init() method and add the listener after you create the button. Leave the variables at the instance level where you have them, but put the myButton1 = new JButton("1"); part in init() so it only gets executed once.

Doesn't that don't change where the bomb is first located?

No, it will still generate a random number because you put that in init() too:

int randomNumber = (int)(Math.random()*9)+1;

yay! got it!

thanks :D :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.