Memory/Concentration Game

Java Swing not using IDE

Page 1 of 1

8 Replies - 16907 Views - Last Post: 16 October 2010 - 09:53 AM Rate Topic: -----

#1 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Memory/Concentration Game

Posted 14 October 2010 - 03:40 PM

JUST FYI: I AM NOT ASKING FOR ANYONE TO DO MY HOMEWORK - I SIMPLY AM HOPING FOR A STARTING POINT OR MAYBE A SIMPLE OUTLINE TO PUSH ME IN THE RIGHT DIRECTION
I guess I should start off by saying I am a complete beginner in Java. I am in my 4th year of Undergrad university, and have taken two courses where I programmed in C#. I am now on a study abroad trip, and basically have been assigned a program in Java and I don't even know where to start. I know they are similar, but I still feel really lost.

The general overview of the program is as follows:
We are supposed to show that we can use simple swing components, swing layout and simple event handling. We are meant to implement a simple concentration game using Java Swing. The user will be presented with a two-dimensional grid of picture playing cards laid face-down. They must be able to select two cards and turn the mover. If they show the same picture the player scores a HIT and the matching cards are removed from the playing surface. If the pictures revealed are different they are turned face-down again and the player scores a MISS. The game ends when all cards have been removed. The object is to clear the surface with as few MISSES as possible.

This game should allow the player to select from a set of rectangular screen regions using the mouse. On selection, the screen region should reveal some form of underlying graphic. The player should select two such regions, and the game should record HITS and MISSES. The game ends when all graphics have been revealed.

Marks will be given for
-the effective use of layout managers to position input components and displays so that they are able to handle movement and resizing
-provision of a scramble button which randomly reorganizes the "cards" and creates a new game
-Provision of a solve button which reveals all the hidden graphics


I guess what worries me the most about this program is that we are not meant to write in an IDE, the source code must be compiled and run using only javac. What I'm hoping is for someone to be able to push me in the right direction. I REALLY want to learn how to do this on my own, without copying anybody else's code. Can anybody offer me any advice? I think if I can work my way through this program I will feel a lot more confident about my skills. Right now I don't have any confidence, but I can't give up! The program is due on Monday October 25th, 2010...so I've not got a whole lot of time! Please, if you can help in any way, or outline some general steps I should take, I would REALLY appreciate it ^^

Is This A Good Question/Topic? 0
  • +

Replies To: Memory/Concentration Game

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Memory/Concentration Game

Posted 14 October 2010 - 04:26 PM

My a stupid assignment/teacher starting right in front with GUI application when you don't even know the basic.

You can use Notepad as editor or NotepadPlus from which, I think, you can compile (invoke javac)

Your game is usually call the "Memory Game" and you will find many in this forum
You will have to learn a lot of new concepts:
You will need a JPanel with a GridLayout to put your card on as JLabel or JButton. You will have toput your JPanel in a JFrame
You will need an ActionListener to react to the click on them

If you want to cheat I wrote a CodeSnippet (in the CodeSnippet section of this forum) about a Memory Game. Doubt that you can complete your own before Oct 25th especially that you have a C++ background which as a GUI Builder and we usually don't use them in Java.

Any how welcome at Dic and good luck and we will be happy to correct your code
Was This Post Helpful? 0
  • +
  • -

#3 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Memory/Concentration Game

Posted 14 October 2010 - 04:36 PM

Well I'm kind off in the same position as you are, I'm used to c#/VS, and now I'm getting java in university. It's another way, but certainly fun, java has it's pro's and con's. I think your main issue would be programming the UI "by hand". For this you would most certainly need a layoutmanager as you mentioned. Basically what it does is format components you add in a particular way, and also takes in account the user resizing the frame etc. Another cool thing is you could add more or less components at runtime, and sizing would be done automatically. All this is done in a JPanel btw. Another handy thing is the pack() function of the JFrame, which sizes the frame to fit what the layout manager does in the JPanel.

I found the gridlayout easy to use, and also the borderlayout, which allows you to have separate areas for a menu, a status bar etc. Take a look at this page for a good explanation.

So it comes down to:
-Make a frame.
-Add a panel to the frame, and set desired layoutmanager
-Add components to the frame
-call pack() on the frame(or you could set a fixed size in the frame's constructor)

After that, it's hooking up events, and program the rest, but am sure you know what to do there.
But another thing is different layout managers can be a bit unpredictable when it comes to sizing, so you need to experiment with them a bit to find out. Also remember you can put another panel inside your panel with a different layoutmanager, think of panels as div's in html, that's a good comparison.

Ok I hope my ramblings made any sense, good luck with your assignment.

This post has been edited by Renagado: 14 October 2010 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Memory/Concentration Game

Posted 14 October 2010 - 06:22 PM

Here are several resources:
http://www.dreaminco...g-swing-part-i/

There is a series. Just look on the Author's contributions page.

Good tutorials:
http://download.orac...tart/index.html
http://download.orac...ents/index.html
http://download.orac...yout/index.html
http://download.orac...ents/index.html
http://download.orac...misc/index.html
Was This Post Helpful? 0
  • +
  • -

#5 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: Memory/Concentration Game

Posted 14 October 2010 - 11:52 PM

Thanks much everyone - I'm going to see if I can get a start on this today. I'll post what I get so you can give me feedback if you'd like. To be perfectly honest, I'm also a bit unsure about how to actually code the Memory Game, how it works. I'm guessing that I'll need some sort of array, maybe some sort of "card class"? I never had to write that many "functional" programs, it was usually just little mini programs, and for my final project I didn't behave properly and Ctrl+c/Ctrl+v other people's code. I want to get this figured out, I want to feel like I understand the concepts in this and feel proud of what I did. I'll walk through the Java Swing tutorial and then try to get SOMETHING laid out, so you can have a look then. I'll be working in Notepad++.
Thanks again!
Was This Post Helpful? 0
  • +
  • -

#6 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: Memory/Concentration Game

Posted 15 October 2010 - 07:48 AM

Here is a rough skeleton of my program so far. It really doesn't do anything yet. At all...I also need to maybe draw in some buttons to represent cards. What would be the best method to do this? I know I will need an array for this most likely, to compare the matching cards. I've also included a picture of what the JFrame looks like - not much AT ALL, but it is what I have at the minute. Please let me know what steps I should take next :sweatdrop: :whistling: I appreciate it!

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

public class ConcentrationGame extends JFrame
{
	JButton scrambleButton;
	JButton solveButton;
	DrawPanel panel;
	
	public ConcentrationGame()
	{
		//set the window size, title, close op, and visibility
		
		setSize(600, 600);
		setTitle("Concentration");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		//create buttons with names
		scrambleButton = new JButton("Scramble");
		solveButton = new JButton("Solve");
		
		// create event listeners for the buttons
		scrambleButton.addActionListener(new scrambleButtonListener());
		solveButton.addActionListener(new solveButtonListener());
		
		//create panel and add buttons to the panel
		
		DrawPanel panel = new DrawPanel();
		panel.add(solveButton);
		panel.add(scrambleButton);
		
		// add panel to window
		add(panel);
		}
		
		//this section will be used to handle the event
		//when the user clicks the scrambleButton
		public class scrambleButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				//will randomly reorganize the "cards" and
				//create a new game
			}
		}
		
		public class solveButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				// will reveal all the hidden graphics
			}
		}
		
		public class DrawPanel extends JPanel
		{
				public void paintComponent( Graphics g)
				{
					// ensure the panel displays correctly
					super.paintComponent(g);
			
					int width = getWidth(); // panel width
					int height = getHeight(); // panel height
				}
		}
	
		
		public static void main(String[] args)
		{
			new ConcentrationGame();
		}
}

Attached image(s)

  • Attached Image

This post has been edited by taylor.alison: 15 October 2010 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: Memory/Concentration Game

Posted 16 October 2010 - 06:57 AM

What's happening forum?
I have tried to instantiate a GridLayout, but now where before I had my two buttons showing, now nothing is showing. Could somebody please point to where I am going wrong? I would totally appreciate it!! ^^
Thanks so much!

/**
 * @(#)ConcentrationGame.java
 *
 * ConcentrationGame application
 *
 * @author
 * @version 1.00 2010/10/16
 */

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

public class ConcentrationGame extends JFrame
{
	JButton scrambleButton;
	JButton solveButton;
	DrawPanel panel;

	//create an array to store the card values - two of each to represent the matches
	//int[] cardMatchValues={1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};

	//create an array of buttons
	//private final String names[] =
	//{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"};

	// implement the ToggleButton which allows two buttons to remain pressed
	JButton[] deck = new JButton[16];

	public ConcentrationGame()
	{
		gameSetUp();
	}

	private void gameSetUp()
	{

		//set the window size, title, close op, and visibility

		setSize(600, 600);
		setTitle("Concentration");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

		//create buttons with names
		scrambleButton = new JButton("Scramble");
		solveButton = new JButton("Solve");

		// create gridLayout
		GridLayout cardGridLayout = new GridLayout(4,4,5,5); //4x4, spacing by 5

		// create event listeners for the buttons
		scrambleButton.addActionListener(new scrambleButtonListener());
		solveButton.addActionListener(new solveButtonListener());

		//create panel add gridLayout to the panel
		DrawPanel layoutPanel = new DrawPanel();
		layoutPanel.setLayout(cardGridLayout);
		for (int i = 0; i < deck.length; i++)
		{
			layoutPanel.add(deck[i]);
		}



		//create panel and add buttons to the panel
		DrawPanel buttonPanel = new DrawPanel();
		buttonPanel.add(solveButton);
		buttonPanel.add(scrambleButton);

		// add panel to window
		add(panel);
		}

		//this section will be used to handle the event
		//when the user clicks the scrambleButton
		public class scrambleButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				//will randomly reorganize the "cards" and
				//create a new game
			}
		}

		public class solveButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				// will reveal all the hidden graphics
			}
		}

		public class DrawPanel extends JPanel
		{
				public void paintComponent( Graphics g)
				{
					// ensure the panel displays correctly
					super.paintComponent(g);

					int width = getWidth(); // panel width
					int height = getHeight(); // panel height
				}
		}


		public static void main(String[] args)
		{
			new ConcentrationGame();
		}
}

Was This Post Helpful? 0
  • +
  • -

#8 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: Memory/Concentration Game

Posted 16 October 2010 - 08:39 AM

Okay, so I've actually taken a little bit of a different approach to things. Here is a different version of my program. All it is doing is setting up a GridLayout and displaying an array of buttons. Basic, but it looks alright so far. Now I've got to get the two buttons and possibly two labels on a bottom row to display hits and misses. Any ideas for steps I should take from here? Sorry I keep updating with something different, I just really don't know how to tackle this :( I know everything is in the main class right now, it was just for testing purposes because I couldn't get it to work when I wrote it as methods. I will get there, but for now I'll do it this way just to make sure things work.

/**
 * @(#)Practice.java
 *
 * Practice application
 *
 * @author
 * @version 1.00 2010/10/16
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class Practice extends JFrame {

	//public void gridSetup(){

//	}



    public static void main(String[] args) {

    	// TODO, add your application code
    	//new gridSetup();
    	JButton[] deck = new JButton[16];
		JFrame frame = new JFrame("Memory");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel deckPanel = new JPanel(new GridLayout(4,4,5,5));

		 for (int i = 0; i < deck.length; i++)
         {
            deck[i] = new JButton("Card: " + i);
            deck[i].setSize(80,80);
            deck[i].setText("Card " + (i + 1));
            deckPanel.add(deck[i]);
         }

		//add the panel and set size/visibility of frame
	    frame.add(deckPanel);
		frame.setSize(400,400);
		frame.setVisible(true);
    }
}

This post has been edited by taylor.alison: 16 October 2010 - 08:40 AM

Was This Post Helpful? 0
  • +
  • -

#9 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: Memory/Concentration Game

Posted 16 October 2010 - 09:53 AM

LAST UPDATE OF THE NIGHT!!!
I think I finally have the layout sorted out. FINALLY. So the next step will be all the behind the scenes business...what steps do you think I should take next? A general outline?
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class Practice extends JFrame {

    public static void main(String[] args) {

    	// TODO, add your application code
    	//new gridSetup();
    	// Create array of Buttons
    	JButton[] deck = new JButton[12];

    	// Create Frame and set the X operation to Exit
		JFrame frame = new JFrame("Memory");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//create a panel for the cards and the buttons and the hit/miss labels
		JPanel deckPanel = new JPanel(new GridLayout(3,4,5,5));
		JPanel buttonPanel = new JPanel(new GridLayout(1,4,5,5));

		//create icon
		ImageIcon pokeball = new ImageIcon("images/pokeball.jpg");


		// adds all of the buttons into the deckPanel
		 for (int i = 0; i < deck.length; i++)
         {
            deck[i] = new JButton("", pokeball);
            //deck[i].addActionListener(this);
            deck[i].setSize(80,80);
            deckPanel.add(deck[i]);
         }

         //add buttons and labels to the buttonPanel
         JButton scrambleButton = new JButton("Scramble");
         JButton solveButton = new JButton("Solve");
         JLabel hitLabel = new JLabel("Hits: ");
         JLabel missLabel = new JLabel("Misses: ");

 	     buttonPanel.add(hitLabel);
         buttonPanel.add(missLabel);
         buttonPanel.add(scrambleButton);
         buttonPanel.add(solveButton);

		//add the panel and set size/visibility of frame
	    frame.add(deckPanel, BorderLayout.NORTH);
	    frame.add(buttonPanel, BorderLayout.SOUTH);
		frame.setSize(500,500);
		frame.setVisible(true);
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1