From b05b7cd13c77b07e270d5fc409a50014c5729322 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Tue, 26 Mar 2024 21:59:12 +0100 Subject: [PATCH] hell + gui --- AppLaser.java | 6 +- makefile | 4 + saves/prof.txt | 4 +- Situation.java => universe/Situation.java | 2 + Stack.java => universe/Stack.java | 2 + Universe.java => universe/Universe.java | 2 + userInterface/Grid.java | 103 ++++++++++++++++++++++ userInterface/UserInterface.java | 12 +++ userInterface/Window.java | 80 +++++++++++++++++ 9 files changed, 212 insertions(+), 3 deletions(-) rename Situation.java => universe/Situation.java (95%) rename Stack.java => universe/Stack.java (96%) rename Universe.java => universe/Universe.java (99%) create mode 100644 userInterface/Grid.java create mode 100644 userInterface/UserInterface.java create mode 100644 userInterface/Window.java diff --git a/AppLaser.java b/AppLaser.java index ccd6258..7209609 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -9,6 +9,9 @@ import java.util.Set; import java.util.stream.Stream; import java.util.stream.Collectors; +import userInterface.UserInterface; +import universe.*; + public class AppLaser { public static void main(String [] args) { @@ -312,7 +315,8 @@ public class AppLaser { } while (choice != 0); } else { - System.out.println("there is no GUI yet."); + UserInterface userInterface= new UserInterface(); + userInterface.start(); } } } diff --git a/makefile b/makefile index b2096fa..5d2950c 100644 --- a/makefile +++ b/makefile @@ -5,3 +5,7 @@ build: run: javac --source 1.8 --target 1.8 AppLaser.java java AppLaser -cli --optimize-duration + +run_gui: + javac --source 1.8 --target 1.8 AppLaser.java + java AppLaser diff --git a/saves/prof.txt b/saves/prof.txt index 1278254..4022bef 100644 --- a/saves/prof.txt +++ b/saves/prof.txt @@ -1,7 +1,7 @@ 13 20 -11 -1 +10 +9 1 2 6 diff --git a/Situation.java b/universe/Situation.java similarity index 95% rename from Situation.java rename to universe/Situation.java index d456677..fb3fd9c 100644 --- a/Situation.java +++ b/universe/Situation.java @@ -1,5 +1,7 @@ // Antoine CRETUAL, Lukian LEIZOUR, 21/02/2024 +package universe; + public class Situation { // Atributes diff --git a/Stack.java b/universe/Stack.java similarity index 96% rename from Stack.java rename to universe/Stack.java index 9a6e064..464faec 100644 --- a/Stack.java +++ b/universe/Stack.java @@ -1,5 +1,7 @@ // Antoine CRETUAL, Lukian LEIZOUR, 21/02/2024 +package universe; + import java.util.Vector; public class Stack { diff --git a/Universe.java b/universe/Universe.java similarity index 99% rename from Universe.java rename to universe/Universe.java index 3a86c87..40fba4f 100644 --- a/Universe.java +++ b/universe/Universe.java @@ -1,5 +1,7 @@ // Antoine CRETUAL, Lukian LEIZOUR, 14/02/2024 +package universe; + import java.io.File; import java.io.FileWriter; diff --git a/userInterface/Grid.java b/userInterface/Grid.java new file mode 100644 index 0000000..db573ee --- /dev/null +++ b/userInterface/Grid.java @@ -0,0 +1,103 @@ +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; +import java.awt.GridLayout; +import java.awt.Insets; + +import javax.swing.*; +import java.awt.event.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import java.awt.image.BufferedImage; +import java.io.File; +import javax.imageio.ImageIO; + +public class Grid extends JPanel { + private int rows; + private int cols; + private Color[][] colors; + + public Grid(int rows, int cols) { + this.rows = rows; + this.cols = cols; + setPreferredSize(new Dimension(400, 400)); + setBackground(Color.WHITE); + + colors = new Color[rows][cols]; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + colors[i][j] = Color.WHITE; + } + } + + addMouseListener(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.RED; + } else { + colors[row][col] = Color.WHITE; + } + repaint(); + } + } + + @Override + 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")); + // 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 + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + int cellWidth = getWidth() / cols; + int cellHeight = getHeight() / rows; + + // Dessiner les cases avec les couleurs stockées + for (int i = 0; i < rows; i++) { + for (int j = 0; j < 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()); + } + } +} \ No newline at end of file diff --git a/userInterface/UserInterface.java b/userInterface/UserInterface.java new file mode 100644 index 0000000..b1c4614 --- /dev/null +++ b/userInterface/UserInterface.java @@ -0,0 +1,12 @@ +package userInterface; + +public class UserInterface extends Thread { + private Window window; + + public UserInterface() { + window = new Window(); + } + + public void run() { + } +} \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java new file mode 100644 index 0000000..c1649c9 --- /dev/null +++ b/userInterface/Window.java @@ -0,0 +1,80 @@ +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; +import java.awt.GridLayout; +import java.awt.Insets; + +import javax.swing.*; +import java.awt.event.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import java.awt.image.BufferedImage; +import java.io.File; +import javax.imageio.ImageIO; + +public class Window extends JFrame { + + JPanel panel; + JMenuBar menuBar; + Grid grid; + + public Window() { + super("Laser Finder"); + + 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"); + + 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"); + + 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"); + + fichierMenu.add(nouveauItem); + fichierMenu.add(ouvrirItem); + fichierMenu.add(enregistrerItem); + + aideMenu.add(apropos); + aideMenu.add(regles); + + menuBar.add(fichierMenu); + menuBar.add(aideMenu); + + + 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 !"); + }); + + grid = new Grid(5, 5); + panel.add(grid, BorderLayout.CENTER); + + super.setJMenuBar(menuBar); + super.setContentPane(this.panel); + + super.setLocationRelativeTo(null); + super.setLocation(1000, 400); + super.pack(); + super.setVisible(true); + super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +} \ No newline at end of file