Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help me in creating memory game

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help me in creating memory game

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;

    public class memgame extends JFrame
    {
    Timer myTimer;
    int first,second;
    String tiles[]={"red.jpg","pink.jpg","blue.jpg","grey.jpg","viol et.jpg","orange.jpg","yellow.jpg","green.jpg"};
    ImageIcon close;
    ImageIcon icons[];
    int Buttons;
    JButton buttons[];

    public memgame()
    {
    super("Memory Game");
    setLayout(new GridLayout(4, tiles.length));
    close = new ImageIcon("close.jpg");
    Buttons = tiles.length*4;
    buttons = new JButton[Buttons];
    icons = new ImageIcon[Buttons];


    for(int a=0,b=0;a<tiles.length;a++)
    {
    icons[b] = new ImageIcon(tiles[a]);
    buttons[b] = new JButton("");
    buttons[b].addActionListener(new memgame.ImageButtonListener());
    buttons[b].setIcon(close);
    add(buttons[b++]);

    icons[b] = icons[b - 1];
    buttons[b] = new JButton("");
    buttons[b].addActionListener(new memgame.ImageButtonListener());
    buttons[b].setIcon(close);
    add(buttons[b++]);
    }

    Random generator = new Random();
    for(int a=0 ; a<Buttons; a++)
    {
    int b = generator.nextInt(Buttons);
    ImageIcon temp = icons[b];
    icons[b] = icons[b];
    icons[b] = temp;
    }
    pack();
    setVisible(true);
    myTimer = new Timer(2000, new TimerListener());


    }
    private class TimerListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    myTimer.stop();
    myTimer.start();
    buttons[first].setIcon(close);
    buttons[second].setIcon(close);

    }

    }
    private class ImageButtonListener implements ActionListener
    {

    public void actionPerformed(ActionEvent e)
    {

    for (int a=0; a<Buttons; a++)
    {
    if (e.getSource() == buttons[a])
    {
    buttons[a].setIcon(icons[a]);
    }
    }
    }


    }
    public static void main(String[] args)
    {
    new memgame();
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help me in creating memory game

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link and ask a specific question.

Similar Threads

  1. Andorid Memory Matching game help
    By iTry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 9th, 2013, 06:02 AM
  2. Memory test game program !
    By sharpedge in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 10th, 2013, 11:41 AM
  3. Help with my memory concentration game (2D Array help)
    By lukeyry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2012, 08:33 AM
  4. Memory Card Game JButtons Array
    By rlo10 in forum What's Wrong With My Code?
    Replies: 26
    Last Post: March 4th, 2012, 05:02 PM
  5. Problem with Memory Card Game
    By rlo10 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 18th, 2012, 10:09 PM

Tags for this Thread