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 javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*;
class Affiche extends JComponent { public class Affiche extends JPanel {
@Override int tabJeu[][] = {
protected void paintComponent(Graphics g) { {0,0,1,2,0,0,0,0,0},
super.paintComponent(g); {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()) { int pionTaille = 50;
g.setColor(getBackground()); int pas = 70;
g.fillRect(0, 0, getWidth(), getHeight()); int yBase = 60;
} int xBase = 60;
Graphics2D pinceau = (Graphics2D) g.create(); public Affiche() {
setLayout(null);
int pionTaille = 50; for (int i = 0; i < tabJeu.length; i++) {
int pas = 70; for (int j = 0; j < tabJeu[i].length; j++) {
int yBase = 60; int valeur = tabJeu[i][j];
int tabJeu[][] = {{0,0,1,2,0,0,0,0,0}, if (valeur != 0) {
{0,1,2,1,2,0,0,0,0}, Color couleur = (valeur == 1) ? Color.BLUE : Color.RED;
{0,2,1,2,1,2,1,0,0}, JButton bouton = creerBoutonRond(couleur);
{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 < pionsParLigne.length; i++) { int x = xBase + j * pas;
int nbPions = pionsParLigne[i]; int y = yBase + i * pas;
int x = 60 + decalageLigne[i]; bouton.setBounds(x, y, pionTaille, pionTaille);
int y = yBase + i * pas;
for (int j = 0; j < nbPions; j++) { add(bouton);
if ((i+j) % 2 == 0) }
pinceau.setColor(Color.BLUE);
else
pinceau.setColor(Color.RED);
pinceau.fillOval(x + j * pas, y, pionTaille, pionTaille);
} }
} }
}
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) { 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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(900, 800); fenetre.setSize(900, 800);
fenetre.add(new Affiche()); fenetre.add(new Affiche());
fenetre.setVisible(true); fenetre.setVisible(true);
} }
} }