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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Help with concentration program

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with concentration program

    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() );
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    Where do you add the button objects to the GUI so they will be shown?

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    AH I forgot that, I just did that actually.
    Updated
    GUI.java
    import 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;
    	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 = new JPanel();
    		gameBoard.setLayout(new GridLayout( rows , columns ) );
    		gameBoard.setVisible(true);
    		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] );
    		}
    		add(gameBoard,BorderLayout.CENTER);
     
    	}
    	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() );
    	}
    }
    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;
    	}
    }
    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 );
    						live = false;
    					}
    				}
    				else
    				{
     
    					try { Thread.sleep( 500 ); }
    					catch ( InterruptedException z ){ z.printStackTrace(); }
    					mainGameWindow.setButtonText(choice1, "");
    					mainGameWindow.setButtonText(choice2, "");
    					mainGameWindow.enableButton(choice1);
    					mainGameWindow.enableButton(choice2);
    				}
    			}
    		}
    	}
    }

    The commented line is the last line to get this program working right

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    Are you saying that somewhere in that code there is a line with a comment and that I should try to find it?

    Are the buttons working now?

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    THe buttons are and arent working lol.
    They're up now but not working due to the code in main.
    3rd window, scolled up 5 lines from bottom is where the comment is. Baiscally I need it to wait for an action from mainGameWindow before I try to get the button clicked

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    I need it to wait for an action from mainGameWindow before I try to get the button clicked
    If you don't want a button to be clicked until some event, you could disable it and enable it when the event happens.
    THe buttons are and arent working
    Explain about the buttons

  7. #7
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    I'm sorry I didn't word what I meant to say correctly.
    Basically right here
    		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);
    			}
    is in the main source file.
    I want it to get in to this loop and basically wait before a button is clicked before assigning "
    choice1 = mainGameWindow.getButtonClicked();"

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    You can "wait" by exiting your code and waiting until an event occurs that calls a method in your code where you can do what you wanted to do.
    The event for you appears to be the button being clicked. When the button is clicked then you can exectute this in the button's listener.
    choice1 = mainGameWindow.getButtonClicked()

  9. #9
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    how would I implement that?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    You'd rewrite your code.Move the actions to be done when the button is pressed in the button's actionListener.

  11. #11
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    I had a version of my code like that, it messes up since the GUI and logic need to be in 2 different threads or else it hangs up and never shows the 2nd clicks value if it doesnt match the first click.
    package main;
    import 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 GMG extends JFrame implements ActionListener
    {
    	private JButton[] button;
    	private char[] gameMatchArray;
    	private JLabel message = new JLabel( "Welcome to memory game!" );
    	private static final long serialVersionUID = 2987090742412923219L;
    	int amtOfButtons = 0, choice1 = 0, columns = 0, choice2 = 0, rows = 0, clicks = 0, matched = 0;
     
    	public static void main( String[] args )
    	{
    		GMG theGame = new GMG();
    		theGame.setVisible( true );
    	}
     
    	public GMG()
    	{
    		super( "Memory" );
     
    		setInput();
     
    		setSize( 100*columns, 100*rows );
    		button = new JButton[amtOfButtons];
    		gameMatchArray = new char[amtOfButtons];
     
    		createMatches();
     
    		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		setLayout( new BorderLayout() );
    		add( message , BorderLayout.NORTH );
     
    		JPanel gameBoard = new JPanel();
    		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] );
    		}
     
    		add( gameBoard , BorderLayout.CENTER );
    	}
     
    	public void actionPerformed( ActionEvent e )
    	{
    		++clicks;
     
    		if( ( clicks % 2 ) == 1 )
    		{
    			choice1 = Integer.parseInt( e.getActionCommand() );
    			button[choice1].setText( Character.toString( gameMatchArray[choice1] ) );
    			button[choice1].setEnabled( false );
    		}
    		else
    		{
    			choice2 = Integer.parseInt( e.getActionCommand() );
    			button[choice2].setText( Character.toString( gameMatchArray[choice2] ) );
    			button[choice2].setEnabled( false );
     
    			winCheck();
    		}
    	}
     
    	public void setInput()
    	{	
    		while ( true )
    		{
    			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 ) )
    				return;
    			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 );
    		}
    	}
     
    	public void createMatches()
    	{
    		int charOffset = 65;
     
    		for( int loopCounter = 0 ; loopCounter < amtOfButtons ; 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 void winCheck()
    	{
    		if( gameMatchArray[choice1] == gameMatchArray[choice2] )
    		{
    			++matched;
    			++matched;
    			button[choice1].setBackground( Color.BLUE );
    			button[choice2].setBackground( Color.BLUE );
    			if( matched == amtOfButtons )
    			{
    				message.setText( "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(); }
    			button[choice1].setText( "" );
    			button[choice1].setEnabled( true );
    			button[choice2].setText( "" );
    			button[choice2].setEnabled( true );
    		}
    	}
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    Can you explain what a user is supposed to do and what he should see when he does it?

  13. #13
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    basically its a memory game
    You click 2 buttons see if the values match if they do keep the text as is, change color to blue
    else if it doesnt match switch back to blank and re-enable

    The first [3 part] program I sent should be able to be finished if I knew how to implement a SwingWoker but I don't.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    What is the problem with the code you posted in post#11

  15. #15
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    Text display, basically its an inner workings of java error. It compiles and runs and makes matches but if no match theres a bug that I cant iron out. again that can also be fixed with a SwingWorker also I believe

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    Can you explain what the problem is when you execute the code. What will the user see?

  17. #17
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    It's hard to explain. I tried already it messes up since the GUI and logic need to be in 2 different threads or else it hangs up and never shows the 2nd buttons value if it doesnt match the first button.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    Without a way to test it that will demonstrate what you are seeing, its hard to test.

  19. #19
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    import these workspace folders in eclipse
    Attached Files Attached Files

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    Sorry, I don't use that IDE.

  21. #21
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    Why not? I love eclipse

  22. #22
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help with concentration program

    Quote Originally Posted by CjStaal View Post
    Why not? I love eclipse
    You love eclipse but everyone has his/her own choice. Simply stating IDE nevertheless will take you to do everything and learn nothing. (Most of the cases)
    Well, back to your question now,
    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] );
    		}
    As far as drawing multiple buttons is concerned, a loop is fine but why do you need to register action listener for every button separately. Won't you just try defining one event listener for all of your buttons?

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with concentration program

    Any updates?

  24. #24
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    Ah, just an FYI, I fixed everything. The problem was how Java swing handled the updates of the jFrame, only updating AFTER the actionPerformed thread was done and returned to the main thread. I worked around this by using multiple swingworkers in a quite clever way. Also stopped any input during the showing of the two tiles.
    Here is the code
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.SwingWorker;
    import javax.swing.JOptionPane;
    import java.util.StringTokenizer;
    public class Matchgame extends JFrame implements ActionListener
    {
    	private static String currentPlayer = "Player 1";
    	private static char[] buttonValue;
    	private static JButton[] button;
    	private static boolean runCheck = false, finished=false;
    	private static final long serialVersionUID = -1890926013993464332L;
    	private static JLabel message= new JLabel(currentPlayer + ", choose a tile...");
    	private static int currentTurnClicks=0, player1Matches=0,player2Matches=0,clickedButton1=-1,clickedButton2=-2,numberOfMatchesFound=0,totalMatches=0;
    	public static void main(String[] args)
    	{
    			Matchgame theGame = new Matchgame();
    			theGame.setVisible(true);
    			synchronized(theGame)
    			{
    				while(!finished)
    					if(runCheck) mySwingWorker(clickedButton1,clickedButton2);
    			}
    	}
    	private Matchgame()
    	{
    		super("Staal's MatchGame");
    		while(true)
    		{
    			StringTokenizer tk;
    			try
    			{
    				tk = new StringTokenizer(JOptionPane.showInputDialog("Please enter enter x,y values\n" +
    					"{amount of buttons across comma amount of buttons down}\n must be divisible by 2 and <= 52. "), ",");
    				if(tk.countTokens()==2)
    				{
    					int width = Integer.parseInt(tk.nextToken());
    					int height = Integer.parseInt(tk.nextToken());
    					int amount = width*height;
    					if((amount)%2==0 & amount <=52)
    					{
    						totalMatches=amount/2;
    						setSize(height*100, width*125);
    						buttonValue= new char[amount];
    						button = new JButton[amount];
    						setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    						setLayout(new BorderLayout());
    						add(message,BorderLayout.NORTH);
    						JPanel gameBoard = new JPanel();
    						gameBoard.setLayout(new GridLayout(width,height));
    						for(int i=0; i<amount; ++i)
    						{
    							button[i] = new JButton(Integer.toString(i+1));
    							button[i].setBackground(Color.GRAY);
    							button[i].addActionListener(this);
    							gameBoard.add(button[i]);
    						}
    						add(gameBoard,BorderLayout.CENTER);
    						for(int c=0, b=65;c<amount;++c)
    						{
    							buttonValue[c]=(char)(b);
    							if(c%2==1)
    								++b;
    						}
    						char temp;
    						for(int c=5;c>=0;--c)
    							for(int d=amount-1, b; d>=0; --d)
    							{
    								b=(int)Math.round(Math.random()*d);
    								temp=buttonValue[d];
    								buttonValue[d]=buttonValue[b];
    								buttonValue[b]=temp;
    							}
    						return;
    					}
    				}
    				JOptionPane.showMessageDialog(null,"Incorrect paramaters");
    			}
    			catch(NumberFormatException e){e.printStackTrace();}
    		}
    	}
    	private final static void switchPlayer()
    	{
    		if(currentPlayer.equals("Player 1")) currentPlayer="Player 2";
    		else currentPlayer="Player 1";
    		message.setText( currentPlayer + ", choose your tile...");
    		currentTurnClicks=0;
    	}
    	public final void actionPerformed(ActionEvent e)
    	{
    		if(!runCheck) mySwingWorker(Integer.parseInt(e.getActionCommand()));
    	}
    	private final static SwingWorker<Integer, String> mySwingWorker(int tButtonNum)
    	{
    		Color playerColor = Color.RED;
    		if (currentPlayer.equals("Player 2")) playerColor = Color.GREEN;
    		if(++currentTurnClicks==1)
    		{
    			clickedButton1=tButtonNum--;
    			message.setText(currentPlayer + ", choose your second tile...");
    			setUpButton(clickedButton1, Character.toString(buttonValue[tButtonNum]), playerColor, false);
    		}
    		else if(currentTurnClicks==2)
    		{
    			clickedButton2=tButtonNum--;
    			setUpButton(clickedButton2, Character.toString(buttonValue[tButtonNum]), playerColor, false);
    			runCheck=true;
    		}
    		return null;
    	}
    	private final static SwingWorker<Integer, String> mySwingWorker(int buttonNumber, int buttonNumber2)
    	{
    		if(Character.toString(buttonValue[clickedButton1-1]).equals(Character.toString(buttonValue[clickedButton2-1])))
    		{
    			if(currentPlayer.equals("Player 1")) ++player1Matches;
    			else ++player2Matches;
    			if(++numberOfMatchesFound==totalMatches)
    			{
    				if(player1Matches>player2Matches) message.setText("Player 1 Wins with "+player1Matches+" matches.");
    				else if(player1Matches<player2Matches) message.setText("Player 2 Wins with "+player2Matches+" matches.");
    				else if(player1Matches==player2Matches) message.setText("Tie. Game over.");
    				finished=true;
    				return null;
    			}
    		}
    		else
    		{
    			try{Thread.sleep(2000);}catch(InterruptedException i){i.printStackTrace();}
    			setUpButton(buttonNumber,Integer.toString(buttonNumber),Color.GRAY,true);
    			setUpButton(buttonNumber2,Integer.toString(buttonNumber2),Color.GRAY,true);
    		}
    		switchPlayer();
    		runCheck=false;
    		return null;
    	}
    	private final static void setUpButton(int buttonNumberX, String text, Color background, boolean value)
    	{
    		button[--buttonNumberX].setBackground(background);
    		button[buttonNumberX].setText(text);
    		button[buttonNumberX].setEnabled(value);
    	}
    }
    The way I used the boolean to differentiate between the threads is clever in my eyes, because it also allows for no input during the pause.
    ^^ I've become a much more robust coder in the past few months.
    Last edited by CjStaal; April 17th, 2012 at 10:43 PM.

  25. #25
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with concentration program

    Hello,

    I have a problem...I have two servers (test & real)... I compiled my .java file & .class file is generated...
    There is no problem with the test server, the code works fine, I get the output as desired... but in the real server it gives me below error..
    java.sql.SQLException: Column 'dccyn' not found.

    Below is my code...

    sbQuery.append(" select ");
    sbQuery.append(" merchantno, krname ");
    sbQuery.append(", dccyn, mcpyn, mcayn, mpiyn ");
    sbQuery.append(" from ");
    sbQuery.append(" keb_merchant ");
    sbQuery.append("where pg='KP' ");

    pstmt = con.prepareStatement(sbQuery.toString());
    rs = pstmt.executeQuery();
    while(rs.next()){

    KEBLimitEntity entity = new KEBLimitEntity();

    entity.setMerchantNo(rs.getString("merchantno"));
    entity.setKrName(rs.getString("krname"));
    entity.setDccYN(rs.getString("dccyn"));
    entity.setMcpYN(rs.getString("mcpyn"));
    entity.setMcaYN(rs.getString("mcayn"));
    entity.setMpiYN(rs.getString("mpiyn"));

    entityList.add(entity);
    }
    rs.close();
    pstmt.close();
    }



    I have selected 'dccyn' in my query & also added it to the arrayList.

    Please somebody help me, Its urgent...
    Thanks in advance

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM