For some reason my program will not draw the buttons, also I have a comment put in for a line of code that I do not know how to implement.
Any help is appreciated
Main.java import javax.swing.JOptionPane; public class Main { public static void main( String[] args ) { int clicks = 0; int matched = 0; boolean live = true; int choice1 = 0, choice2 = 0; GUI mainGameWindow = new GUI(); GLogic gameLogic = new GLogic(mainGameWindow.getAmountOfButtons()); mainGameWindow.setVisible( true ); while( live ) { ++clicks; //wait for action in mainGameWindow if(clicks % 2 == 1) { choice1 = mainGameWindow.getButtonClicked(); mainGameWindow.setButtonText(choice1, Character.toString(gameLogic.getCharacter(choice1))); mainGameWindow.disableButton(choice1); } else { choice1 = mainGameWindow.getButtonClicked(); mainGameWindow.setButtonText(choice2, Character.toString(gameLogic.getCharacter(choice2))); mainGameWindow.disableButton(choice2); if( gameLogic.checkMatch( choice1, choice2 ) ) { mainGameWindow.setButtonColorBLUE(choice1); mainGameWindow.setButtonColorBLUE(choice2); ++matched; ++matched; if(matched == mainGameWindow.getAmountOfButtons()) { mainGameWindow.setCompletedMessage("Game completed." ); JOptionPane.showMessageDialog( null , "Completed! in " + ( clicks / 2 ) + " attempted matches!" , "COMPLETED" , JOptionPane.INFORMATION_MESSAGE ); } } else { try { Thread.sleep( 500 ); } catch ( InterruptedException z ){ z.printStackTrace(); } mainGameWindow.setButtonText(choice1, ""); mainGameWindow.setButtonText(choice2, ""); mainGameWindow.enableButton(choice1); mainGameWindow.enableButton(choice2); } } } } }
GLogic.java public class GLogic { int amtOfOptions=0; char[]gameMatchArray; public GLogic(int amt) { if( amt %2 != 0 || amt == 0 ) return; amtOfOptions=amt; setGameMatchArray(); } private void setGameMatchArray() { int charOffset = 65; gameMatchArray = new char[amtOfOptions]; for( int loopCounter = 0 ; loopCounter < amtOfOptions ; loopCounter++ ) { gameMatchArray[loopCounter] = (char)charOffset; if( ( loopCounter % 2 ) == 1 ) ++charOffset; } for( int shuffle = 5 ; shuffle >= 0 ; shuffle-- ) { char temp; for( int lastChar = gameMatchArray.length-1 ; lastChar >= 0 ; lastChar-- ) { charOffset = (int)Math.round( Math.random() * lastChar ); temp = gameMatchArray[lastChar]; gameMatchArray[lastChar] = gameMatchArray[charOffset]; gameMatchArray[charOffset] = temp; } } } public char getCharacter(int pointer) { return gameMatchArray[pointer]; } public boolean checkMatch(int a, int b) { if(gameMatchArray[a]==gameMatchArray[b]) return true; else return false; } }
GUI.javaimport java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.StringTokenizer; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class GUI extends JFrame implements ActionListener { private JButton[] button; private int lastButtonClicked = 0; private int amtOfButtons = 0; private JPanel gameBoard = new JPanel(); private JLabel message = new JLabel( "Welcome to memory game!" ); private static final long serialVersionUID = -2191074802078732591L; public GUI() { super( "Memory" ); int rows = 0, columns = 0; boolean validInput=false; while ( !validInput ) { String myInput = JOptionPane.showInputDialog( null , "Please enter number of columns and rows seperated by a comma.\n(columns*rows must multiply to be even and be between 4 and 52.)" ); StringTokenizer separateFields = new StringTokenizer( myInput , "," ); columns = Integer.parseInt( separateFields.nextToken( ) ); rows = Integer.parseInt( separateFields.nextToken() ); amtOfButtons = columns * rows ; if( ( ( amtOfButtons % 2 ) == 0 ) && ( amtOfButtons >= 4 ) && ( amtOfButtons <= 52 ) ) validInput=true; else if( ( amtOfButtons % 2 ) != 0 ) JOptionPane.showMessageDialog( null , "There is not an even amount of boxes. Please enter again." , "alert" , JOptionPane.ERROR_MESSAGE ); else if( amtOfButtons < 4 ) JOptionPane.showMessageDialog( null , "There is too few boxes. Please enter again." , "alert" , JOptionPane.ERROR_MESSAGE ); else if( amtOfButtons > 52 ) JOptionPane.showMessageDialog( null , "There is too many boxes. Please enter again." , "alert" , JOptionPane.ERROR_MESSAGE ); } setSize( 100*columns, 100*rows ); button = new JButton[amtOfButtons]; setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setLayout( new BorderLayout() ); add( message , BorderLayout.NORTH ); gameBoard.setLayout(new GridLayout( rows , columns ) ); for( int n = 0 ; n < amtOfButtons ; n++ ) { button[n] = new JButton(); button[n].setBackground( Color.BLACK ); button[n].setActionCommand( Integer.toString(n) ); button[n].addActionListener( this ); gameBoard.add( button[n] ); } } public void disableButton(int pointer) { button[pointer].setEnabled(false); } public void enableButton(int pointer) { button[pointer].setEnabled(true); } public void setCompletedMessage(String text) { message.setText( text ); } public int getButtonClicked() { return lastButtonClicked; } public int getAmountOfButtons() { return amtOfButtons; } public void setButtonColorBLUE(int pointer) { button[pointer].setBackground( Color.BLUE ); } public void setButtonText(int buttonNum, String text) { button[buttonNum].setText(text); } public void actionPerformed( ActionEvent e ) { lastButtonClicked = Integer.parseInt( e.getActionCommand() ); } }