From 2f51df0d82162dd2e7a5238eafcaa8daef3b507f Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 15:31:02 +0100 Subject: [PATCH] commit --- userInterface/Grid.java | 119 +++++++++++++++++++++++--------------- userInterface/Window.java | 66 ++++++++++++++++++--- 2 files changed, 129 insertions(+), 56 deletions(-) diff --git a/userInterface/Grid.java b/userInterface/Grid.java index ae42f90..5360e52 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -26,6 +26,8 @@ import universe.Universe; import universe.Stack; import universe.Situation; +import java.time.Instant; + public class Grid extends JPanel { private int width; private int height; @@ -33,8 +35,10 @@ public class Grid extends JPanel { private int selected; private Universe universe; private int button_width, button_height; + private int refreshRate; + private boolean solving; - public Grid(int width, int height, Universe universe) { + public Grid(int width, int height, Universe universe, int refreshRate) { super(new GridLayout(height, width)); this.width = width; this.height = height; @@ -42,6 +46,8 @@ public class Grid extends JPanel { this.universe = universe; this.button_width = 600 / this.height; this.button_height = button_width; + this.refreshRate = refreshRate; + this.solving = false; this.mat = new JButton[height][width]; @@ -55,6 +61,10 @@ public class Grid extends JPanel { final int coord_j = j; this.mat[i][j].addActionListener(e -> { + if (solving) { + return; + } + switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) { case 0: if (this.selected == 1) { @@ -93,101 +103,113 @@ public class Grid extends JPanel { break; } - this.printUniverse(); + this.printUniverseGrid(this.universe.getGrid()); }); super.add(this.mat[i][j]); } } - this.printUniverse(); + this.printUniverseGrid(this.universe.getGrid()); } public void setSelected(int selected) { this.selected = selected; } - public void setButtonsEnabled(boolean enabled) { - for (int i = 0; i < this.height; i++) { - for (int j = 0; j < this.width; j ++) { - this.mat[i][j].setEnabled(enabled); - } - } + public void setRefreshRate(int refreshRate) { + this.refreshRate = refreshRate; } - public void printUniverse() { + public void setSolving(boolean solving) { + this.solving = solving; + } + + public void printUniverseGrid(int [][] universeGrid) { for (int i = 0; i < this.height; i++) { for (int j = 0; j < this.width; j ++) { Image photo; - switch (this.universe.getGrid()[i + 1][j + 1]) { + switch (universeGrid[i + 1][j + 1]) { case -1: - photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/wall.png"); break; case 0: - this.mat[i][j].setBackground(Color.WHITE); - this.mat[i][j].setIcon(null); + this.changeButtonState(i, j, null); break; case 1: - photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/verticalLaser.png"); break; case 2: - photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/horizontalLaser.png"); break; case 3: - photo = new ImageIcon(this.getClass().getResource("../images/crossLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/crossLaser.png"); break; case 4: - photo = new ImageIcon(this.getClass().getResource("../images/miror1.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/miror1.png"); break; case 5: - photo = new ImageIcon(this.getClass().getResource("../images/miror2.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/miror2.png"); break; case 6: - photo = new ImageIcon(this.getClass().getResource("../images/miror3.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/miror3.png"); break; case 7: - photo = new ImageIcon(this.getClass().getResource("../images/miror4.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/miror4.png"); break; case 10: - photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/startUp.png"); break; case 11: - photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/startBot.png"); break; case 12: - photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/startRight.png"); break; case 13: - photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); - this.mat[i][j].setIcon(new ImageIcon(photo)); + this.changeButtonState(i, j, "../images/startLeft.png"); break; } } } } + public void changeButtonState(int coord_i, int coord_j, String url) { + if (url == null) { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + mat[coord_i][coord_j].setBackground(Color.WHITE); + mat[coord_i][coord_j].setIcon(null); + } + }); + + t.start(); + } else { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + Image image = new ImageIcon(getClass().getResource(url)).getImage().getScaledInstance(button_width, button_height, Image.SCALE_SMOOTH); + mat[coord_i][coord_j].setIcon(new ImageIcon(image)); + } + }); + + t.start(); + } + } + public void reset() { this.universe.resetUniverse(); - this.printUniverse(); + this.printUniverseGrid(this.universe.getGrid()); } public void solve() { - this.setButtonsEnabled(false); this.universe.resetUniverse(); final Universe universe = this.universe; - Thread t1 = new Thread(new Runnable() { + this.solving = true; + + Thread computeThread = new Thread(new Runnable() { @Override public void run() { int [] startCoords = universe.getStartCoords(); @@ -208,6 +230,8 @@ public class Grid extends JPanel { int best_filled_boxes = 0; int best_nb_mirrors = 0; + long lastPrinted = Instant.now().toEpochMilli(); + do { if (universe.canEvolve(currentState)) { stack.push(currentState.copy(universe.possibleChoices(currentState))); @@ -218,9 +242,9 @@ public class Grid extends JPanel { best_filled_boxes = universe.getFilledBoxes(); best_nb_mirrors = universe.getNbMirrors(); - System.out.println(best_filled_boxes + " " + best_nb_mirrors); - - printUniverse(); + if (Instant.now().toEpochMilli() - lastPrinted > refreshRate) { + printUniverseGrid(bestGrid); + } } } else if (stack.size() > 0) { @@ -229,12 +253,13 @@ public class Grid extends JPanel { } else { break; } - } while (stack.size() != 0); + } while (stack.size() != 0 && solving == true); + solving = false; + Universe.print(bestGrid, width + 2, height + 2, 4, 4); + printUniverseGrid(bestGrid); } }); - t1.start(); - - this.setButtonsEnabled(true); + computeThread.start(); } } \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java index a84d6ec..5eec67e 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -48,6 +48,9 @@ public class Window extends JFrame { JMenu fichierMenu = new JMenu("File"); JMenu aideMenu = new JMenu("Help"); JMenu toolsMenu = new JMenu("Tools"); + JMenu solveMenu = new JMenu("Solve"); + JMenu displayMenu = new JMenu("Display"); + JMenu refreshRate = new JMenu("Refresh Rate"); URL newUrl = getClass().getResource("../images/new.png"); URL openUrl = getClass().getResource("../images/open.png"); @@ -57,9 +60,9 @@ public class Window extends JFrame { 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 nouveauItem = new JMenuItem("New", nouveauIcon); + JMenuItem ouvrirItem = new JMenuItem("Open", openIcon); + JMenuItem enregistrerItem = new JMenuItem("Save", saveIcon); JMenuItem apropos = new JMenuItem("About"); JMenuItem regles = new JMenuItem("Rules"); @@ -75,8 +78,24 @@ public class Window extends JFrame { buttonGroup.add(radioStart); JMenuItem changeSize = new JMenuItem("Change Size"); - JMenuItem solve = new JMenuItem("Solve"); JMenuItem reset = new JMenuItem("Reset"); + + JMenuItem solve = new JMenuItem("Start"); + JMenuItem stop = new JMenuItem("Stop"); + + JRadioButtonMenuItem radio10ms = new JRadioButtonMenuItem("10ms"); + JRadioButtonMenuItem radio200ms = new JRadioButtonMenuItem("200ms"); + JRadioButtonMenuItem radio500ms = new JRadioButtonMenuItem("500ms"); + JRadioButtonMenuItem radio1000ms = new JRadioButtonMenuItem("1000ms"); + + radio10ms.setSelected(true); + + ButtonGroup refreshRates = new ButtonGroup(); + + refreshRates.add(radio10ms); + refreshRates.add(radio200ms); + refreshRates.add(radio500ms); + refreshRates.add(radio1000ms); fichierMenu.add(nouveauItem); fichierMenu.add(ouvrirItem); @@ -94,15 +113,28 @@ public class Window extends JFrame { toolsMenu.addSeparator(); toolsMenu.add(reset); + solveMenu.add(solve); + solveMenu.add(stop); + + refreshRate.add(radio10ms); + refreshRate.add(radio200ms); + refreshRate.add(radio500ms); + refreshRate.add(radio1000ms); + + displayMenu.add(refreshRate); + menuBar.add(fichierMenu); menuBar.add(aideMenu); menuBar.add(toolsMenu); + menuBar.add(solveMenu); + menuBar.add(displayMenu); nouveauItem.addActionListener(e -> { - this.universe.changeUniverseDim(5, 5); this.universe.changeUniverseStart(1, 1, 11); + this.universe.changeUniverseDim(5, 5); + this.universe.resetUniverseObstacles(); this.panel.remove(this.grid); - this.grid = new Grid(3, 3, this.universe); + this.grid = new Grid(3, 3, this.universe, 10); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -149,7 +181,7 @@ public class Window extends JFrame { } this.panel.remove(this.grid); - this.grid = new Grid(universe_width, universe_height, this.universe); + this.grid = new Grid(universe_width, universe_height, this.universe, 10); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -167,7 +199,7 @@ public class Window extends JFrame { this.universe.changeUniverseDim(width + 2, height + 2); this.panel.remove(this.grid); - this.grid = new Grid(width, height, this.universe); + this.grid = new Grid(width, height, this.universe, 10); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -185,11 +217,27 @@ public class Window extends JFrame { this.grid.solve(); }); + stop.addActionListener(e -> { + this.grid.setSolving(false); + }); + reset.addActionListener(e -> { this.grid.reset(); }); + + radio200ms.addActionListener(e -> { + this.grid.setRefreshRate(200); + }); + + radio500ms.addActionListener(e -> { + this.grid.setRefreshRate(500); + }); + + radio1000ms.addActionListener(e -> { + this.grid.setRefreshRate(1000); + }); - this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe); + this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe, 10); this.panel.add(grid, BorderLayout.CENTER); super.setJMenuBar(menuBar);