Hello Guys , I need Help

Hello Java Programmers ,

i need help in my application ...

i'm doing a memory game application and it's almost done , but i just

need help in how to return the buttons closed again if the firstclick

does not equal the second click....

and how to say that you won after finishing the game ...

thank you very much for reading and i beg for help , thank u

here's my code :

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

public class Memory extends JFrame implements ActionListener

{

ImageIcon img[ ]=new ImageIcon[12];

public Memory()

{

img[0]=new ImageIcon("sahara.jpg");

img[1]=new ImageIcon("elvispresley.jpg");

img[2]=new ImageIcon("adamo.jpg");

img[3]=new ImageIcon("paulanka2.jpg");

img[4]=new ImageIcon("mike brant.jpg");

img[5]=new ImageIcon("lara2cov.jpg");

img[6]=new ImageIcon("italian job.jpg");

img[7]=new ImageIcon("ildivo.jpg");

img[8]=new ImageIcon("guitar.jpg");

img[9]=new ImageIcon("joedolan.jpg");

img[10]=new ImageIcon("damasco.jpg");

img[11]=new ImageIcon("chia.jpg");

JButton button[]=new JButton[24];

JPanel p1=new JPanel();

p1.setLayout(new GridLayout(4,2,5,5));

for(int i=0;i<button.length;i++){

button= new JButton(Integer.toString(i+1));

p1.add(button);

Container c=getContentPane();

c.add(p1);

}

setTitle("Memory Game");

setSize(700,700);

setVisible(true);

button[0].addActionListener(this);

button[11].addActionListener(this);

button[1].addActionListener(this);

button[7].addActionListener(this);

button[2].addActionListener(this);

button[5].addActionListener(this);

button[3].addActionListener(this);

button[6].addActionListener(this);

button[8].addActionListener(this);

button[10].addActionListener(this);

button[13].addActionListener(this);

button[19].addActionListener(this);

button[15].addActionListener(this);

button[21].addActionListener(this);

button[18].addActionListener(this);

button[20].addActionListener(this);

button[4].addActionListener(this);

button[9].addActionListener(this);

button[12].addActionListener(this);

button[14].addActionListener(this);

button[16].addActionListener(this);

button[22].addActionListener(this);

button[17].addActionListener(this);

button[23].addActionListener(this);

button[0].setActionCommand("0");

button[11].setActionCommand("0");

button[1].setActionCommand("1");

button[7].setActionCommand("1");

button[2].setActionCommand("2");

button[5].setActionCommand("2");

button[3].setActionCommand("3");

button[6].setActionCommand("3");

button[8].setActionCommand("4");

button[10].setActionCommand("4");

button[13].setActionCommand("5");

button[19].setActionCommand("5");

button[15].setActionCommand("6");

button[21].setActionCommand("6");

button[18].setActionCommand("7");

button[20].setActionCommand("7");

button[4].setActionCommand("8");

button[9].setActionCommand("8");

button[12].setActionCommand("9");

button[14].setActionCommand("9");

button[16].setActionCommand("10");

button[22].setActionCommand("10");

button[17].setActionCommand("11");

button[23].setActionCommand("11");

}

int counter = 0;

String firstClick;

String secondClick;

public void actionPerformed (ActionEvent e){

int x=Integer.parseInt(e.getActionCommand());

JButton o=(JButton)e.getSource();

o.setIcon(img[x]);

if(counter==0){

firstClick=e.getActionCommand();

counter ++;

}

if(counter==1){

secondClick=e.getActionCommand();

}

if(secondClick.equals(firstClick)){

}

else{

}

}

public static void main(String[ ]args){

new Memory();

}

}>

[4116 byte] By [aasta] at [2007-10-2 20:36:02]
# 1

i posted this in the forums some time back, but I can't find it now

it doesn't have a 'win' scenarion, but you'd just need to keep a track of the 'matches'

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

class MemoryMatch extends JFrame

{

MyButton[] btn = new MyButton[16];

ImageIcon[] icons = new ImageIcon[btn.length];

int buttonShowingIndex = -1;

public MemoryMatch()

{

icons[0] = new ImageIcon(getClass().getResource("Test0.gif"));

icons[1] = new ImageIcon(getClass().getResource("Test1.gif"));

icons[2] = new ImageIcon(getClass().getResource("Test2.gif"));

icons[3] = new ImageIcon(getClass().getResource("Test3.gif"));

icons[4] = new ImageIcon(getClass().getResource("Test4.gif"));

icons[5] = new ImageIcon(getClass().getResource("Test5.gif"));

icons[6] = new ImageIcon(getClass().getResource("Test6.gif"));

icons[7] = new ImageIcon(getClass().getResource("Test7.gif"));

icons[8] = icons[0];

icons[9] = icons[1];

icons[10] = icons[2];

icons[11] = icons[3];

icons[12] = icons[4];

icons[13] = icons[5];

icons[14] = icons[6];

icons[15] = icons[7];

Collections.shuffle(Arrays.asList(icons));

setLocation(300,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel(new GridLayout(4,4));

for(int x = 0; x < btn.length; x++)

{

btn[x] = new MyButton(icons[x],x);

mainPanel.add(btn[x]);

}

getContentPane().add(mainPanel);

pack();

}

class MyButton extends JButton

{

int index;

ImageIcon icon;

public MyButton(ImageIcon img, int i)

{

icon = img;

index = i;

setPreferredSize(new Dimension(150,150));

addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

if(getIcon() == null)

{

setIcon(icon);

if(buttonShowingIndex > -1)

{

if(icon != btn[buttonShowingIndex].getIcon())

{

final int bsi = buttonShowingIndex;

ActionListener al = new ActionListener(){

public void actionPerformed(ActionEvent ae){

MyButton.this.setIcon(null);

btn[bsi].setIcon(null);}};

javax.swing.Timer timer = new javax.swing.Timer(500,al);

timer.setRepeats(false);

timer.start();

}

buttonShowingIndex = -1;

}

else buttonShowingIndex = index;

}}});

}

}

public static void main(String[] args){new MemoryMatch().setVisible(true);}

}

Michael_Dunna at 2007-7-13 23:19:15 > top of Java-index,Java Essentials,Java Programming...