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

Java Discussion :

interface graphique java jeu bubble


Sujet :

Java

  1. #1
    Membre à l'essai
    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
    Points : 14
    Points
    14
    Par défaut interface graphique java jeu bubble
    bonjour,
    Je rencontre un problème avec ce code,ce dernier permet de supprimer deux boutons adjacents.
    il me fait sortir une exception lorsque je clique sur une bouton à l’extrémité de la fenêtre !!!

    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
    169
    170
    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;
     
                			}
                        }//end inner for
                }//end for
                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(b[i][j].getBackground() == b[i+1][j].getBackground() && i<=9){
    						b[i+1][j].addActionListener(this);
     
    						remove(b[i+1][j]);
    						revalidate();
    						repaint();
     
    					}
    					else System.out.println("non");
    						if(b[i][j].getBackground() == b[i-1][j].getBackground() && i>0){
    							b[i-1][j].addActionListener(this);
     
    							remove(b[i-1][j]);
    							revalidate();
    							repaint();
    						}
    						else System.out.println("non");
    						if(b[i][j].getBackground() == b[i][j+1].getBackground() && j<=9){
    							b[i][j+1].addActionListener(this);
     
    							remove(b[i][j+1]);
    							revalidate();
    							repaint();
    						}
    						else System.out.println("non");
    						if(b[i][j].getBackground() == b[i][j-1].getBackground() && j>0){
    							b[i][j-1].addActionListener(this);
     
    							remove(b[i][j-1]);
    							revalidate();
    							repaint();
     
    					}
    						else System.out.println("non");
    						remove(b[i][j]);
     
    						revalidate();
    						repaint();
    				}
    			}
    		}
     
     
     
     
     
     
     
    	}
     
    }
    comment peut-on le modifier pour qu'il ne fait sortira pas d'exception et il supprimera tous les boutons adjacents??
    Et merci d'avance

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Citation Envoyé par nadhem17 Voir le message
    il me fait sortir une exception lorsque je clique sur une bouton à l’extrémité de la fenêtre !!!
    Tu peux poster les stacktraces de l'exception?

    A+.

  3. #3
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 084
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

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

    Informations forums :
    Inscription : Août 2006
    Messages : 4 084
    Points : 7 996
    Points
    7 996
    Par défaut
    Ton code contient des boucles qui vont de 0 a 10 et dedans tu fais des access du genre

    hors, b[i][10+1], ca provoque une exception du genre indexoutofbounds. Donc il faut eviter d'aller jouer en dehors du tableau.

    même chose si tu avais tableau[0-1] ca n'irait pas non plus.

    Tu as pourtant essayer de ne pas sortir du tableau avec des lignes genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(b[i][j].getBackground() == b[i+1][j].getBackground() && i<=9){
    ou tu vérifies que i < 9 et i > 0 mais ... mais tu accèdes au tableau avant de passer au test i < 9 donc le problème est la.

    donc soit faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    if (i<=9)
    if(b[i][j].getBackground() == b[i+1][j].getBackground())
    ou

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(i<=9 && b[i][j].getBackground() == b[i+1][j].getBackground())

  4. #4
    Membre à l'essai
    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
    Points : 14
    Points
    14
    Par défaut
    Citation Envoyé par andry.aime Voir le message
    Bonjour,



    Tu peux poster les stacktraces de l'exception?

    A+.
    cv le pbm est résolue!!
    l'erreur est dans le test:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(i<9 && b[i][j].getBackground() == b[i+1][j].getBackground())
    Au lieu de:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(b[i][j].getBackground() == b[i+1][j].getBackground() && i<=9)

  5. #5
    Membre à l'essai
    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
    Points : 14
    Points
    14
    Par défaut
    bonsoir,
    j'ai un autre problème aussi dans la méthode actionPerformed!!!
    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!!

Discussions similaires

  1. Interface graphique java
    Par clubiste012 dans le forum Débuter
    Réponses: 3
    Dernier message: 11/03/2009, 16h48
  2. matrice interface graphique JAVA
    Par saidoumosta dans le forum Interfaces Graphiques en Java
    Réponses: 1
    Dernier message: 26/01/2009, 11h15
  3. Interface graphique java swing awt toolkit
    Par MaxLaMenaX dans le forum AWT/Swing
    Réponses: 6
    Dernier message: 06/11/2008, 15h39
  4. Interface graphique pour jeu de Sudoku
    Par Stradithehutt dans le forum Interfaces Graphiques
    Réponses: 5
    Dernier message: 01/11/2006, 10h43
  5. Composants à utiliser pour une interface graphique Java
    Par nicolas.pied dans le forum Composants
    Réponses: 4
    Dernier message: 28/11/2005, 20h27

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