Results 1 to 2 of 2
  1. #1
    sjl2013 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Concentration Game with Pictures

    I am attempting to make a concentration game using images for my computer science class at school, and I am having a hard time. Here is what I have so far, so if anyone has any suggestions, they are appreciated.

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    import static java.util.Collections.*;
    
    public class NBARewind extends JFrame implements ActionListener
    {
    	private static final long serialVersionUID = 1L;
    	private JButton[] gameButton = new JButton[16];
    	private ArrayList<ImageIcon> gameList = new ArrayList<ImageIcon>();
    	private int counter = 0;
    	private int[] buttonID = new int[2];
    	private ImageIcon[] buttonValue = new ImageIcon[16];
    	ImageIcon barkley = new ImageIcon("Barkley");
    	ImageIcon hill = new ImageIcon("Hill");
    	ImageIcon jordan = new ImageIcon("Jordan");
    	ImageIcon kemp = new ImageIcon("Kemp");
    	ImageIcon olajuwon = new ImageIcon("Olajuwon");
    	ImageIcon robinson = new ImageIcon("Robinson");
    	ImageIcon rodman = new ImageIcon("Rodman");
    	ImageIcon stockton = new ImageIcon("Stockton");
    	ImageIcon nba = new ImageIcon("NBA");
    	
    	public NBARewind() 
    	{
    		init();
    		panel();
    		setArrayList();
    		setTitle("NBA Rewind");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(500, 500);
    		setVisible(true);
    	}
    	
    	public void setArrayList() 
    	{
    		gameList.add(barkley);
    		gameList.add(hill);
    		gameList.add(jordan);
    		gameList.add(kemp);
    		gameList.add(olajuwon);
    		gameList.add(robinson);
    		gameList.add(rodman);
    		gameList.add(stockton);
    		gameList.add(barkley);
    		gameList.add(hill);
    		gameList.add(jordan);
    		gameList.add(kemp);
    		gameList.add(olajuwon);
    		gameList.add(robinson);
    		gameList.add(rodman);
    		gameList.add(stockton);
    		shuffle(gameList);
    	}	
    
    	public void init()
    	{
    		
    		for (int j = 0; j < gameButton.length; j++) 
    		{
    			gameButton[j] = new JButton();
    			gameButton[j].addActionListener(this);
    		}
    		
    	}
    	
    	public void panel()
    	{
    		Panel gamePanel = new Panel();
    		gamePanel.setLayout(new GridLayout(4, 4));
    		
    		for (int j = 0; j < gameButton.length; j++)
    		{
    			gamePanel.add(gameButton[j]);
    		}
    
    		add(gamePanel, BorderLayout.CENTER);
    	}
    	
    	public boolean sameValues() 
    	{
    		
    		if (buttonValue[0] == buttonValue[1])
    		{
    			return true;
    		}
    		
    		return false;		
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		
    		for (int i = 0; i < gameButton.length; i++) 
    		{
    			
    			if (gameButton[i] == e.getSource()) 
    			{
    				//gameButton[i].setIcon(gameList.get(i));
    				gameButton[i].setEnabled(false);
    				counter++;
    				
    				if (counter == 3) 
    				{
    					if (sameValues()) 
    					{
    						 gameButton[buttonID[0]].setEnabled(false);
    						gameButton[buttonID[1]].setEnabled(false);
    					} 
    					else 
    					{
    						gameButton[buttonID[0]].setEnabled(true);
    						gameButton[buttonID[0]].setIcon(nba);
    						gameButton[buttonID[1]].setEnabled(true);
    						gameButton[buttonID[1]].setText("");
    					}
    					
    					counter = 1;
    				}
    				
    				if (counter == 1) 
    				{
    					buttonID[0] = i;
    					buttonValue[0] = gameList.get(i);
    				}
    				
    				if (counter == 2) 
    				{
    					buttonID[1] = i;
    					buttonValue[1] = gameList.get(i);
    				}
    				
    			}
    			
    		}
    		
    	}
    	
    	public static void main(String[] args) 
    	{
    		new NBARewind();
    	}	
    }
    Last edited by Fubarable; 05-14-2011 at 08:25 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sjl2013 View Post
    I am attempting to make a concentration game using images for my computer science class at school, and I am having a hard time. Here is what I have so far, so if anyone has any suggestions, they are appreciated.
    You'll find that the help you get is much better if you ask a specific question.

    Also, I've added code tags to your post above so that your code will retain its formatting. To do this yourself, please see the link in my signature below.

    Best of luck!

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 10:51 PM
  2. 3D pictures
    By mine0926 in forum Advanced Java
    Replies: 8
    Last Post: 10-27-2010, 02:43 AM
  3. Choosing a degree concentration
    By xcallmejudasx in forum Jobs Discussion
    Replies: 11
    Last Post: 08-14-2009, 12:30 PM
  4. pictures
    By chalo in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 01:26 PM
  5. pictures
    By f_the_cook in forum AWT / Swing
    Replies: 5
    Last Post: 10-29-2008, 07:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •