First of all in the cardListListener method that I have commented out bellow, which is for the JComboBox, Im getting an error with
String choice = (String)e.getSelectedItem();
Another problem Im haveing is once a button is clicked it can be clicked again, and it is seen as the match where as I need to make it where once the card is turned it cant be clicked anymore. Ive tried to setEnabled(false); but then it greys the button out. Is there some way to lock the card, and then release it once the timer ends. maybe a global button lock or something?
Finally I was looking for some help with how to initialize the new game button, to reset the board.
Anyways if im asking to much sorry :/
import java.awt.*; import java.util.Timer.*; import java.awt.event.*; import java.awt.event.ActionEvent.*; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import javax.swing.*; public class SimpsonsGame extends JFrame { public JButton button[] = new JButton[40]; public int counter = 0; public JComboBox cardList; public String nGame = "New Game"; public JPanel cardPanel, newGamePanel, dropDownPanel; public String[] cardType = {"Simpson Family", "Simpsons Logo", "Simpsons Cast"}; ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>(); HashMap<JButton, Icon> buttonImage = new HashMap<JButton, Icon>(); ImageIcon Family = new ImageIcon("img/Simpsons.jpg"); ImageIcon Logo = new ImageIcon("img/SimpsonsLogo2.jpg"); ImageIcon Cast = new ImageIcon("img/SimpsonClass.jpg"); ImageIcon photo1 = new ImageIcon("img/Bart.jpg"); ImageIcon photo2 = new ImageIcon("img/Maggie.jpg"); ImageIcon photo3 = new ImageIcon("img/Marge.jpg"); ImageIcon photo4 = new ImageIcon("img/Lisa.jpg"); ImageIcon photo5 = new ImageIcon("img/Homer.jpg"); ImageIcon photo6 = new ImageIcon("img/Nelson.jpg"); ImageIcon photo7 = new ImageIcon("img/Grampa.jpg"); ImageIcon photo8 = new ImageIcon("img/Chief.jpg"); ImageIcon photo9 = new ImageIcon("img/Cecil.jpg"); ImageIcon photo10 = new ImageIcon("img/Mel.jpg"); ImageIcon photo11 = new ImageIcon("img/Hans.jpg"); ImageIcon photo12 = new ImageIcon("img/ComicBook.jpg"); ImageIcon photo13 = new ImageIcon("img/Krusty.jpg"); ImageIcon photo14 = new ImageIcon("img/Moe.jpg"); ImageIcon photo15 = new ImageIcon("img/MilHouse.jpg"); ImageIcon photo16 = new ImageIcon("img/Santa.jpg"); ImageIcon photo17 = new ImageIcon("img/Snowball.jpg"); ImageIcon photo18 = new ImageIcon("img/Snake.jpg"); ImageIcon photo19 = new ImageIcon("img/Sideshow.jpg"); ImageIcon photo20 = new ImageIcon("img/Ralph.jpg"); ImageIcon photo1copy = photo1; ImageIcon photo2copy = photo2; ImageIcon photo3copy = photo3; ImageIcon photo4copy = photo4; ImageIcon photo5copy = photo5; ImageIcon photo6copy = photo6; ImageIcon photo7copy = photo7; ImageIcon photo8copy = photo8; ImageIcon photo9copy = photo9; ImageIcon photo10copy = photo10; ImageIcon photo11copy = photo11; ImageIcon photo12copy = photo12; ImageIcon photo13copy = photo13; ImageIcon photo14copy = photo14; ImageIcon photo15copy = photo15; ImageIcon photo16copy = photo16; ImageIcon photo17copy = photo17; ImageIcon photo18copy = photo18; ImageIcon photo19copy = photo19; ImageIcon photo20copy = photo20; int[] btnID = new int[2]; ImageIcon[] btnValue = new ImageIcon[2]; Timer timer; public SimpsonsGame() { setTitle("Simpsons Memory Game"); setLayout(new BorderLayout()); cardPanel = new JPanel(new GridLayout(5, 8, 5, 5)); JButton newGame = new JButton(); newGamePanel = new JPanel(); newGame = new JButton(nGame); newGame.setActionCommand(nGame); newGame.addActionListener(new newGameButtonListener()); newGamePanel.add(newGame); add(newGamePanel, BorderLayout.SOUTH); cardList = new JComboBox(cardType); cardList.setSelectedIndex(0); add(cardList, BorderLayout.NORTH); for (int i = 0; i < button.length; i++) { button[i] = new JButton(); button[i].setIcon(Family); button[i].setSize(400, 400); cardList.addActionListener(new cardListListener()); button[i].addActionListener(new buttonListener()); cardPanel.add(button[i]); } add(cardPanel, BorderLayout.CENTER); addIcons(); } private void addIcons() { icons.add(photo1); icons.add(photo2); icons.add(photo3); icons.add(photo4); icons.add(photo5); icons.add(photo6); icons.add(photo7); icons.add(photo8); icons.add(photo9); icons.add(photo10); icons.add(photo11); icons.add(photo12); icons.add(photo13); icons.add(photo14); icons.add(photo15); icons.add(photo16); icons.add(photo17); icons.add(photo18); icons.add(photo19); icons.add(photo20); icons.add(photo1copy); icons.add(photo2copy); icons.add(photo3copy); icons.add(photo4copy); icons.add(photo5copy); icons.add(photo6copy); icons.add(photo7copy); icons.add(photo8copy); icons.add(photo9copy); icons.add(photo10copy); icons.add(photo11copy); icons.add(photo13copy); icons.add(photo14copy); icons.add(photo15copy); icons.add(photo16copy); icons.add(photo17copy); icons.add(photo18copy); icons.add(photo19copy); icons.add(photo20copy); icons.add(photo12copy); Collections.shuffle(icons); for (int i = 0; i < button.length; i++) { buttonImage.put(button[i], icons.get(i)); } } public void time() { timer = new Timer(1000, new TimerListener()); timer.start(); } private class cardListListener implements ActionListener { public void actionPerformed(ActionEvent e) { /* String choice = (String)e.getSelectedItem(); if(choice.equals("Simpson Family")) { cardList.setSelectedIndex(0); } else if(choice.equals("Simpsons Logo")) { cardList.setSelectedIndex(1); } else if(choice.equals("Simpsons Cast")) { cardList.setSelectedIndex(2); }*/ } } private class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { button[btnID[0]].setEnabled(true); button[btnID[0]].setIcon(Family); button[btnID[1]].setEnabled(true); button[btnID[1]].setIcon(Family); timer.stop(); counter = 0; } } // public void buttonLock() { // } public boolean sameValues() { if (btnValue[0].equals(btnValue[1])) { return true; } return false; } private class buttonListener implements ActionListener { public void actionPerformed(ActionEvent e) { for (int i = 0; i < button.length; i++) { if (button[i] == e.getSource()) { button[i].setIcon(buttonImage.get(button[i])); button[i].setEnabled(true); counter++; if (counter == 1) { btnID[0] = i; btnValue[0] = (ImageIcon) buttonImage.get(button[i]); } if (counter == 2) { btnID[1] = i; btnValue[1] = (ImageIcon) buttonImage.get(button[i]); if (sameValues()) { button[btnID[0]].setEnabled(false); button[btnID[1]].setEnabled(false); } else { time(); } counter = 0; } } } } } public class newGameButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { int count = 0; if (e.getActionCommand().equals("New Game")) { count++; if (count == 1) { return; } } } } }
import java.awt.Color; import javax.swing.*; public class Main { public static void main(String[] args) { JFrame frame = new SimpsonsGame(); frame.setSize(1000, 1000); frame.setVisible(true); frame.setBackground(Color.CYAN); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }