2 Commits

Author SHA1 Message Date
b115b88b5a Amélioration de l'affichage, ajout de bouton 2025-11-10 17:18:19 +01:00
98ae6dc754 Affichage 2025-10-16 11:59:41 +02:00
4 changed files with 121 additions and 0 deletions

BIN
Affichage/Affiche.class Normal file

Binary file not shown.

106
Affichage/Affiche.java Normal file
View File

@@ -0,0 +1,106 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Affiche extends JPanel {
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}
};
int pionTaille = 50;
int pas = 70;
int yBase = 60;
int xBase = 60;
public Affiche() {
setLayout(null);
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);
int x = xBase + j * pas;
int y = yBase + i * pas;
bouton.setBounds(x, y, pionTaille, pionTaille);
add(bouton);
}
}
}
}
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("Pions interactifs sans bord bleu");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(900, 800);
fenetre.add(new Affiche());
fenetre.setVisible(true);
}
}

BIN
Affichage/Test.class Normal file

Binary file not shown.

15
Affichage/Test.java Normal file
View File

@@ -0,0 +1,15 @@
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame fenetre = new JFrame();
fenetre.setSize(1500, 1050);
fenetre.setLocation(150, 30);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Affiche aff = new Affiche();
fenetre.add(aff);
fenetre.setVisible(true);
}
}