I am trying to make a matching game where the user selects two images and the program has to check if the images match. I am trying to put random images in buttons and I am getting this error:
M:\School\ICS4U1\Projects\Matching\MatchingGame.ja va:134: cannot find symbol
symbol : method generateNewRandom()
location: class MatchingGame
gameButton[i].setIcon(images[generateNewRandom()]);
^
M:\School\ICS4U1\Projects\Matching\MatchingGame.ja va:160: incompatible types
found : java.lang.Object
required: int
return numbersList.get(n);
This is my code:
/** *Matching Game * * * * 2013/2/25 */ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import javax.swing.*; import static java.util.Collections.*; import javax.swing.ImageIcon.*; public class MatchingGame extends JFrame implements ActionListener { private JButton exitButton, replayButton; private JButton[] gameButton = new JButton[36]; private ImageIcon[] ImageArray; public MatchingGame() { setLayout(new BorderLayout()); init(); panel(); setImages(); setTitle("MathingGame"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(900, 900); setVisible(true); } public void init() { for (int i = 0; i < gameButton.length; i++) { gameButton[i] = new JButton(); gameButton[i].addActionListener(this); } exitButton = new JButton("Exit"); exitButton.addActionListener(this); replayButton = new JButton("Replay"); replayButton.addActionListener(this); } public void panel() { Panel gamePanel = new Panel(); gamePanel.setLayout(new GridLayout(6, 6)); for (int i = 0; i < gameButton.length; i++) { gamePanel.add(gameButton[i]); } Panel buttonPanel = new Panel(); buttonPanel.add(replayButton); buttonPanel.add(exitButton); buttonPanel.setLayout(new GridLayout(1, 0)); add(gamePanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); } public void setImages(){ ImageIcon Aquaman = new ImageIcon ("Aquaman.jpg"); ImageIcon Batman = new ImageIcon ("Batman.jpg"); ImageIcon CaptainAmerica = new ImageIcon ("CaptainAmerica.jpg"); ImageIcon Deadpool = new ImageIcon ("Deadpool.jpg"); ImageIcon FantasticFour = new ImageIcon ("FantasticFour.jpg"); ImageIcon Flash = new ImageIcon ("Flash.jpg"); ImageIcon GreenArrow = new ImageIcon ("GreenArrow.jpg"); ImageIcon GreenLantern = new ImageIcon ("GreenLantern.jpg"); ImageIcon Hawkeye = new ImageIcon ("Hawkeye.jpg"); ImageIcon Ironman = new ImageIcon ("Ironman.jpg"); ImageIcon Punisher = new ImageIcon ("Punisher.jpg"); ImageIcon RedTornado = new ImageIcon ("RedTornado.jpg"); ImageIcon Robin = new ImageIcon ("Robin.jpg"); ImageIcon Spiderman = new ImageIcon ("Spiderman.jpg"); ImageIcon Superman = new ImageIcon ("Superman.jpg"); ImageIcon TheAvengers = new ImageIcon ("TheAvengers.jpg"); ImageIcon Thor = new ImageIcon ("Thor.jpg"); ImageIcon Wonderwoman = new ImageIcon ("Wonderwoman.jpg"); ImageIcon[] images = new ImageIcon[36]; images[0] = Aquaman; images[1] = Batman; images[2] = CaptainAmerica; images[3] = Deadpool; images[4] = FantasticFour; images[5] = Flash; images[6] = GreenArrow; images[7] = GreenLantern; images[8] = Hawkeye; images[9] = Ironman; images[10] = Punisher; images[11] = RedTornado; images[12] = Robin; images[13] = Spiderman; images[14] = Superman; images[15] = TheAvengers; images[16] = Thor; images[17] = Wonderwoman; images[18] = Aquaman; images[19] = Batman; images[20] = CaptainAmerica; images[21] = Deadpool; images[22] = FantasticFour; images[23] = Flash; images[24] = GreenArrow; images[25] = GreenLantern; images[26] = Hawkeye; images[27] = Ironman; images[28] = Punisher; images[29] = RedTornado; images[30] = Robin; images[31] = Spiderman; images[32] = Superman; images[33] = TheAvengers; images[34] = Thor; images[35] = Wonderwoman; for(int i=0; i<36;i++){ gameButton[i].setIcon(images[generateNewRandom()]); } } public class RandomNumberGenerator { ArrayList numbersList = new ArrayList (); public RandomNumberGenerator(int length) { for(int i=0;i<=36;i++) numbersList.add(i); Collections.shuffle(numbersList); } public int generateNewRandom(int n) { return numbersList.get(n); } } public void actionPerformed(ActionEvent e) { if (exitButton == e.getSource()) { System.exit(0); } if (replayButton == e.getSource()) { } } public static void main (String[] args){ new MatchingGame(); } }