This commit is contained in:
Ninja-Jambon 2024-03-27 13:42:19 +01:00
parent 29ce62150e
commit 491cd35136
2 changed files with 40 additions and 136 deletions

View file

@ -23,144 +23,38 @@ import javax.imageio.ImageIO;
import java.net.URL;
public class Grid extends JPanel {
private int rows;
private int cols;
private Color[][] colors;
private MouseAdapter mouseListener;
private int width;
private int height;
private JButton[][] mat;
private int selected;
public Grid(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.setPreferredSize(new Dimension(400, 400));
this.setBackground(Color.WHITE);
public Grid(int width, int height) {
super(new GridLayout(height, width));
this.width = width;
this.height = height;
this.selected = 0;
colors = new Color[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
colors[i][j] = Color.WHITE;
this.mat = new JButton[height][width];
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
this.mat[i][j] = new JButton();
this.mat[i][j].setBackground(Color.WHITE);
this.mat[i][j].setPreferredSize(new Dimension(Math.max(50, 600 / this.height), Math.max(50, (this.width * 600 / this.height) / this.width)));
final int coord_i = i;
final int coord_j = j;
this.mat[i][j].addActionListener(e -> {
System.out.println(coord_i + " " + coord_j);
});
super.add(this.mat[i][j]);
}
}
mouseListener = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
int cellWidth = getWidth() / cols;
int cellHeight = getHeight() / rows;
int col = evt.getX() / cellWidth;
int row = evt.getY() / cellHeight;
if (row >= 0 && row < rows && col >= 0 && col < cols) {
if (colors[row][col] == Color.WHITE) {
colors[row][col] = Color.GRAY;
} else {
colors[row][col] = Color.WHITE;
}
repaint();
}
}
@Override
public void mouseEntered(MouseEvent e) {
try {
// Charger l'image pour le curseur personnalisé
URL url = getClass().getResource("../images/blade.png");
BufferedImage cursorImage = ImageIO.read(new File(url.getPath()));
// Créer le curseur personnalisé
Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "customCursor");
// Définir le curseur personnalisé
setCursor(customCursor);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void mouseExited(MouseEvent e) {
setCursor(Cursor.getDefaultCursor()); // Rétablir l'icône de la souris par défaut quand elle quitte le composant
}
};
this.addMouseListener(this.mouseListener);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int cellWidth = this.getWidth() / this.cols;
int cellHeight = this.getHeight() / this.rows;
// Dessiner les cases avec les couleurs stockées
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
g.setColor(colors[i][j]);
g.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);
}
}
// Dessiner les lignes de la grille
g.setColor(Color.BLACK);
for (int i = 0; i <= rows; i++) {
g.drawLine(0, i * cellHeight, getWidth(), i * cellHeight);
}
for (int j = 0; j <= cols; j++) {
g.drawLine(j * cellWidth, 0, j * cellWidth, getHeight());
}
}
public void changeDim(int width, int height) {
this.rows = height;
this.cols = width;
this.setPreferredSize(new Dimension(700, 400));
this.setBackground(Color.WHITE);
this.colors = new Color[height][width];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
colors[i][j] = Color.WHITE;
}
}
this.removeMouseListener(this.mouseListener);
this.mouseListener = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
int cellWidth = getWidth() / cols;
int cellHeight = getHeight() / rows;
int col = evt.getX() / cellWidth;
int row = evt.getY() / cellHeight;
if (row >= 0 && row < rows && col >= 0 && col < cols) {
if (colors[row][col] == Color.WHITE) {
colors[row][col] = Color.GRAY;
} else {
colors[row][col] = Color.WHITE;
}
repaint();
}
}
@Override
public void mouseEntered(MouseEvent e) {
try {
// Charger l'image pour le curseur personnalisé
URL url = getClass().getResource("../images/blade.png");
BufferedImage cursorImage = ImageIO.read(new File(url.getPath()));
// Créer le curseur personnalisé
Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "customCursor");
// Définir le curseur personnalisé
setCursor(customCursor);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void mouseExited(MouseEvent e) {
setCursor(Cursor.getDefaultCursor()); // Rétablir l'icône de la souris par défaut quand elle quitte le composant
}
};
this.addMouseListener(this.mouseListener);
public void setSelected(int selected) {
this.selected = selected;
}
}