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; import java.net.URL;
public class Grid extends JPanel { public class Grid extends JPanel {
private int rows; private int width;
private int cols; private int height;
private Color[][] colors; private JButton[][] mat;
private MouseAdapter mouseListener; private int selected;
public Grid(int rows, int cols) { public Grid(int width, int height) {
this.rows = rows; super(new GridLayout(height, width));
this.cols = cols; this.width = width;
this.setPreferredSize(new Dimension(400, 400)); this.height = height;
this.setBackground(Color.WHITE); this.selected = 0;
colors = new Color[rows][cols]; this.mat = new JButton[height][width];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) { for (int i = 0; i < this.height; i++) {
colors[i][j] = Color.WHITE; 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() { public void setSelected(int selected) {
@Override this.selected = selected;
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);
} }
} }

View file

@ -88,7 +88,6 @@ public class Window extends JFrame {
menuBar.add(aideMenu); menuBar.add(aideMenu);
menuBar.add(toolsMenu); menuBar.add(toolsMenu);
regles.addActionListener(e -> { regles.addActionListener(e -> {
JOptionPane.showMessageDialog(this, "Définissez la taille du plateau ainsi que l'orientation du laser, enfin ajoutez des obstacles et laissez le programme trouver le bon chemin !"); JOptionPane.showMessageDialog(this, "Définissez la taille du plateau ainsi que l'orientation du laser, enfin ajoutez des obstacles et laissez le programme trouver le bon chemin !");
}); });
@ -97,8 +96,19 @@ public class Window extends JFrame {
int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width")); int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width"));
int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height")); int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height"));
this.grid.changeDim(width, height); this.panel.remove(this.grid);
this.grid.repaint(); this.grid = new Grid(width, height);
this.panel.add(this.grid);
super.pack();
super.repaint();
});
radioWall.addActionListener(e -> {
this.grid.setSelected(1);
});
radioStart.addActionListener(e -> {
this.grid.setSelected(0);
}); });
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2); this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2);