Plateau d'Hexagones avec Java.awt et Java.swing
Bonjour,
Je souhaite générer des Hexagones dans ma fenêtre en les disposant selon les contraintes du GridBagLayout.
Ça fonctionne avec des JButtons (en activant la ligne 30 de la classe Plateau) mais rien ne s'affiche pour les Hexagones, impossible de trouver pourquoi...
Voici mon code :
Classe Hexagone
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JPanel;
public class Hexagone extends JPanel{
public Hexagone(){}
public void paintComponent(Graphics g){
int x[] = {20, 30, 50, 60, 50, 30};
int y[] = {40, 20, 20, 40, 60, 60};
g.drawPolygon(x, y, 6);
}
} |
Classe Initialisation
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
import javax.swing.JFrame;
public class Initialisation {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Plateau(1));
f.setVisible(true);
}
} |
Classe Plateau
Code:
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
|
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Plateau extends JPanel {
public Plateau(int taille){
int ligne = 3;
int colonne = 5;
if(taille == 1){
ligne = 3;
colonne = 5;
}
else{
ligne = ligne+2*(taille-1);
colonne = 5+4*2*(taille-1);
}
int nbHexa = ligne*colonne;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
for(int i=0; i<nbHexa; i++){
gbc.gridx = i;
gbc.gridy = i;
//add(new JButton("test"),gbc);
add(new Hexagone(), gbc);
}
}
} |