Professional Documents
Culture Documents
Mi Board
Mi Board
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
public MiBoard(){
this.inicialize();
//<editor-fold desc="InicializacionBotones">
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4 ; j++) {
botones[i][j]=new MyButton();
if(i==0){
if(j==0) {
botones[i][j].setBounds(0, 0, 175, 175);
}
else if(j==1){
botones[i][j].setBounds(175, 0, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 0, 175, 175);
}
else{
botones[i][j].setBounds(525, 0, 175, 175);
}
}
else if(i==1){
if(j==0) {
botones[i][j].setBounds(0, 175, 175, 175);
}
else if(j==1){
botones[i][j].setBounds(175, 175, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 175, 175, 175);
}
else{
botones[i][j].setBounds(525, 175, 175, 175);
}
}
else if(i==2){
if(j==0) {
botones[i][j].setBounds(0, 350, 175, 175);
}
else if(j==1){
botones[i][j].setBounds(175, 350, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 350, 175, 175);
}
else{
botones[i][j].setBounds(525, 350, 175, 175);
}
}
else{
if(j==0) {
botones[i][j].setBounds(0, 525, 175, 175);
}
else if(j==1){
botones[i][j].setBounds(175, 525, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 525, 175, 175);
}
else{
botones[i][j].setBounds(525, 525, 175, 175);
}
}
this.add(botones[i][j]);
botones[i][j].setEnabled(true);
}
}
//</editor-fold>
Collections.shuffle(cardaIds);
System.out.println(cardaIds);
it=cardaIds.iterator();
while(it.hasNext()){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
botones[i][j].setCard(new Card((int)it.next()));
System.out.println(botones[i][j].getCard().getId());
}
}
}
//INicializamos la etiqueta
numberCupless=new JLabel("Number of mached cards: 0");
numberCupless.setBounds(720,50,200,40);
this.add(numberCupless);
botonRestart=new JButton("Restart");
botonRestart.setBounds(770,150,100,20);
this.add(botonRestart);
botonWin=new JButton("Win");
botonWin.setBounds(770,220,100,20);
botonWin.setVisible(false);
this.add(botonWin);
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer){
contador++;
// System.out.println(contador);
if(contador==6){
botonWin.setVisible(true);
}
}
if(e.getSource()==botonRestart){
JOptionPane.showMessageDialog(null,"Restarting");
this.restart();
if(e.getSource()==botonWin){
JOptionPane.showMessageDialog(null,"You have won");
System.exit(1);
}
if(e.getSource()==boton){
cardsFliped++;
if(cardsFliped==2){
((MyButton) e.getSource()).setEnabled(false);
((MyButton) e.getSource()).setText("" + ((MyButton)
e.getSource()).getCard().getId());
boton.validate();
flipedCard.getCard().setSegund(true);
segundaCarta=boton;
if(((MyButton)
e.getSource()).getCard().equals(flipedCard.getCard())){
boton.getCard().mached=true;
flipedCard.getCard().mached=true;
mached(flipedCard,boton);
cardsFliped=0;
}
else{
if(boton.getCard().isSegund()) {
if(!(flipedCard.getCard().mached)) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
flipedCard.setText("");
flipedCard.setEnabled(true);
segundaCarta.setText("");
segundaCarta.setEnabled(true);
cardsFliped = 0;
}
boton.getCard().setSegund(false);
}
//notMached(flipedCard,boton);
boton.validate();
}
if(cardsFliped==1){
((MyButton) e.getSource()).setEnabled(false);
((MyButton) e.getSource()).setText("" + ((MyButton)
e.getSource()).getCard().getId());
flipedCard=((MyButton) e.getSource());
if(cardsFliped>2){
cardsFliped=2;
}
}
if(cardsMached==8){
JOptionPane.showMessageDialog(null,"You have won");
System.exit(1);
}
}
@Override
public void mouseClicked(MouseEvent e) {
@Override
public void mousePressed(MouseEvent e) {
@Override
public void mouseReleased(MouseEvent e) {
@Override
public void mouseEntered(MouseEvent e) {
if(e.getSource()==botonWin) {
@Override
public void mouseExited(MouseEvent e) {
it=cardaIds.iterator();
while(it.hasNext()){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
botones[i][j].setCard(new Card((int)it.next()));
botones[i][j].setText("");
System.out.println(botones[i][j].getCard().getId());
}
}
}
botonWin.setBounds(770,220,100,20);
botonWin.setVisible(false);
contador=0;
segundaCarta=null;
cardsFliped=0;
cardsMached=0;
}