Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Tolls

Thread: MEmory_game_image displaying

  1. #1
    vaanie87 is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default MEmory_game_image displaying

    hi,im very new to java, im doin a memory game in java.. its working bt its mix matcch the number... i dont know how to put image in this code so i can mix match the image instant of numbers.

    please help me
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.*;
    
    public class MemoryGame
        // don't extend frame
        /* extends JFrame */
        implements ActionListener {
    
        // very important!
        public void updateHitMiss() {
            HitScore.setText("Hit: " + Hit);
            MissScore.setText("Miss: " + Miss);
        }
    
        private JFrame window = new JFrame("Memory Game");
        private static final int WINDOW_WIDTH = 500; // pixels
        private static final int WINDOW_HEIGHT = 500; // pixels
        private JButton exitBtn, replayBtn, solveBtn;
        ImageIcon ButtonIcon = createImageIcon("Penguins.jpg");
        private JButton[] gameBtn = new JButton[16];
        private ArrayList<Integer> gameList = new ArrayList<Integer>();
        private int Hit, Miss = 0;
        private int counter = 0;
        private int[] btnID = new int[2];
        private int[] btnValue = new int[2];
        private JLabel HitScore, MissScore;
    
        private JPanel gamePnl = new JPanel();
        private JPanel buttonPnl = new JPanel();
        private JPanel scorePnl = new JPanel();
    
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = MemoryGame.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
        public MemoryGame()
        {
            createGUI();
            createJPanels();
            setArrayListText();
            window.setTitle("MemoryGame");
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            window.setVisible(true);
        }
    
        public void createGUI()
        {
            for (int i = 0; i < gameBtn.length; i++)
            {
                gameBtn[i] = new JButton(ButtonIcon);
    
                gameBtn[i].addActionListener(this);
            }
            HitScore = new JLabel("Hit: " + Hit);
            MissScore = new JLabel("Miss: " + Miss);
            exitBtn = new JButton("Exit");
            exitBtn.addActionListener(this);
            replayBtn = new JButton("Shuffle");
            replayBtn.addActionListener(this);
            solveBtn = new JButton("Solve");
            solveBtn.addActionListener(this);
        }
    
        public void createJPanels()
        {
    
            gamePnl.setLayout(new GridLayout(4, 4));
            for (int i = 0; i < gameBtn.length; i++)
            {
                gamePnl.add(gameBtn[i]);
            }
            buttonPnl.add(replayBtn);
            buttonPnl.add(exitBtn);
            buttonPnl.add(solveBtn);
            buttonPnl.setLayout(new GridLayout(1, 0));
            scorePnl.add(HitScore);
            scorePnl.add(MissScore);
            scorePnl.setLayout(new GridLayout(1, 0));
            window.add(scorePnl, BorderLayout.NORTH);
            window.add(gamePnl, BorderLayout.CENTER);
            window.add(buttonPnl, BorderLayout.SOUTH);
        }
    
        public void setArrayListText()
        {
            for (int i = 0; i < 2; i++)
            {
                for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++)
                {
                    gameList.add(ii);
                }
            }
        }
    
    
        public boolean sameValues()
        {
            if (btnValue[0] == btnValue[1])
            {
                return true;
            }
    
            return false;
        }
    
        public void actionPerformed(ActionEvent e)
        {
                if (exitBtn == e.getSource())
                {
                    System.exit(0);
                }
                if (replayBtn == e.getSource())
                {
                    for (int i = 0; i < gameBtn.length; i++)
                    {
                    gamePnl.remove(gameBtn[i]);
                    }
                   
                    for (int i = 0; i < gameBtn.length; i++)
                    {
                    gamePnl.add(gameBtn[i]);
                    }
    
                }
    
                if (solveBtn == e.getSource())
                {
                    for (int i = 0; i < gameBtn.length; i++)
                    {
                        gameBtn[i].setText("" + gameList.get(i));
                        gameBtn[btnID[0]].setEnabled(false);
                        gameBtn[btnID[1]].setEnabled(false);
    
                    }
                }
    
                for (int i = 0; i < gameBtn.length; i++)
                {
    
                    if (gameBtn[i] == e.getSource())
                    {
                        gameBtn[i].setText("" + gameList.get(i));
                        gameBtn[i].setEnabled(false);
                        counter++;
                        if (counter == 3)
                        {
                            if (sameValues())
                            {
                                gameBtn[btnID[0]].setEnabled(false);
                                gameBtn[btnID[1]].setEnabled(false);
                                gameBtn[btnID[0]].setVisible(false);
                                gameBtn[btnID[1]].setVisible(false);
                                Hit = Hit +1;
                            }
                            else
                            {
                                    gameBtn[btnID[0]].setEnabled(true);
                                    gameBtn[btnID[0]].setText("");
                                    gameBtn[btnID[1]].setEnabled(true);
                                    gameBtn[btnID[1]].setText("");
                                    Miss = Miss +1;
                            }
                            counter = 1;
                        }
                        if (counter == 1)
                        {
                            btnID[0] = i;
                            btnValue[0] = gameList.get(i);
                        }
                        if (counter == 2)
                        {
                            btnID[1] = i;
                            btnValue[1] = gameList.get(i);
                        }
                    }
                }
            updateHitMiss();
        }
    
        public static void main(String[] args)
        {
    
            new MemoryGame();
        }
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: MEmory_game_image displaying

    Stick some debugging code in there (System.out.println()'s), printing out values of interest (that'll be up to you to determine) so you can see what's going on.
    Without that you (and us) will be flying blind and just guessing as to the problem.
    quad64bit likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. displaying questions one by one
    By Vijay@007 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-15-2011, 08:22 AM
  2. Displaying an image
    By SWEngineer in forum AWT / Swing
    Replies: 11
    Last Post: 01-28-2010, 05:48 AM
  3. displaying 2D-Matrix
    By srinivasmallabathula in forum New To Java
    Replies: 2
    Last Post: 02-18-2009, 07:19 PM
  4. Displaying Data
    By skiing in forum New To Java
    Replies: 4
    Last Post: 11-15-2008, 02:56 AM

Posting Permissions

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