Amélioration de l'affichage, ajout de bouton

This commit is contained in:
2025-11-10 17:18:19 +01:00
parent 98ae6dc754
commit b115b88b5a

View File

@@ -1,55 +1,106 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Affiche extends JComponent {
public class Affiche extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int tabJeu[][] = {
{0,0,1,2,0,0,0,0,0},
{0,1,2,1,2,0,0,0,0},
{0,2,1,2,1,2,1,0,0},
{0,1,2,1,2,1,2,1,2},
{1,2,1,2,0,2,1,2,1},
{2,1,2,1,2,1,2,1,0},
{0,0,1,2,1,2,1,2,0},
{0,0,0,0,2,1,2,1,0},
{0,0,0,0,0,2,1,0,0}
};
if (isOpaque()) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
int pionTaille = 50;
int pas = 70;
int yBase = 60;
int xBase = 60;
Graphics2D pinceau = (Graphics2D) g.create();
public Affiche() {
setLayout(null);
int pionTaille = 50;
int pas = 70;
int yBase = 60;
int tabJeu[][] = {{0,0,1,2,0,0,0,0,0},
{0,1,2,1,2,0,0,0,0},
{0,2,1,2,1,2,1,0,0},
{0,1,2,1,2,1,2,1,2},
{1,2,1,2,0,2,1,2,1},
{2,1,2,1,2,1,2,1,0},
{0,0,1,2,1,2,1,2,0},
{0,0,0,0,2,1,2,1,0},
{0,0,0,0,0,2,1,0,0}};
for (int i = 0; i < tabJeu.length; i++) {
for (int j = 0; j < tabJeu[i].length; j++) {
int valeur = tabJeu[i][j];
if (valeur != 0) {
Color couleur = (valeur == 1) ? Color.BLUE : Color.RED;
JButton bouton = creerBoutonRond(couleur);
for (int i = 0; i < pionsParLigne.length; i++) {
int nbPions = pionsParLigne[i];
int x = 60 + decalageLigne[i];
int y = yBase + i * pas;
int x = xBase + j * pas;
int y = yBase + i * pas;
bouton.setBounds(x, y, pionTaille, pionTaille);
for (int j = 0; j < nbPions; j++) {
if ((i+j) % 2 == 0)
pinceau.setColor(Color.BLUE);
else
pinceau.setColor(Color.RED);
pinceau.fillOval(x + j * pas, y, pionTaille, pionTaille);
add(bouton);
}
}
}
}
pinceau.dispose();
private JButton creerBoutonRond(Color couleurBase) {
JButton bouton = new JButton() {
private boolean hover = false;
{
setBorderPainted(false);
setContentAreaFilled(false);
setFocusPainted(false);
setOpaque(false);
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
hover = true;
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
hover = false;
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,
"Tu as cliqué sur un pion " + (couleurBase == Color.BLUE ? "bleu" : "rouge") + " !");
}
});
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color couleur = hover ? couleurBase.darker() : couleurBase;
g2.setColor(couleur);
g2.fillOval(0, 0, getWidth(), getHeight());
g2.dispose();
}
@Override
public boolean contains(int x, int y) {
double dx = x - getWidth() / 2.0;
double dy = y - getHeight() / 2.0;
return dx * dx + dy * dy <= (getWidth() / 2.0) * (getWidth() / 2.0);
}
};
bouton.setPreferredSize(new Dimension(pionTaille, pionTaille));
return bouton;
}
public static void main(String[] args) {
JFrame fenetre = new JFrame("Forme diagonale miroir");
JFrame fenetre = new JFrame("Pions interactifs sans bord bleu");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(900, 800);
fenetre.add(new Affiche());
fenetre.setVisible(true);
}
}
}