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();
}
}