So i'm making a memory game in java using netbeans. The game starts with an interface of 18 cards (9 pairs) in which the player has to select two of the same card for the pair to be removed. This goes until no cards are left and the player wins a point. Because i'm very new to this, i need some help adding function to the cards (making them flip, giving the ability to select two, making them face down again). I currently have 3 classes. A Main class, which tells my CController class to create the game using information stored in my final class, CView. The help i need is with the CView class. I already have the following code:
package concentration; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class CView extends JButton implements ActionListener{ String [] Images = {"one.png", "two.png", "three.png", "four.png", "five.png", //array of images to be used on cards "six.png", "seven.png", "eight.png", "nine.png"}; ArrayList<ImageIcon> imagesarraylist = new ArrayList<ImageIcon>(); java.net.URL card = CView.class.getResource("card.png"); JButton[][] button = new JButton[3][6]; JTextField playerscore = new JTextField("Player score: "); JButton exit = new JButton("Exit"); JTextField computerscore = new JTextField("Computer score: "); CController controller; public CView(CController thecontroller){ //establishes the Jframe, sets the sizes, colours etc. super ("Concentration"); JFrame frame = new JFrame(); JPanel gridPanel = new JPanel(new GridLayout(3,6)); JPanel northPanel = new JPanel(); northPanel.add(playerscore); northPanel.setBackground( Color.CYAN ) ; northPanel.add(computerscore); northPanel.add(exit); frame.setSize(900, 680); frame.add(northPanel, BorderLayout.NORTH); northPanel.setSize(900, 100); frame.add(gridPanel); gridPanel.setSize(800, 600); for(int i=0; i<button.length; i++){ for(int j=0; j<button[i].length; j++){ int n = i*button[i].length + j+1; button[i][j] = new JButton(); gridPanel.add(button[i][j]); } } for(int i=0; i<3; i++){ for(int j=0; j<6; j++){ button[i][j].setIcon(new ImageIcon(card)); button[i][j].addActionListener(this); //adds an actionlistenr to the grid buttons } } for(int i=0; i<Images.length; i++){ imagesarraylist.add(new ImageIcon(Images[i])); } Collections.shuffle(imagesarraylist); int count = 0; for(int i=0; i<button.length; i++){ for(int j=0; j<button[i].length; j++){ button[i][j] = new JButton(); button[i][j].setIcon(imagesarraylist.get(count%imagesarraylist.size())); count++; } } exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { //adds functionality to the exit button (exit) System.exit(0); } }); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); //makes the frame visible, sets default close operation and sets the frames resizable quality to false } public void updateSCore(int which, int value){ // change score to value provided by controller switch(which){ case 1: playerscore.setText(Integer.toString(value)); case 2: computerscore.setText(Integer.toString(value)); } } public void actionPerformed(ActionEvent eve) { } public void mouseClicked(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } }
Any help would be greatly appreciated. Currently the outcome of the program is pic.jpg this.
Mainly i need code in the actionPerformed method.