3 Replies - 34410 Views - Last Post: 29 November 2014 - 07:36 AM Rate Topic: -----

#1 dubxcity  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 21-March 14

GUI Memory Game

Posted 30 June 2014 - 06:11 PM

Making a memory game where you click a square it turns over an image and you match the two pretty simple
My issue is my images are not displaying and I am not sure why everything looks correct to me. Any suggestions would be great
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;


public class MemoryApp extends JFrame {

	static String Pics[] = {"beaver.JPG", "dawgs.jfif",
		"ducks.jfif", "forky.jfif",
		"trees.jfif", "utes.jfif"};
	static JButton buttons[];
	ImageIcon closedIcon;
	int numButtons;
	ImageIcon icons[];
	int firstClick, secondClick, clickNumber;
	Timer myTimer;
	int openImages;

	public MemoryApp() {
		// Set the title.


		setTitle("Memory Game");

		// Specify an action for the close button.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create a BorderLayout manager.
		getContentPane().setLayout(new GridLayout(2, Pics.length));

		closedIcon = new ImageIcon("p12.jfif");
		numButtons = Pics.length * 2;
		buttons = new JButton[numButtons];
		icons = new ImageIcon[numButtons];
		for (int i = 0, j = 0; i < Pics.length; i++) {
			icons[j] = new ImageIcon(Pics[i]);
			buttons[j] = new JButton("");
			buttons[j].addActionListener(new MemoryApp.ImageButtonListener());
			buttons[j].setIcon(closedIcon);
			getContentPane().add(buttons[j++]);

			icons[j] = icons[j - 1];
			buttons[j] = new JButton("");
			buttons[j].addActionListener(new MemoryApp.ImageButtonListener());
			buttons[j].setIcon(closedIcon);
			getContentPane().add(buttons[j++]);
		}

		// randomize icons
		Random generator = new Random();
		for(int i=0 ; i<numButtons; i++)
		{
			int j = generator.nextInt(numButtons);
			ImageIcon temp = icons[i];
			icons[i] = icons[j];
			icons[j] = temp;
		}    
		// Pack and display the window.
		pack();
		setVisible(true);
		validate();
		myTimer = new Timer(1000, new TimerListener());
	}

	private class TimerListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			myTimer.stop();
			buttons[firstClick].setIcon(closedIcon);
			buttons[secondClick].setIcon(closedIcon);

		}
	}

	private class ImageButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			for (int i = 0; i < numButtons; i++) {
				if (e.getSource() == buttons[i]) {
					buttons[i].setIcon(icons[i]);
				}
			}
		}
	}

	public static void main(String[] args) {
		MemoryApp mem = new MemoryApp();
		mem.setSize(450,300);
		mem.setVisible(true);
		mem.setLocation(450, 220);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: GUI Memory Game

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,166
  • Joined: 27-December 08

Re: GUI Memory Game

Posted 30 June 2014 - 06:19 PM

Why is this in ColdFusion? Moving to Java.

Can you confirm the ImageIcons are not null?
Was This Post Helpful? 0
  • +
  • -

#3 dubxcity  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 21-March 14

Re: GUI Memory Game

Posted 01 July 2014 - 09:05 PM

View Postmacosxnerd101, on 30 June 2014 - 06:19 PM, said:

Why is this in ColdFusion? Moving to Java.

Can you confirm the ImageIcons are not null?

yes they are not null any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 adolph11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-November 14

Re: GUI Memory Game

Posted 29 November 2014 - 07:36 AM

You need to initialize the closedIcon variable and your other icons variables like this

closedIcon = new ImageIcon(getClass().getResource("your_file_name.jpg"));
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1