Bonjour,
Si qqn pouvais me dire pourquoi je n ai pas ma nouvelle grille par apport a mon code ça serait super sympas merci!
Donc voila le souci:
Je crée un grille à partir de cellules.
Première fois tout est ok!
Ensuite dans l'application j'ai un JTexteField qui me permet de redimensionner cette grille en donnant le nombre de cellules total.
Donc la grille dois se rafraichir
Pas de chance ça bug PK?
Classe Jeu-->
Classe Grille -->
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 public class Jeu extends javax.swing.JFrame { // Déclaration des champs private int nbrCellules; /** Creates new form Jeu */ public Jeu() { initComponents(); grille.creationGrille(); } private void setNbrCellules() { String ch = txtNbrCellules.getText(); grille.setNbrCellules(Integer.parseInt(ch)); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { txtNbrCellules = new javax.swing.JTextField(); grille = new Grille(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); txtNbrCellules.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nombre Cellules", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N txtNbrCellules.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtNbrCellulesActionPerformed(evt); } }); grille.setPreferredSize(new java.awt.Dimension(500, 500)); javax.swing.GroupLayout grilleLayout = new javax.swing.GroupLayout(grille); grille.setLayout(grilleLayout); grilleLayout.setHorizontalGroup( grilleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 421, Short.MAX_VALUE) ); grilleLayout.setVerticalGroup( grilleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 343, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(grille, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE) .addComponent(txtNbrCellules, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(20, 20, 20)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(grille, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtNbrCellules, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(55, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void txtNbrCellulesActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setNbrCellules(); grille.creationGrille(); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Jeu().setVisible(true); } }); } // Variables declaration - do not modify private Grille grille; private javax.swing.JTextField txtNbrCellules; // End of variables declaration }
Classe Cellules -->
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 // Import des bibliothèques import java.awt.*; import javax.swing.*; public class Grille extends javax.swing.JPanel { // Déclaration des champs private GridLayout canevasGrille; private Cellule cellules; private int nbrCellules = 2500; private int tailleGrille; public Grille() { initComponents(); } public void setNbrCellules(int nbrCellules) { this.nbrCellules = nbrCellules; } private void calculTailleGrille() { tailleGrille = (int)Math.sqrt(nbrCellules); } public void creationGrille() { // Variables locale int cptLigne, cptCol; this.removeAll(); calculTailleGrille(); this.setLayout(new GridLayout(tailleGrille,tailleGrille)); for(cptLigne = 1; cptLigne <= tailleGrille; cptLigne++) { for(cptCol = 1; cptCol <= tailleGrille; cptCol++) { cellules = new Cellule(); this.add(cellules); } } repaint(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { setBackground(new java.awt.Color(0, 0, 0)); setMaximumSize(new java.awt.Dimension(500, 500)); setMinimumSize(new java.awt.Dimension(500, 500)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 500, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 500, Short.MAX_VALUE) ); }// </editor-fold> // Variables declaration - do not modify // End of variables declaration }
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 public class Cellule extends javax.swing.JPanel { public Cellule() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { setBackground(new java.awt.Color(255, 255, 255)); setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 66, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 63, Short.MAX_VALUE) ); }// </editor-fold> // Variables declaration - do not modify // End of variables declaration }
Partager