diff --git a/AppLaser.java b/AppLaser.java index 7209609..b37f5c2 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -10,7 +10,9 @@ import java.util.stream.Stream; import java.util.stream.Collectors; import userInterface.UserInterface; -import universe.*; +import universe.Universe; +import universe.Stack; +import universe.Situation; public class AppLaser { public static void main(String [] args) { @@ -46,12 +48,11 @@ public class AppLaser { else if (start_dir == 12) firstState_j = start_j + 1; else if (start_dir == 13) firstState_j = start_j - 1; + Stack stack = new Stack (); + Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir); + Situation currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0); + if (cli == true) { - - Stack stack = new Stack (); - Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir); - Situation currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0); - Scanner scanner = new Scanner(System.in); int choice; @@ -315,7 +316,7 @@ public class AppLaser { } while (choice != 0); } else { - UserInterface userInterface= new UserInterface(); + UserInterface userInterface = new UserInterface(universe); userInterface.start(); } } diff --git a/images/blade.png b/images/blade.png new file mode 100644 index 0000000..4ffe69a Binary files /dev/null and b/images/blade.png differ diff --git a/images/new.png b/images/new.png new file mode 100644 index 0000000..4d78a59 Binary files /dev/null and b/images/new.png differ diff --git a/images/open.png b/images/open.png new file mode 100644 index 0000000..153e75b Binary files /dev/null and b/images/open.png differ diff --git a/images/save.png b/images/save.png new file mode 100644 index 0000000..0b6a949 Binary files /dev/null and b/images/save.png differ diff --git a/makefile b/makefile index 5d2950c..61b95f6 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,17 @@ build: + javac --source 1.8 --target 1.8 universe/*.java + javac --source 1.8 --target 1.8 userInterface/*.java javac --source 1.8 --target 1.8 AppLaser.java jar cfe AppLaser.jar AppLaser *.class run: + javac --source 1.8 --target 1.8 universe/*.java + javac --source 1.8 --target 1.8 userInterface/*.java javac --source 1.8 --target 1.8 AppLaser.java java AppLaser -cli --optimize-duration run_gui: + javac --source 1.8 --target 1.8 universe/*.java + javac --source 1.8 --target 1.8 userInterface/*.java javac --source 1.8 --target 1.8 AppLaser.java java AppLaser diff --git a/universe/Universe.java b/universe/Universe.java index 40fba4f..a100ec2 100644 --- a/universe/Universe.java +++ b/universe/Universe.java @@ -381,4 +381,12 @@ public class Universe { public int[][] copyGrid() { return this.grid.clone(); } + + public int getWidth() { + return this.width; + } + + public int getHeight() { + return this.height; + } } diff --git a/userInterface/Grid.java b/userInterface/Grid.java index db573ee..7e5d6d0 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -20,16 +20,19 @@ import java.awt.image.BufferedImage; import java.io.File; 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; public Grid(int rows, int cols) { this.rows = rows; this.cols = cols; - setPreferredSize(new Dimension(400, 400)); - setBackground(Color.WHITE); + this.setPreferredSize(new Dimension(400, 400)); + this.setBackground(Color.WHITE); colors = new Color[rows][cols]; for (int i = 0; i < rows; i++) { @@ -38,7 +41,7 @@ public class Grid extends JPanel { } } - addMouseListener(new MouseAdapter() { + mouseListener = new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { int cellWidth = getWidth() / cols; @@ -48,7 +51,7 @@ public class Grid extends JPanel { if (row >= 0 && row < rows && col >= 0 && col < cols) { if (colors[row][col] == Color.WHITE) { - colors[row][col] = Color.RED; + colors[row][col] = Color.GRAY; } else { colors[row][col] = Color.WHITE; } @@ -60,7 +63,8 @@ public class Grid extends JPanel { public void mouseEntered(MouseEvent e) { try { // Charger l'image pour le curseur personnalisé - BufferedImage cursorImage = ImageIO.read(new File("h:\\Mes documents\\INF1404\\projet\\picture\\sabre.png")); + 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é @@ -74,18 +78,20 @@ public class Grid extends JPanel { 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 = getWidth() / cols; - int cellHeight = getHeight() / rows; + 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 < rows; i++) { - for (int j = 0; j < cols; j++) { + 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); } @@ -100,4 +106,61 @@ public class Grid extends JPanel { 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); + } } \ No newline at end of file diff --git a/userInterface/UserInterface.java b/userInterface/UserInterface.java index b1c4614..c91cd08 100644 --- a/userInterface/UserInterface.java +++ b/userInterface/UserInterface.java @@ -1,10 +1,12 @@ package userInterface; +import universe.*; + public class UserInterface extends Thread { private Window window; - public UserInterface() { - window = new Window(); + public UserInterface(Universe universe) { + window = new Window(universe); } public void run() { diff --git a/userInterface/Window.java b/userInterface/Window.java index c1649c9..33d8f8f 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -1,9 +1,5 @@ package userInterface; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JPanel; - import java.awt.Dimension; import java.awt.Color; import java.awt.GridBagConstraints; @@ -20,35 +16,59 @@ import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; +import java.net.URL; + +import universe.*; + public class Window extends JFrame { - JPanel panel; - JMenuBar menuBar; - Grid grid; + private JPanel panel; + private JMenuBar menuBar; + private Grid grid; + private Universe universe; - public Window() { + public Window(Universe universe) { super("Laser Finder"); + this.universe = universe; + panel = new JPanel(); panel.setPreferredSize(new Dimension(1000,800)); panel.setBackground(Color.decode("#EBEBD3")); - menuBar = new JMenuBar(); - JMenu fichierMenu = new JMenu("Fichier"); - JMenu aideMenu = new JMenu("Aide"); + JMenu fichierMenu = new JMenu("File"); + JMenu aideMenu = new JMenu("Help"); + JMenu toolsMenu = new JMenu("Tools"); + + URL newUrl = getClass().getResource("../images/new.png"); + URL openUrl = getClass().getResource("../images/open.png"); + URL saveUrl = getClass().getResource("../images/save.png"); - ImageIcon nouveauIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\nouveau.png"); - ImageIcon openIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\ouvrir.png"); - ImageIcon saveIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\sauvegarder.png"); + ImageIcon nouveauIcon = new ImageIcon(newUrl.getPath()); + ImageIcon openIcon = new ImageIcon(openUrl.getPath()); + ImageIcon saveIcon = new ImageIcon(saveUrl.getPath()); JMenuItem nouveauItem = new JMenuItem("Nouveau", nouveauIcon); JMenuItem ouvrirItem = new JMenuItem("Ouvrir", openIcon); JMenuItem enregistrerItem = new JMenuItem("Enregistrer", saveIcon); - JMenuItem apropos = new JMenuItem("à propos"); - JMenuItem regles = new JMenuItem("règles"); + JMenuItem apropos = new JMenuItem("About"); + JMenuItem regles = new JMenuItem("Rules"); + + JRadioButtonMenuItem radioWall = new JRadioButtonMenuItem("Wall"); + JRadioButtonMenuItem radioStart = new JRadioButtonMenuItem("Start"); + + radioWall.setSelected(true); + + ButtonGroup buttonGroup = new ButtonGroup(); + + buttonGroup.add(radioWall); + buttonGroup.add(radioStart); + + JMenuItem changeSize = new JMenuItem("Change Size"); + JMenuItem solve = new JMenuItem("Solve"); fichierMenu.add(nouveauItem); fichierMenu.add(ouvrirItem); @@ -57,16 +77,32 @@ public class Window extends JFrame { aideMenu.add(apropos); aideMenu.add(regles); + toolsMenu.add(radioWall); + toolsMenu.add(radioStart); + toolsMenu.addSeparator(); + toolsMenu.add(changeSize); + toolsMenu.addSeparator(); + toolsMenu.add(solve); + menuBar.add(fichierMenu); 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 !"); }); + + changeSize.addActionListener(e -> { + 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(); + }); - grid = new Grid(5, 5); - panel.add(grid, BorderLayout.CENTER); + this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2); + this.panel.add(grid, BorderLayout.CENTER); super.setJMenuBar(menuBar); super.setContentPane(this.panel);