Results 1 to 20 of 20
  1. #1
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default memory game in JAVA

    harlo, can anyone help me to see what is wrong? I use netbean to run it, but
    don't have any error already, but still the picture cannot being display when playing the game.

    Java Code:
    //Import all java packages
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    import javax.swing.border.*;
    
    
    public class JavaMemory extends JFrame implements ActionListener
    
    {
    
    private JPanel contentPane;
    private JMenuBar jMenuBar1 = new JMenuBar();
    
    // Declare and initialise buttons
    private JButton bt[] = new JButton[16];
    private JButton bt1 = new JButton();
    private JButton bt2 = new JButton();
    private JButton bt3 = new JButton();
    private JButton bt4 = new JButton();
    private JButton bt5 = new JButton();
    private JButton bt6 = new JButton();
    private JButton bt7 = new JButton();
    private JButton bt8 = new JButton();
    private JButton bt9 = new JButton();
    private JButton bt10 = new JButton();
    private JButton bt11 = new JButton();
    private JButton bt12 = new JButton();
    private JButton bt13 = new JButton();
    private JButton bt14 = new JButton();
    private JButton bt15 = new JButton();
    private JButton bt16 = new JButton();
    
    private JButton btTimer = new JButton();
    private JButton Exit = new JButton();
    private JButton btreset = new JButton();
    
    //Declare and initialise icon for the pictures
    ImageIcon myimage = new ImageIcon("");
    
    // To create an array of integer.
    private int no[]= new int [16];
    
    // To allow the player to enter his name
    String name = new String();
    
    // Create random generator to generate random numbers
    private Random r= new Random();
    
    // Counters
    int count = bt.length;
    
    // To record the number of tries
    int numclick = 0;
    
    // Keep track of which button is pressed
    int ButtonPressed=-1;
    
    // Keep track of the number
    int numtrack;
    
    // To check the state of a game when double clicking
    boolean turn=false;
    
    // Declare a boolean array
    boolean Buttons[] = new boolean[16];
    
    // To create timers. The time is in milliseconds
    Timer timerGame = new Timer(1000,null);
    
    // To store time
    int timeStart;
    
    // The time left for the player to solve the game
    int timeLeft;
    
    // Time Delay for turning cards face down
    Timer timer = new Timer(600, null);
    
    // Create Labels, border and components for the menu
    TitledBorder BorderTitle;
    private JLabel jLabel1 = new JLabel();
    private JLabel jLabel2 = new JLabel();
    private JLabel jLabel3 = new JLabel();
    private JLabel jLabel4 = new JLabel();
    private JLabel jLabel5 = new JLabel();
    
    private JMenu jMenuFile = new JMenu();
    private JMenuItem jMenuNewGame = new JMenuItem();
    private JMenuItem jMenuFileExit = new JMenuItem();
    private JMenu jMenuHelp = new JMenu();
    private JMenuItem jMenuInstructions = new JMenuItem();
    
    private JTextField jTextField1 = new JTextField();
    private JTextField jTextName = new JTextField();
    private JTextField jTextGameInfo = new JTextField();
    
    
    // To construct the GUI
    public JavaMemory()
    {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try
    {
    jbInit();
    
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    
    // Initialise Component
    private void jbInit() throws Exception
    { 
    BorderTitle = new TitledBorder("");
    this.setBoard();
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(550, 560));
    this.setTitle("MEMORY GAME");
    contentPane = (JPanel) this.getContentPane();
    
    // To handle all events when a button is clicked
    bt1.setBounds(new Rectangle(75, 101, 64, 62));
    bt1.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt1_actionPerformed(e);
    }
    
    });
    
    bt2.setBounds(new Rectangle(139, 101, 64, 62));
    bt2.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt2_actionPerformed(e);
    }
    });
    
    bt3.setBounds(new Rectangle(203, 101, 64, 62));
    bt3.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt3_actionPerformed(e);
    }
    
    });
    
    bt4.setBounds(new Rectangle(267, 101, 64, 62));
    bt4.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt4_actionPerformed(e);
    }
    
    });
    
    bt5.setBounds(new Rectangle(75, 163, 64, 62));
    bt5.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt5_actionPerformed(e);
    }
    });
    
    bt6.setBounds(new Rectangle(139, 163, 64, 62));
    bt6.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt6_actionPerformed(e);
    }
    });
    
    bt7.setBounds(new Rectangle(203, 163, 64, 62));
    bt7.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt7_actionPerformed(e);
    }
    });
    
    bt8.setBounds(new Rectangle(267, 163, 64, 62));
    bt8.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt8_actionPerformed(e);
    }
    });
    
    bt9.setBounds(new Rectangle(75, 225, 64, 62));
    bt9.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt9_actionPerformed(e);
    }
    });
    
    bt10.setBounds(new Rectangle(139, 225, 64, 62));
    bt10.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt10_actionPerformed(e);
    }
    });
    
    bt11.setBounds(new Rectangle(203, 225, 64, 62));
    bt11.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt11_actionPerformed(e);
    }
    });
    
    bt12.setBounds(new Rectangle(267, 225, 64, 62));
    bt12.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt12_actionPerformed(e);
    }
    
    });
    
    bt13.setBounds(new Rectangle(75, 287, 64, 62));
    bt13.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt13_actionPerformed(e);
    }
    
    });
    
    bt14.setBounds(new Rectangle(139, 287, 64, 62));
    bt14.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt14_actionPerformed(e);
    }
    
    });
    
    bt15.setBounds(new Rectangle(203, 287, 64, 62));
    bt15.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt15_actionPerformed(e);
    }
    
    });
    
    bt16.setBounds(new Rectangle(267, 287, 64, 62));
    bt16.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt16_actionPerformed(e);
    }
    
    });
    
    timer.setRepeats(false); // So that cards stay face up when turned
    timer.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    timer_actionPerformed(e);
    }
    });
    
    // Set the title of game
    jLabel1.setBounds(new Rectangle(0, 18, 490, 28));
    jLabel1.setFont(new java.awt.Font("Arial", 1, 24));
    jLabel1.setText("MEMORY GAME");
    jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel1.setForeground(Color.blue);
    jLabel1.setRequestFocusEnabled(true);
    jLabel2.setBounds(new Rectangle(0, 43, 570, 28));
    
    // Set text to the menu bar options
    jMenuFile.setText("File");
    jMenuFileExit.setText("Exit");
    jMenuHelp.setText("Help");
    jMenuNewGame.setText("New Game");
    
    // Implementation of the new game on the menu bar
    jMenuNewGame.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    jMenuNewGame_actionPerformed(e);
    }
    });
    
    // Implementation of the exit functionality on the menu bar
    jMenuFileExit.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    jMenuFileExit_actionPerformed(e);
    }
    });
    
    // Implementation of the timer limit
    jTextField1.setText("200");// To set a time limit to complete the game
    jTextField1.setBounds(new Rectangle(372, 243, 36, 25));
    jTextField1.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    jTextField1_actionPerformed(e);
    }
    
    private void jTextField1_actionPerformed(ActionEvent e) {
    //throw new UnsupportedOperationException("Not yet implemented");
    }
    });
    
    // To implement the start button
    btTimer.setBounds(new Rectangle(373, 120, 103, 30));
    btTimer.setText("PLAY");
    btTimer.setToolTipText("Press button to start game");
    btTimer.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    btTimer_actionPerformed(e);
    }
    });
    
    timerGame.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    timerGame_actionPerformed(e);
    }
    });
    
    // To implement the reset button
    btreset.setBounds(new Rectangle(373, 155, 103, 30));
    btreset.setToolTipText("Press button to reset game!");
    btreset.setText("RESET");
    
    btreset.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e) { 
    
    }
    });
    
    jLabel3.setText("Name:");
    jLabel3.setBounds(new Rectangle(55, 75, 41, 17));
    jTextName.setBounds(new Rectangle(100, 75, 158, 21));
    
    // Implementation of the Exit button on the game
    Exit.setBounds(new Rectangle(373, 300, 103, 30));
    Exit.setToolTipText("Press button to quit!");
    Exit.setText("EXIT");
    Exit.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    Quit_actionPerformed(e);
    }
    });
    
    // Implementation of the Instructions functionality on the menu bar
    jLabel5.setText("seconds");
    jLabel5.setBounds(new Rectangle(416, 248, 68, 17));
    jMenuInstructions.setText("Instructions");
    jMenuInstructions.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    jMenuInstructions_actionPerformed(e);
    }
    });
    
    // To add components to the GUI
    contentPane.setBackground(Color.lightGray);
    contentPane.setBorder(BorderFactory.createRaisedBevelBorder());
    
    contentPane.add(jLabel1, null);
    contentPane.add(jLabel2, null);
    contentPane.add(jLabel3, null);
    contentPane.add(jLabel4, null);
    contentPane.add(jLabel5, null);
    
    contentPane.add(bt1, null);
    contentPane.add(bt2, null);
    contentPane.add(bt3, null);
    contentPane.add(bt4, null);
    contentPane.add(bt5, null);
    contentPane.add(bt6, null);
    contentPane.add(bt7, null);
    contentPane.add(bt8, null);
    contentPane.add(bt9, null);
    contentPane.add(bt10, null);
    contentPane.add(bt11, null);
    contentPane.add(bt12, null);
    contentPane.add(bt13, null);
    contentPane.add(bt14, null);
    contentPane.add(bt15, null);
    contentPane.add(bt16, null);
    
    contentPane.add(jTextField1, null);
    contentPane.add(jTextName, null);
    contentPane.add(jTextGameInfo, null);
    
    contentPane.add(btTimer, null);
    contentPane.add(btreset, null);
    contentPane.add(Exit, null);
    
    this.setJMenuBar(jMenuBar1);
    jMenuBar1.add(jMenuFile);
    jMenuFile.add(jMenuNewGame);
    jMenuFile.add(jMenuFileExit);
    jMenuBar1.add(jMenuHelp);
    jMenuHelp.add(jMenuInstructions);
    
    }
    
    // To exit game
    public void jMenuFileExit_actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    
    protected void processWindowEvent(WindowEvent e)
    {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING)
    {
    jMenuFileExit_actionPerformed(null);
    }
    }
    
    // To set a board to draw
    void setBoard()
    {
    bt[0] = this.bt1;
    bt[1] = this.bt2;
    bt[2] = this.bt3;
    bt[3] = this.bt4;
    bt[4] = this.bt5;
    bt[5] = this.bt6;
    bt[6] = this.bt7;
    bt[7] = this.bt8;
    bt[8] = this.bt9;
    bt[9] = this.bt10;
    bt[10] = this.bt11;
    bt[11] = this.bt12;
    bt[12] = this.bt13;
    bt[13] = this.bt14;
    bt[14] = this.bt15;
    bt[15] = this.bt16;
    
    btTimer.setEnabled(true);
    jTextName.setEditable(false);
    jTextGameInfo.setEditable(false);
    
    // To disable all buttons so that player has to click on start to begin
    for (int i=0; i<16; i++)
    bt[i].setEnabled(false);
    
    // Create a set of picture
    for (int i=0;i<8;i++)
    {
    no[i]=no[i+8]=i+1;
    }
    
    // Scramble the numbers
    for (int i=0;i<300;i++)
    {
    // Select two positions randomly
    int R1=r.nextInt(16);
    int R2=r.nextInt(16);
    
    // Change positions of card at every new game
    int temp = no[R1];
    no[R1] = no[R2];
    no[R2] = temp;
    }
    }
    
    // to check the status of the cards during play
    void checkStatus(int intSecValue, int intSecButton)
    {
    if(turn == false)
    {
    timer.start();
    this.bt1.setEnabled(false);
    this.bt2.setEnabled(false);
    this.bt3.setEnabled(false);
    this.bt4.setEnabled(false);
    this.bt5.setEnabled(false);
    this.bt6.setEnabled(false);
    this.bt7.setEnabled(false);
    this.bt8.setEnabled(false);
    this.bt9.setEnabled(false);
    this.bt10.setEnabled(false);
    this.bt11.setEnabled(false);
    this.bt12.setEnabled(false);
    this.bt13.setEnabled(false);
    this.bt14.setEnabled(false);
    this.bt15.setEnabled(false);
    this.bt16.setEnabled(false);
    
    if (no[numtrack] == no[intSecValue])
    {
    Buttons[ButtonPressed] = true;// If false the second matched card only remains on display
    Buttons[intSecButton] = true; // If false first button matched clicked remains on display
    // If both false, cards matched return face down
    
    // Keep track of the number of cards left
    count -=2;
    if(count <=0)
    {
    endGame(true);
    }
    }
    }
    }
    
    // To set images to the buttons and set the events to happen when the game is played
    void bt1_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[0]+"tiger.gif");
    this.bt1.setIcon(img);
    this.bt1.setEnabled(false);
    if (turn == false) // if true random two matched cards will not be displayed
    {
    ButtonPressed = 0;
    numtrack = no[0];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[0], 0); 
    this.bt1.setDisabledIcon(img); // Display the picture.
    }
    
    void bt2_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[1]+"tiger.gif");
    this.bt2.setIcon(img);
    this.bt2.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 1;
    numtrack = no[1];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[1], 1);
    this.bt2.setDisabledIcon(img); // Display the picture.
    }
    
    void bt3_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[2]+"hawk.gif");
    this.bt3.setIcon(img);
    this.bt3.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 2;
    numtrack = no[2];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[2], 2);
    this.bt3.setDisabledIcon(img); // Display the picture.
    }
    
    void bt4_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[3]+"hawk.gif");
    this.bt4.setIcon(img);
    this.bt4.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 3;
    numtrack = no[3];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[3], 3);
    this.bt4.setDisabledIcon(img); // Display the picture.
    }
    
    void bt5_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[4]+"dog.gif");
    this.bt5.setIcon(img);
    this.bt5.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 4;
    numtrack = no[4];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[4], 4);
    this.bt5.setDisabledIcon(img); // Display the picture.
    }
    
    void bt6_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[5]+"dog.gif");
    this.bt6.setIcon(img);
    this.bt6.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 5;
    numtrack = no[5];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[5], 5);
    this.bt6.setDisabledIcon(img); // Display the picture.
    }
    
    void bt7_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[6]+"cat.gif");
    this.bt7.setIcon(img);
    this.bt7.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 6;
    numtrack = no[6];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[6], 6);
    this.bt7.setDisabledIcon(img); // Display the picture.
    }
    void bt8_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[7]+"cat.gif");
    this.bt8.setIcon(img);
    this.bt8.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 7;
    numtrack = no[7];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[7], 7);
    this.bt8.setDisabledIcon(img); // Display the picture.
    }
    
    void bt9_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[8]+"snake.gif");
    this.bt9.setIcon(img);
    this.bt9.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 8;
    numtrack = no[8];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[8], 8);
    this.bt9.setDisabledIcon(img); // Display the picture.
    }
    
    void bt10_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[9]+"snake.gif");
    this.bt10.setIcon(img);
    this.bt10.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 9;
    numtrack = no[9];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[9], 9);
    this.bt10.setDisabledIcon(img); // Display the picture.
    }
    
    void bt11_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[10]+"crocodile.gif");
    this.bt11.setIcon(img);
    this.bt11.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 10;
    numtrack = no[10];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[10], 10);
    this.bt11.setDisabledIcon(img); // Display the picture.
    }
    
    void bt12_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[11]+"crocodile.gif");
    this.bt12.setIcon(img);
    this.bt12.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 11;
    numtrack = no[11];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[11], 11);
    this.bt12.setDisabledIcon(img); // Display the picture.
    }
    
    void bt13_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[12]+"shark.gif");
    this.bt13.setIcon(img);
    this.bt13.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 12;
    numtrack = no[12];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[12], 12);
    this.bt13.setDisabledIcon(img); // Display the picture.
    }
    
    void bt14_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[13]+"shark.gif");
    this.bt14.setIcon(img);
    this.bt14.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 13;
    numtrack = no[13];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[13], 13);
    this.bt14.setDisabledIcon(img); // Display the picture.
    }
    
    void bt15_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[14]+"frog.gif");
    this.bt15.setIcon(img);
    this.bt15.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 14;
    numtrack = no[14];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[14], 14);
    this.bt15.setDisabledIcon(img); // Display the picture.
    }
    
    void bt16_actionPerformed(ActionEvent e)
    {
    ImageIcon img = new ImageIcon(no[15]+"frog.gif");
    this.bt16.setIcon(img);
    this.bt16.setEnabled(false);
    if (turn == false)
    {
    ButtonPressed = 15;
    numtrack = no[15];
    numclick++;
    }
    turn = !turn;
    this.checkStatus(no[15], 15);
    this.bt16.setDisabledIcon(img); // Display the picture.
    } 
    
    void timer_actionPerformed(ActionEvent e)
    {
    // Hide all numbers
    if (this.Buttons[0] == false)
    {
    bt1.setIcon(null);
    bt1.setEnabled(true);
    }
    if (this.Buttons[1] == false)
    {
    bt2.setIcon(null);
    bt2.setEnabled(true);
    }
    if (this.Buttons[2] == false)
    {
    bt3.setIcon(null);
    bt3.setEnabled(true);
    }
    if (this.Buttons[3] == false)
    {
    bt4.setIcon(null);
    bt4.setEnabled(true);
    }
    if (this.Buttons[4] == false)
    {
    bt5.setIcon(null);
    bt5.setEnabled(true);
    }
    if (this.Buttons[5] == false)
    {
    bt6.setIcon(null);
    bt6.setEnabled(true);
    }
    if (this.Buttons[6] == false)
    {
    bt7.setIcon(null);
    bt7.setEnabled(true);
    }
    if (this.Buttons[7] == false)
    {
    bt8.setIcon(null);
    bt8.setEnabled(true);
    }
    if (this.Buttons[8] == false)
    {
    bt9.setIcon(null);
    bt9.setEnabled(true);
    }
    if (this.Buttons[9] == false)
    {
    bt10.setIcon(null);
    bt10.setEnabled(true);
    }
    if (this.Buttons[10] == false)
    {
    bt11.setIcon(null);
    bt11.setEnabled(true);
    }
    if (this.Buttons[11] == false)
    {
    bt12.setIcon(null);
    bt12.setEnabled(true);
    }
    
    if (this.Buttons[12] == false)
    {
    bt13.setIcon(null);
    bt13.setEnabled(true);
    }
    
    if (this.Buttons[13] == false)
    {
    bt14.setIcon(null);
    bt14.setEnabled(true);
    }
    
    if (this.Buttons[14] == false)
    {
    bt15.setIcon(null);
    bt15.setEnabled(true);
    }
    
    if (this.Buttons[15] == false)
    {
    bt16.setIcon(null);
    bt16.setEnabled(true);
    }
    
    ButtonPressed= -1;
    numtrack = 0;
    }
    
    // Ending the game
    public void endGame(boolean win)
    {
    if(win)
    {
    timerGame.stop();
    jTextGameInfo.setText("Well Done \n"+jTextName.getText()+" have completed the game in "+(timeStart-timeLeft)+"seconds");
    JOptionPane.showMessageDialog(this,"Well Done " + jTextName.getText()+"!\nYou win in "+numclick+" number of Clicks \nYou completed the game in "+(timeStart-timeLeft)+"seconds","Game Over", JOptionPane.NO_OPTION);
    btTimer.setEnabled(false);
    }
    else
    {
    jTextGameInfo.setText("Sorry! You lost in "+numclick+" number of Clicks");
    JOptionPane.showMessageDialog(this,"Time is up "+jTextName.getText()+"! \nSorry! You lost in "+numclick+" number of Clicks", "Game Over",JOptionPane.CANCEL_OPTION);
    btTimer.setEnabled(false);
    }
    
    }
    
    void timerGame_actionPerformed(ActionEvent e)
    {
    timeLeft--;
    if (timeLeft==-1)
    timerGame.stop();
    else
    this.jTextField1.setText(timeLeft+"");
    if(timeLeft==0)
    {
    endGame(false);
    for (int i=0; i<16; i++)
    bt[i].setEnabled(false);
    }
    }
    
    // Start game
    void btTimer_actionPerformed(ActionEvent e)
    {
    timeStart = Integer.parseInt(this.jTextField1.getText());
    for (int i=0; i<16; i++)
    bt[i].setEnabled(true);
    jTextField1.setEnabled(false);
    name= JOptionPane.showInputDialog(null,"Please Enter Your Name:", "Welcome to the Memory Game!", JOptionPane.CANCEL_OPTION);
    jTextName.setText(name);
    timeLeft = Integer.parseInt(this.jTextField1.getText());
    timerGame.start();
    btTimer.setEnabled(false);
    }
    
    // Instruction button
    void jMenuInstructions_actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(this,"Instructions \n- Each card in the memory game hides a picture. \n- Click on the cards to reveal a picture and remember their positions to match all pairs within the time given.","How to Play",JOptionPane.PLAIN_MESSAGE);
    }
    
    // New game functionality
    public void jMenuNewGame_actionPerformed(ActionEvent e)
    {
    /*
    timerGame.start();
    timeStart = 120;
    numclick = 0;
    count=bt.length;
    for (int i = 0; i < 100; i++)
    {
    //randomly pick two position
    int p1 = r.nextInt(16);
    int p2 = r.nextInt(16);
    //swap the two position
    int temp = no[p1];
    no[p1] = no[p2];
    no[p2] = temp;
    }
    
    new JavaMemory();
    for (int i=0; i<=16;i++)
    {
    bt[i].setIcon(null);
    bt[i].setEnabled(true);
    }
    timeLeft = 120;
    timerGame.start();*/
    }
    
    // To exit game
    void Quit_actionPerformed(ActionEvent e)
    {
    jTextName.setText("");
    jTextGameInfo.setText("");
    JOptionPane.showMessageDialog(this," Press OK to leave this game!","Quit Game",JOptionPane.CANCEL_OPTION);
    System.exit(0);
    }
    
    // This part of code is the main method to execute the program
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new JavaMemory().setVisible(true);
    }
    });
    
    }
    
    public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
    }
    Last edited by Fubarable; 08-23-2009 at 05:10 PM. Reason: code tags added, but pasted code not formatted

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

    Default

    Suggestions to help you get help:
    1) The class is too large and large classes are difficult for anyone to debug and for others (us) to understand. So try to break it up into smaller more manageable classes.
    2) When posting code, please post formatted code and use code tags. This will make it much easier for others (us) to read your code, understand it, and help you, and this will then increase your chances of getting decent help.
    3) Sprinkle your code generously with println statements that let you know the state of certain variables as the program is running. This will help you to debug what is not working.

    Best of luck!

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

    Default

    Other suggestions:

    1) You've got an array of buttons that you could use to greater effect. In fact your program doesn't need the variables bt1 - bt16. Instead wherever you see these, use the array and for loops instead. This will simplify your code and make it much easier to debug. For instance, you'll only have one actionPerformed method to deal with instead of 16.
    2) Avoid null layout and absolute positioning. This app is tailor-made for placing the buttons in a JPanel that uses a GridLayout(4, 4). Doing this, you'd change this:
    Java Code:
    // Declare and initialise buttons
    private JButton bt[] = new JButton[16];
    private JButton bt1 = new JButton();
    private JButton bt2 = new JButton();
    private JButton bt3 = new JButton();
    private JButton bt4 = new JButton();
    private JButton bt5 = new JButton();
    private JButton bt6 = new JButton();
    private JButton bt7 = new JButton();
    private JButton bt8 = new JButton();
    private JButton bt9 = new JButton();
    private JButton bt10 = new JButton();
    private JButton bt11 = new JButton();
    private JButton bt12 = new JButton();
    private JButton bt13 = new JButton();
    private JButton bt14 = new JButton();
    private JButton bt15 = new JButton();
    private JButton bt16 = new JButton();
    
    //.... code deleted
    
    bt1.setBounds(new Rectangle(75, 101, 64, 62));
    bt1.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt1_actionPerformed(e);
    }
    
    });
    
    bt2.setBounds(new Rectangle(139, 101, 64, 62));
    bt2.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt2_actionPerformed(e);
    }
    });
    
    // .... LOTS of code deleted here
    
    bt15.setBounds(new Rectangle(203, 287, 64, 62));
    bt15.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt15_actionPerformed(e);
    }
    
    });
    
    bt16.setBounds(new Rectangle(267, 287, 64, 62));
    bt16.addActionListener(new java.awt.event.ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    bt16_actionPerformed(e);
    }
    
    });
    to this:
    Java Code:
    // in variable declaration section
    private JButton bt[] = new JButton[16]; // or could use a 2-dimensional array here
    
    // in init method or constructor:
    JPanel gridPanel = new JPanel(new GridLayout(4, 4,));
    for (int i = 0; i < bt.length; i++)
    {
      bt[i] = new JButton();
      bt[i].setActionCommand(String.valueOf(i)); // one way to identify which button pressed...
      bt[i].setPreferredSize(new Dimension(64, 62)); // you'll need to call pack on JFrame
      bt[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
          btnActionPerformed(e);
        }
      });
      gridPanel.add(bt[i]);
    }
    (note: code has not been compiled nor tested)

    which do you feel is easier to debug and maintain?
    Last edited by Fubarable; 08-23-2009 at 06:13 PM.

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    I think this is the issue:

    Java Code:
    ImageIcon img = new ImageIcon(no[0]+"tiger.gif");

    You are asking it to open a file that is located in your working directory, where ever that may be:

    Java Code:
    //it is looking for the images here:
    File file = new File(".");
    System.out.println(file.getAbsolutePath());

    Instead open the file using the path of your existing class:

    Java Code:
    ImageIcon img = new ImageIcon(this.getClass().getResource(no[0]+"tiger.gif"));

    That should open the image files granted they are in the same directory as your compiled .class files. If you "build" your project in netbeans it should copy over those images. After you build, then run it.

  5. #5
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    Fubarable, thanks for your guideline, I will try to improve the code.

    mrmatt1111, I use the way you teach me by trying the code
    ImageIcon img = new ImageIcon(this.getClass().getResource(no[0]+"tiger.gif"));
    I have save in the same location as my folder, but still no image coming out...

  6. #6
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    can someone offer their help.....

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

    Default

    Suggestion: put your big app aside and just work on creating a simple small JFrame that holds nothing but a single JLabel holding an ImageIcon and see if you can display an image in that image icon. If you can't then post this small program and also let us know where the image file is located in relationship to this program's class files.

  8. #8
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    Below is the code:

    Java Code:
    public void setImageIcon()
    	{
    		for (int i = 0; i < 2; i++)
    		{
    			for (int ii = 1; ii < (cardButton.length / 2) + 1; ii++)
    			{
    				gameList.add(ii);
    			}	
    		}
    		
    		/*shuffle(gameList);
    		
    		int newLine = 0;
    		for (int a = 0; a < gameList.size(); a++)
    		{
    			newLine++;
    		
    			if (newLine == 4)
    			{			
    				newLine = 0;
    			}
    		}*/
    	}
    	
    	public boolean sameValues()
    	{
    		if (btnValue[0] == btnValue[1])
    		{
    			return true;
    		}
    		return false;
    	}		
    	
    	class displayHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			for (int i = 0; i < cardButton.length; i++)
    			{
    				if (cardButton[i] == e.getSource())
    				{
    					cardButton[i].setIcon(p1);
    					cardButton[i].setEnabled(false);
    					counter++;
    					
    				if (counter == 3)
    				{
    					if (sameValues())
    					{
    						cardButton[btnID[0]].setEnabled(false);
    						cardButton[btnID[1]].setEnabled(false);
    					}
    					else
    					{
    						cardButton[btnID[0]].setEnabled(true);
    						cardButton[btnID[0]].setIcon(p3);
    						cardButton[btnID[1]].setEnabled(true);
    						cardButton[btnID[1]].setIcon(p2);
    					}
    					counter = 1;
    				}
    				
    				if (counter == 1)
    				{
    					btnID[0] = i;
    					btnValue[0] = gameList.get(i);
    				}
    				
    				if (counter == 2)
    				{
    					btnID[1] = i;
    					btnValue[1] = gameList.get(i);
    				}
    								
    			   }
    		    }	
    		}
    				
    	}

    How could I store image in that?
    where to put
    Java Code:
    ImageIcon icon = new Imageicon ("tiger.gif");

  9. #9
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    Fubarable,
    can you help me again?
    Cause I am totally new with JAVA

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

    Default

    Again, as suggested above, create and post a small compilable, runnable program that attempts to show an image in an ImageIcon on a JLabel. I don't see that you've done this yet.

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

    Default

    For instance:
    Java Code:
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ImageIconEg {
      private static final String IMAGE_URL = "https://duke.dev.java.net/" +
            "images/boxer/boxer.gif";
    
      private static void createAndShowGUI() {
        // get an image from the net
        URL url = null;
        BufferedImage image = null;
        try {
          url = new URL(IMAGE_URL);
          image = ImageIO.read(url);
        } catch (MalformedURLException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        
        JLabel label = new JLabel();
        // put it in an ImageIcon and place that in a JLabel
        if (image != null) {
          label.setIcon(new ImageIcon(image));
          label.setPreferredSize(
              new Dimension(image.getWidth(), image.getHeight()));
        }
    
        JFrame frame = new JFrame("ShowPictureEg Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(label);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }
    }

  12. #12
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    thanks you ^^

  13. #13
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    I am also trying to make a memory game in java code. And my problem is that i cannot hide the pictures. You'd you please help me. I really want to understand Java. Thank you. Here is my code:

    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.*;
    import java.util.Random;
    
    public class GuessMe2 extends JFrame implements ActionListener {
    	
    	JPanel wholePanel, firstPanel, secondPanel;
    	JButton newButton, quitButton, scoreButton, levelButton, learnButton;			
    	ImageIcon[] pictureIcon = new ImageIcon[12];
    	JButton[] pictureButton = new JButton[12];
    	ArrayList<Integer> ImageList = new ArrayList<Integer>();
    	
    	int i;
    	//mga declarations
    
    
    	public GuessMe2(){
    		Container c = getContentPane();
    		panel ();				
    		firstPanelButtons ();		
    		secondPanelButtons ();					
    		c.add(wholePanel); //dapat i-add sa container yung panel para makita
    		
    	}
    	
    	public void panel(){
    		wholePanel = new JPanel();
    		firstPanel = new JPanel();
    		secondPanel = new JPanel();
    		
    		wholePanel.setLayout(new BorderLayout ());
    		firstPanel.setLayout(new FlowLayout());
    		secondPanel.setLayout(new FlowLayout());
    		
    		wholePanel.add(firstPanel, BorderLayout.NORTH); //para sa taas
    		wholePanel.add(secondPanel, BorderLayout.CENTER); //center para i-fill up		
    	}	
    		
    	public void firstPanelButtons (){
    		newButton = new JButton ("New Game");
    		newButton.addActionListener(this);		
    		scoreButton = new JButton ("Score");
    		scoreButton.addActionListener(this);
    		levelButton = new JButton ("Level");
    		levelButton.addActionListener(this);
    		learnButton = new JButton("Instructions");
    		learnButton.addActionListener(this);
    		quitButton = new JButton ("Quit Game");
    		quitButton.addActionListener(this);		
    		firstPanel.add(newButton);
    		firstPanel.add(learnButton);
    		firstPanel.add(levelButton);
    		firstPanel.add(scoreButton);
    		firstPanel.add(quitButton);	
    		}
    		
    	public void secondPanelButtons (){
    	
    		initPictureIcons();		
    
    		for(int i=0; i<pictureButton.length; i++){
    			pictureButton[i]=new JButton(pictureIcon[i]);
    			pictureButton[i].addActionListener(this);
    		}
    
    		for(int i=0; i<pictureButton.length; i++){
    			secondPanel.add(pictureButton[i]);
    		}
    		
    		
    	}
    		
    	public void initPictureIcons (){
    		pictureIcon[0]=new ImageIcon("http://www.java-forums.org/images/U1.jpg");
    		pictureIcon[1]=new ImageIcon("http://www.java-forums.org/images/U2.jpg");
    		pictureIcon[2]=new ImageIcon("http://www.java-forums.org/images/U3.jpg");
    		pictureIcon[3]=new ImageIcon("http://www.java-forums.org/images/U4.jpg");
    		pictureIcon[4]=new ImageIcon("http://www.java-forums.org/images/U5.jpg");
    		pictureIcon[5]=new ImageIcon("http://www.java-forums.org/images/U6.jpg");
    		pictureIcon[6]=new ImageIcon("http://www.java-forums.org/images/U1.jpg");
    		pictureIcon[7]=new ImageIcon("http://www.java-forums.org/images/U2.jpg");
    		pictureIcon[8]=new ImageIcon("http://www.java-forums.org/images/U3.jpg");
    		pictureIcon[9]=new ImageIcon("http://www.java-forums.org/images/U4.jpg");
    		pictureIcon[10]=new ImageIcon("http://www.java-forums.org/images/U5.jpg");
    		pictureIcon[11]=new ImageIcon("http://www.java-forums.org/images/U6.jpg");
    	}
    		
    
    		
    	public void actionPerformed(ActionEvent e){
    
    	for (int i=0; i<pictureButton.length; i++){
    			if(pictureButton[i]==e.getSource()){
    				pictureButton[i].setEnabled(false);
    				//pictureButton[i]= add (new JButton (pictureIcon[i]); i tried this to hide the pictures.
    
    			}else if(pictureButton[i]!=e.getSource()){
    				pictureButton[i].setEnabled(true);
    			}
    		}		
    		
    		
    	}	
    	
    	public static void main(String args[]){
    		GuessMe2 application = new GuessMe2();
    	//	Toolkit kit = Toolkit.getDefaultToolkit();
    	//	Image img = kit.getImage("any picture na maliit para icon sa taas");
    	//	application.setIconImage(img); 
    		application.setTitle("Guess Me");
    		application.setSize(800,700);
    		application.setVisible(true);		
    		application.setResizable(true); //para di nababago size
    
    	}
    }
    Last edited by Fubarable; 10-17-2009 at 04:44 AM. Reason: code tags added to aid readability

  14. #14
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    please...
    i need someone to reply on this.
    i keep on trying of hiding the image but i can't
    i cannot move on with my code without knowing how to hide the images.
    please...
    i would really really appreciate your help.
    thank you

  15. #15
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    would you please help me?
    it is my first time in Java

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

    Default

    To increase your chances of getting help, you should not hijack an old thread but instead should create your own thread.

    Also, if I were you, I'd try to simplify the problem as much as possible. Your problem is one of displaying and hiding pictures. I would recommend that you try to work on this on a single JLabel or button. My own solution to this is to create a class that holds a JPanel (the container) that uses a CardLayout and have the panel first display a jbutton without image. When the button is pressed, the cardlayout swaps the jbutton for a jlabel that then displays the image.

    Also, when posting code, please use code tags. I've added tags to your post above.

    Much luck.

  17. #17
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
    setEnabled(false);
    by putting this code, you could hide your picture after the correct match.

  18. #18
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    to lclclc... thank you, but how could flip down the card if the images don't match?

  19. #19
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    to Fubarable, thank you. I understand how to show the image but I don't know how to check the images if they are the same or not. I need to flip down the cards if they are of different images. i am sorry if I have followed this thread. i thought it would be helpful since this is also related to java. Anyway, I will start a new thread for my java code problem.

  20. #20
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
    setEnabled(true);
    this will close up the image, without shadowing the image.

Similar Threads

  1. Help to make memory game :=)
    By arian88 in forum AWT / Swing
    Replies: 7
    Last Post: 10-15-2009, 06:23 AM
  2. Memory Game
    By torres in forum AWT / Swing
    Replies: 6
    Last Post: 04-21-2009, 11:00 PM
  3. help with memory game!
    By rac in forum New To Java
    Replies: 6
    Last Post: 04-13-2009, 11:39 PM
  4. How to over come java.lang.out of memory error
    By Chaitra_gm in forum New To Java
    Replies: 5
    Last Post: 09-28-2008, 10:33 AM
  5. Replies: 2
    Last Post: 08-21-2008, 07:33 PM

Posting Permissions

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