IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Interfaces Graphiques en Java Discussion :

Problème addActionListener Java jeu Bubble


Sujet :

Interfaces Graphiques en Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Par défaut Problème addActionListener Java jeu Bubble
    bonsoir,

    j'ai un petit problème avec ActionPerformed ,je le maîtrise pas bien ,bref j'arrive pas a savoir pourquoi addactionlistener ne marche lorsque je l'utilise dans la méthode ActionPerformed ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
     
    public class Fenetre extends JFrame implements ActionListener {
     
    	JButton b[][];
     
    	int[][] grid = new int[10][10]; 
    	public int n = 10;
        public int m = 10;
     
    	public Fenetre(){
     
    		this.setTitle("bubble");
     
    		this.setLocationRelativeTo(null);
     
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                b = new JButton [n][m];
                setLayout(new GridLayout(n,m));
                for (int y = 0;y<m;y++){
                        for (int x = 0;x<n;x++){
                        	Random rd = new Random();
                			int r = rd.nextInt(5);
                			switch (r){
                			case 0:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.YELLOW);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                b[x][y].setActionCommand(getName());
                                break;
                			case 1:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.MAGENTA);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 2:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.BLUE);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 3:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.GREEN);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 4:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.RED);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 5:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.ORANGE);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
     
                			}
                        }
                }
                pack();
                setVisible(true);
                start();
     
        }
     
    	public int getN(){
        	return(n);
        	}
    	public int getM(){
        	return(m);
        	}
     
    	public void start(){
    		Component temporaryLostComponent = null;
     
    	}
     
     
     
     
    	public static void main(String[] args) {
     
    		Fenetre window = new Fenetre();
    		window.setSize(600, 500);
     
    	}
     
     
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
     
    		JButton c = (JButton)e.getSource();
    		for (int i = 0; i < 10; i++) {
    			for (int j = 0; j < 10; j++) {
    				if(c == b[i][j]){
    					System.out.println("i= "+i+"j= "+j);
    					if(i<9 && b[i][j].getBackground() == b[i+1][j].getBackground()){
    						b[i+1][j].addActionListener(this);
     
    						remove(b[i+1][j]);
     
    						repaint();
     
    					}
    					else System.out.println("non");
    					if(i>0 && b[i][j].getBackground() == b[i-1][j].getBackground()){
    							b[i-1][j].addActionListener(this);
     
    							remove(b[i-1][j]);
     
    							repaint();
    						}
    						else System.out.println("non");
    					if(j<9 && b[i][j].getBackground() == b[i][j+1].getBackground()){
    							b[i][j+1].addActionListener(this);							
    							remove(b[i][j+1]);						
    							repaint();
    						}
    						else System.out.println("non");
    					if(j>0 && b[i][j].getBackground() == b[i][j-1].getBackground()){
    							b[i][j-1].addActionListener(this);
     
    							remove(b[i][j-1]);
     
    							repaint();
     
    					}
    						else System.out.println("non");
    						remove(b[i][j]);
     
    						repaint();
    				}
    			}
    		}
     
     
     
     
     
     
     
    	}
     
    }
    Normalement lorsqu'il trouve un bouton de même couleur et adjacent il le supprime!!mais dans ce code il ne le supprime pas!! pourquoi?

    et merci d'avance!!

  2. #2
    Modérateur
    Avatar de wax78
    Homme Profil pro
    R&D - Palefrenier programmeur
    Inscrit en
    Août 2006
    Messages
    4 096
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : R&D - Palefrenier programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 096
    Par défaut
    Ca ne marche sans doute pas comme il faut, mais en tout cas chez moi ca supprime bien certaines cases adjacentes. Par contre j'ai l'impression que tu rajoutes a chaque fois des listener dans l'actionperformed, ce qui me sembe bizzare.
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Par défaut
    Citation Envoyé par wax78 Voir le message
    Ca ne marche sans doute pas comme il faut, mais en tout cas chez moi ca supprime bien certaines cases adjacentes. Par contre j'ai l'impression que tu rajoutes a chaque fois des listener dans l'actionperformed, ce qui me sembe bizzare.
    j'ai modifié le code par une méthode récursive !!je pense que c'est la meilleur solution pour qu'il supprime tous les boutons adjacents !!Mais..elle ne marche pas !!il me fait sortir des exceptions!!J'ai pas compris pourquoi!!et comment je vais les corriger !!

    voila mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
     
    public class Fenetre extends JFrame implements ActionListener {
     
    	JButton b[][];
     
    	int[][] grid = new int[10][10]; 
    	public int n = 10;
        public int m = 10;
        public static int i = 0;
        public static int j = 0;
     
    	public Fenetre(){
     
    		this.setTitle("bubble");
     
    		this.setLocationRelativeTo(null);
     
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                b = new JButton [n][m];
                setLayout(new GridLayout(n,m));
                for (int y = 0;y<m;y++){
                        for (int x = 0;x<n;x++){
                        	Random rd = new Random();
                			int r = rd.nextInt(5);
                			switch (r){
                			case 0:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.YELLOW);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                b[x][y].setActionCommand(getName());
                                break;
                			case 1:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.MAGENTA);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 2:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.BLUE);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 3:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.GREEN);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 4:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.RED);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
                			case 5:
                                b[x][y] = new JButton("x= "+x+"y= "+y);
                                b[x][y].setBackground(Color.ORANGE);
                                b[x][y].addActionListener(this);
                                add(b[x][y]);
                                b[x][y].setEnabled(true);
                                break;
     
                			}
                        }
                }
                pack();
                setVisible(true);
     
     
        }
     
    	public int getN(){
        	return(n);
        	}
    	public int getM(){
        	return(m);
        	}
     
     
     
     
     
     
    	public static void main(String[] args) {
     
    		Fenetre window = new Fenetre();
    		window.setSize(600, 500);
     
    	}
     
    	public void btnRec (int i,int j){
    		if(i<9 && b[i][j].getBackground() == b[i+1][j].getBackground()){
    			btnRec(i+1,j);
    			remove(b[i+1][j]);
    			repaint();
    		}
    		if(i>0 && b[i][j].getBackground() == b[i-1][j].getBackground()){
    			btnRec(i+1,j);
    			remove(b[i-1][j]);
    			repaint();
    		}
    		if(j<9 && b[i][j].getBackground() == b[i][j+1].getBackground()){
    			btnRec(i,j+1);
    			remove(b[i][j+1]);
    			repaint();
    		}
    		if(j>0 && b[i][j].getBackground() == b[i][j-1].getBackground()){
    			btnRec(i,j-1);
    			remove(b[i][j-1]);
    			repaint();
    		}
    	}
     
     
     
    	public void actionPerformed(ActionEvent e) {
     
    		JButton c = (JButton)e.getSource();
    		for (i = 0; i < 10; i++) {
    			for (j = 0; j < 10; j++) {
    				if(c == b[i][j]){
    					System.out.println("i= "+i+"j= "+j);
    					btnRec(i,j);
    				}
    			}
    		}
     
     
     
     
     
     
     
    	}
     
    }
    et merci d'avance

  4. #4
    Modérateur
    Avatar de wax78
    Homme Profil pro
    R&D - Palefrenier programmeur
    Inscrit en
    Août 2006
    Messages
    4 096
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : R&D - Palefrenier programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 096
    Par défaut
    Probablement que tu revisites une case que tu as précédemment visitée. Et donc la récursion tourne en boucle et donc stackoverflow.
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  5. #5
    Membre chevronné
    Homme Profil pro
    Inscrit en
    Janvier 2010
    Messages
    312
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 312
    Par défaut
    Bonjour,

    Voici une methode recurcive qui fonctionne.

    L'idée est de créer une liste de bouton que l'on remplit avec la méthode rec des boutons ayant la meme couleur à supprimer.

    la methode rec teste les boutons adjacents et s'ils ne sont pas dans la list et on la même couleur, les y ajoute.

    A la fin on remove les bouton de la liste


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
     
    public class Fenetre extends JFrame implements ActionListener {
     
    	JButton b[][];
    	ArrayList<JButton> list = new ArrayList<JButton>();
    	int[][] grid = new int[10][10]; 
    	public int n = 10;
            public int m = 10;
     
    	public Fenetre(){
    		this.setTitle("bubble");
    		this.setLocationRelativeTo(null);
                    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    b = new JButton [n][m];
                    setLayout(new GridLayout(n,m));
                    for (int y = 0;y<m;y++){
                        for (int x = 0;x<n;x++){
                        	Random rd = new Random();
                	        int r = rd.nextInt(5);
                		switch (r){
                			case 0:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.YELLOW);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           b[x][y].setActionCommand(getName());
                                           break;
                			case 1:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.MAGENTA);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 2:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.BLUE);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 3:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.GREEN);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 4:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.RED);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 5:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.ORANGE);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			}
                        }
                }
                pack();
                setVisible(true);
                start();
        }
     
        public int getN(){
        	return(n);
        }
        public int getM(){
        	return(m);
        }
        public void start(){
            Component temporaryLostComponent = null;
        }  
        public static void main(String[] args) {
    	Fenetre window = new Fenetre();
    	window.setSize(650, 500);
        }
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		list.clear();
    		JButton c = (JButton)e.getSource();
    		rec(c);
    		for(int i=0;i<list.size();i++) {
    			remove(list.get(i));	
    		}
    		repaint();
    	}
    	public void rec(JButton c) {
    			list.add(c);
    			System.out.println(c.getText());
    			String[] xy = c.getText().split(" ");
    			int x = Integer.parseInt(xy[0]);
    			int y = Integer.parseInt(xy[1]);
    			if (x < 9 && b[x][y].getBackground() == b[x+1][y].getBackground() && !list.contains(b[x+1][y])){
    				rec(b[x+1][y]);
    			}
    			if (x > 0 && b[x][y].getBackground() == b[x-1][y].getBackground() && !list.contains(b[x-1][y])){
    				rec(b[x-1][y]);
    			}
    			if (y < 9 && b[x][y].getBackground() == b[x][y+1].getBackground() && !list.contains(b[x][y+1])){
    				rec(b[x][y+1]);
    			}
    			if (y > 0 && b[x][y].getBackground() == b[x][y-1].getBackground() && !list.contains(b[x][y-1])){
    				rec(b[x][y-1]);
    			}
    	}
    }

  6. #6
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Par défaut
    Citation Envoyé par ludomacho Voir le message
    Bonjour,

    Voici une methode recurcive qui fonctionne.

    L'idée est de créer une liste de bouton que l'on remplit avec la méthode rec des boutons ayant la meme couleur à supprimer.

    la methode rec teste les boutons adjacents et s'ils ne sont pas dans la list et on la même couleur, les y ajoute.

    A la fin on remove les bouton de la liste


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
     
    public class Fenetre extends JFrame implements ActionListener {
     
    	JButton b[][];
    	ArrayList<JButton> list = new ArrayList<JButton>();
    	int[][] grid = new int[10][10]; 
    	public int n = 10;
            public int m = 10;
     
    	public Fenetre(){
    		this.setTitle("bubble");
    		this.setLocationRelativeTo(null);
                    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    b = new JButton [n][m];
                    setLayout(new GridLayout(n,m));
                    for (int y = 0;y<m;y++){
                        for (int x = 0;x<n;x++){
                        	Random rd = new Random();
                	        int r = rd.nextInt(5);
                		switch (r){
                			case 0:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.YELLOW);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           b[x][y].setActionCommand(getName());
                                           break;
                			case 1:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.MAGENTA);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 2:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.BLUE);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 3:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.GREEN);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 4:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.RED);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			case 5:
                                           b[x][y] = new JButton(x+" "+y);
                                           b[x][y].setOpaque(true);
                                           b[x][y].setBackground(Color.ORANGE);
                                           b[x][y].addActionListener(this);
                                           add(b[x][y]);
                                           b[x][y].setEnabled(true);
                                           break;
                			}
                        }
                }
                pack();
                setVisible(true);
                start();
        }
     
        public int getN(){
        	return(n);
        }
        public int getM(){
        	return(m);
        }
        public void start(){
            Component temporaryLostComponent = null;
        }  
        public static void main(String[] args) {
    	Fenetre window = new Fenetre();
    	window.setSize(650, 500);
        }
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		list.clear();
    		JButton c = (JButton)e.getSource();
    		rec(c);
    		for(int i=0;i<list.size();i++) {
    			remove(list.get(i));	
    		}
    		repaint();
    	}
    	public void rec(JButton c) {
    			list.add(c);
    			System.out.println(c.getText());
    			String[] xy = c.getText().split(" ");
    			int x = Integer.parseInt(xy[0]);
    			int y = Integer.parseInt(xy[1]);
    			if (x < 9 && b[x][y].getBackground() == b[x+1][y].getBackground() && !list.contains(b[x+1][y])){
    				rec(b[x+1][y]);
    			}
    			if (x > 0 && b[x][y].getBackground() == b[x-1][y].getBackground() && !list.contains(b[x-1][y])){
    				rec(b[x-1][y]);
    			}
    			if (y < 9 && b[x][y].getBackground() == b[x][y+1].getBackground() && !list.contains(b[x][y+1])){
    				rec(b[x][y+1]);
    			}
    			if (y > 0 && b[x][y].getBackground() == b[x][y-1].getBackground() && !list.contains(b[x][y-1])){
    				rec(b[x][y-1]);
    			}
    	}
    }
    Un grand merci à vous!!!
    ça fonctionne très bien!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème en Java - Jeu du Pong
    Par OmiCroh dans le forum Débuter avec Java
    Réponses: 0
    Dernier message: 29/12/2013, 00h23
  2. interface graphique java jeu bubble
    Par nadhem17 dans le forum Général Java
    Réponses: 4
    Dernier message: 30/04/2013, 18h46
  3. Problème console Java (runescape;jeu en ligne)
    Par diliodi dans le forum Général Java
    Réponses: 0
    Dernier message: 13/05/2011, 20h45
  4. Problème Applet Java et Win XP SP2
    Par octal dans le forum Applets
    Réponses: 4
    Dernier message: 04/02/2006, 09h57
  5. Problème avec un jeu
    Par CCin dans le forum C
    Réponses: 6
    Dernier message: 25/10/2005, 18h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo