Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why won't my reset button work?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why won't my reset button work?

    [INDENT][INDENT][INDENT][INDENT]This matching game should be reset to a new game with different colours, but it does not? Can someone help

    (sorry not button, it should be function)

    OOCW2Last.java

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
     
    public class OOCW2Last {
     
    	public static final int WIDTH = 4;
    	public static final int HEIGHT = 4;
    	public static final int COLOURS = 4;
    	public static final int MAX_PER_COLOUR = 4;
    	public static final int BUT_SIZE = 100;
    	int player1Score = 0;
    	int player2Score = 0;
    	int butNumber1 = 0;
    	int butNumber2 = 0;
    	public int turn = 1;
    	boolean player1 = true;
    	boolean matchedColours;
    	int i = 0;  
    	JLabel top = new JLabel("Player 1");
    	JLabel bottom = new JLabel("Player 2");
     
    	Color[] colors = new Color[8];
     
    	ColorButton[] arrayButtons = new ColorButton[WIDTH*HEIGHT];
    	Random rand = new Random();
    	JFrame mainGUI = new JFrame();
    	JPanel panelMain = new JPanel();
    	boolean firstRun = true;
     
    	public static void main(String[] args) 
    	{
     
    		OOCW2Last main = new OOCW2Last();
    		main.createGUI();
    	}
     
    	public void createGUI()
    	{
     
    		top.setBackground(Color.GREEN);
    		top.setOpaque(true);
     
    		bottom.setBackground(Color.RED);
    		bottom.setOpaque(true);
     
    		panelMain.setLayout(new GridLayout(WIDTH,HEIGHT));
     
    		initButtons();
     
    		mainGUI.getContentPane().add(top, BorderLayout.NORTH);
    		mainGUI.getContentPane().add(bottom, BorderLayout.SOUTH);
    		mainGUI.getContentPane().add(panelMain,BorderLayout.CENTER);
     
    		mainGUI.pack();
    		mainGUI.setResizable(false);
    		mainGUI.setVisible(true);
    		message(bottom,top);
    	}
     
    	public void initButtons() {
     
    		System.out.println("reset");
     
    		int x = MAX_PER_COLOUR;
    		int y = WIDTH*HEIGHT;
    		int z;
     
    		for (int i = 0; i < WIDTH*HEIGHT; i++) 
    		{
    			arrayButtons[i] = new ColorButton(i, BUT_SIZE, BUT_SIZE, this);         
    			if(firstRun)
    				panelMain.add(arrayButtons[i]);
    				arrayButtons[i].setButtonFin(false);
    				arrayButtons[i].setButtonState(0);
    				arrayButtons[i].setBackground(Color.gray);
    				arrayButtons[i].colorPicked = false;
    		}
     
    		colors[0] = Color.blue;
    		colors[1] = Color.yellow;
    		colors[2] = Color.cyan;
    		colors[3] = Color.red;
    		colors[4] = Color.orange;
    		colors[5] = Color.green;
    		colors[6] = Color.white;
    		colors[7] = Color.pink;
     
    		for (int i = 0; i < COLOURS; i++)
    		{
    			while(x > 0)
    			{
    				z = rand.nextInt(y);
     
    				while (arrayButtons[z].colorPicked == true){
    					z = rand.nextInt(y);
    				}
     
    				arrayButtons[z].realButtonColor = colors[i];   //get the real colours for the button from the array
    				arrayButtons[z].colorPicked = true;
    				arrayButtons[z].flip(false);				//set all colours to grey front
    				x --;
    			}
    			x = MAX_PER_COLOUR;
    		}
    	}
     
    	public void reset(){
    		initButtons();
    		player1Score = 0;
    		player2Score = 0;
    		turn = 0;
    		player1=true;
    		message(bottom, top);
     
    	}
     
    	public int getTurn(){			// this gets the turn and returns it to ColorButton
    		return turn;
    	}
    }
    Last edited by Norm; April 2nd, 2019 at 10:13 AM. Reason: Added ending code tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why won't my reset button work?

    Please post code with proper indentations. Statements should not all start in the first column.

    Also add some comments to the code that document what it is trying to do and how it is going to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Why won't my reset button work?

    And please don't remove your code this time. It makes the thread hard to follow for future readers.

    Regards,
    Jim

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why won't my reset button work?

    Second time the OP has removed code. This time a small section was left. I'm on the verge of banning the OP for a while.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Why won't my reset button work?

    And I specifically asked the OP not to and explained why.

Similar Threads

  1. [SOLVED] Getting a calculate button to work, and clear button to reset everything.
    By Ancharius in forum AWT / Java Swing
    Replies: 3
    Last Post: April 3rd, 2014, 05:20 PM
  2. Code help with reset button error
    By Razorfc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2012, 02:20 PM
  3. Replies: 3
    Last Post: October 4th, 2011, 09:03 PM
  4. The program working fine but the "NO" button doesn't work
    By ahcenpg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2011, 07:32 PM
  5. Reset button.
    By ish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 07:57 AM