diff --git a/userInterface/Grid.java b/userInterface/Grid.java index 7e5d6d0..9fc19f3 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -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; } } \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java index 33d8f8f..2cad948 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -88,7 +88,6 @@ public class Window extends JFrame { menuBar.add(aideMenu); menuBar.add(toolsMenu); - 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 !"); }); @@ -97,8 +96,19 @@ public class Window extends JFrame { int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width")); int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height")); - this.grid.changeDim(width, height); - this.grid.repaint(); + this.panel.remove(this.grid); + 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);