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.


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

Results 1 to 2 of 2

Thread: Help with my memory concentration game (2D Array help)

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default Help with my memory concentration game (2D Array help)

    So i'm making a memory game in java using netbeans. The game starts with an interface of 18 cards (9 pairs) in which the player has to select two of the same card for the pair to be removed. This goes until no cards are left and the player wins a point. Because i'm very new to this, i need some help adding function to the cards (making them flip, giving the ability to select two, making them face down again). I currently have 3 classes. A Main class, which tells my CController class to create the game using information stored in my final class, CView. The help i need is with the CView class. I already have the following code:

    package concentration;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class CView extends JButton implements ActionListener{
    String [] Images = {"one.png", "two.png", "three.png", "four.png", "five.png",  //array of images to be used on cards
    			"six.png", "seven.png", "eight.png", "nine.png"};
    ArrayList<ImageIcon> imagesarraylist = new ArrayList<ImageIcon>();
    java.net.URL card = CView.class.getResource("card.png");
    JButton[][] button = new JButton[3][6];
    JTextField playerscore = new JTextField("Player score:                                        ");
    JButton exit = new JButton("Exit");
    JTextField computerscore = new JTextField("Computer score:                                    ");
    CController controller;
    public CView(CController thecontroller){  //establishes the Jframe, sets the sizes, colours etc.
    super ("Concentration");
    JFrame frame = new JFrame();
    JPanel gridPanel = new JPanel(new GridLayout(3,6));
    JPanel northPanel = new JPanel();
    northPanel.setBackground( Color.CYAN ) ;
    frame.setSize(900, 680);
    frame.add(northPanel, BorderLayout.NORTH);
    northPanel.setSize(900, 100);
    gridPanel.setSize(800, 600);
    for(int i=0; i<button.length; i++){
    for(int j=0; j<button[i].length; j++){
    int n = i*button[i].length + j+1;
    button[i][j] = new JButton();
    for(int i=0; i<3; i++){
    for(int j=0; j<6; j++){
    button[i][j].setIcon(new ImageIcon(card));
    button[i][j].addActionListener(this);  //adds an actionlistenr to the grid buttons
    for(int i=0; i<Images.length; i++){
    imagesarraylist.add(new ImageIcon(Images[i]));
    int count = 0;
    for(int i=0; i<button.length; i++){
    for(int j=0; j<button[i].length; j++){
    button[i][j] = new JButton();
    exit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {  //adds functionality to the exit button (exit)
    frame.setResizable(false);  //makes the frame visible, sets default close operation and sets the frames resizable quality to false
    public void updateSCore(int which, int value){
    // change score to value provided by controller
    case 1: playerscore.setText(Integer.toString(value));
    case 2: computerscore.setText(Integer.toString(value));
    public void actionPerformed(ActionEvent eve) {
        public void mouseClicked(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");

    Any help would be greatly appreciated. Currently the outcome of the program is pic.jpg this.

    Mainly i need code in the actionPerformed method.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 632 Times in 540 Posts

    Default Re: Help with my memory concentration game (2D Array help)

    What exactly is your question? You say you need help with it, but what exactly are you stuck on?

    Also, most people would advise against using a gui builder until you really understand what's going on behind the scenes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Properly releasing memory to avoid memory pileup/crash
    By fickletrick in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 22nd, 2012, 10:09 AM
  2. [SOLVED] Memory usage increasing in while loop - is it a memory leak
    By mds1256 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2012, 10:06 AM
  3. [SOLVED] Help with concentration program
    By CjStaal in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 18th, 2012, 05:58 AM
  4. Memory Card Game JButtons Array
    By rlo10 in forum What's Wrong With My Code?
    Replies: 26
    Last Post: March 4th, 2012, 04:02 PM
  5. Problem with Memory Card Game
    By rlo10 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 18th, 2012, 09:09 PM