diff --git a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/algorithm/AStarWorker.java b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/algorithm/AStarWorker.java index 0ff6b6b..5470823 100644 --- a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/algorithm/AStarWorker.java +++ b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/algorithm/AStarWorker.java @@ -1,6 +1,6 @@ package fr.uha.gasser.pathfinding.algorithm; -import fr.uha.gasser.pathfinding.ui.Grid; +import fr.uha.gasser.pathfinding.model.Grid; import javax.swing.*; import java.awt.*; @@ -29,7 +29,7 @@ public class AStarWorker extends SwingWorker, List> { protected void done() { if (this.isCancelled()) return; path.forEach(node -> { - if (! node.loc.equals(goal.loc)) target.setTile(node.loc, Color.BLUE); + if (! node.loc.equals(goal.loc)) target.setColor(node.loc, Color.BLUE); }); } } diff --git a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/model/Cell.java b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/model/Cell.java new file mode 100644 index 0000000..33c9d89 --- /dev/null +++ b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/model/Cell.java @@ -0,0 +1,41 @@ +package fr.uha.gasser.pathfinding.model; + +import javax.swing.*; +import java.awt.*; + +public class Cell { + + private final Point pos; + private final JLabel content; + private boolean selected; + private boolean wall; + + public Cell(Point pos, JLabel content) { + this.pos = pos; + this.content = content; + } + + public boolean isSelected() { + return selected; + } + + public void setSelected(boolean selected) { + this.selected = selected; + } + + public boolean isWall() { + return wall; + } + + public void setWall(boolean wall) { + this.wall = wall; + } + + public Point getPos() { + return pos; + } + + public JLabel getContent() { + return content; + } +} diff --git a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/model/Grid.java b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/model/Grid.java new file mode 100644 index 0000000..1e57abd --- /dev/null +++ b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/model/Grid.java @@ -0,0 +1,75 @@ +package fr.uha.gasser.pathfinding.model; + +import javax.swing.*; +import javax.swing.border.Border; +import java.awt.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class Grid implements Iterable { + + private final static Border lineBorder = BorderFactory.createLineBorder(Color.BLACK, 1); + + private final List cells; + private final Dimension size; + private final int cellSize; + + public Grid(int rows, int cols, int cellSize) { + this.size = new Dimension(rows, cols); + this.cellSize = cellSize; + this.cells = new ArrayList<>(rows*cols); + initGrid(rows*cols); + } + + private void initGrid(int size) { + for (int i = 0; i < size; i++) { + final JLabel label = new JLabel(); + // Force cell size + label.setPreferredSize(new Dimension(cellSize, cellSize)); + label.setMinimumSize(label.getPreferredSize()); + label.setMaximumSize(label.getPreferredSize()); + label.setBorder(lineBorder); + label.setOpaque(true); + final Point pos = new Point(i % this.size.width, i / this.size.height); + cells.add(new Cell(pos, label)); + } + } + + public Point findLoc(JLabel content) { + int index = 0; + for (Cell c : cells) { + if (c.getContent() == content) return new Point(index % size.width, index / size.height); + index++; + } + // Not found + return new Point(-1, -1); + } + + public void setColor(Point loc, Color color) { + final Cell c = cells.get(loc.x + size.width * loc.y); + final JLabel content = c.getContent(); + content.setBackground(color); + content.revalidate(); + } + + public void setWall(Point loc) { + final Cell c = cells.get(loc.x + size.width * loc.y); + c.setWall(true); + c.getContent().setBackground(Color.BLACK); + c.getContent().revalidate(); + } + + public void clear() { + for (Cell cell : cells) { + cell.getContent().setBackground(null); + cell.setWall(false); + cell.setSelected(false); + } + } + + @Override + public Iterator iterator() { + return cells.iterator(); + } +} diff --git a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/ui/App.java b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/ui/App.java index 642cdb3..16a454e 100644 --- a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/ui/App.java +++ b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/ui/App.java @@ -2,6 +2,8 @@ package fr.uha.gasser.pathfinding.ui; import fr.uha.gasser.pathfinding.algorithm.AStarWorker; import fr.uha.gasser.pathfinding.algorithm.Node; +import fr.uha.gasser.pathfinding.model.Cell; +import fr.uha.gasser.pathfinding.model.Grid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,8 +36,8 @@ public class App extends JFrame { private void setupInterface() { gridPane = new JPanel(); gridPane.setLayout(new GridLayout(ROWS, COLS)); - for (JLabel label : grid) { - gridPane.add(label); + for (Cell c : grid) { + gridPane.add(c.getContent()); } gridPane.addMouseListener(new MouseAdapter() { @@ -44,10 +46,10 @@ public class App extends JFrame { final JLabel at = (JLabel) gridPane.getComponentAt(e.getPoint()); final Point loc = grid.findLoc(at); if (startNode == null) { - grid.setTile(loc, Color.RED); + grid.setColor(loc, Color.RED); startNode = new Node(loc); } else if (endNode == null) { - grid.setTile(loc, Color.GREEN); + grid.setColor(loc, Color.GREEN); endNode = new Node(loc); } } diff --git a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/ui/Grid.java b/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/ui/Grid.java deleted file mode 100644 index f7b6ce9..0000000 --- a/simple-pathfinding/src/main/java/fr/uha/gasser/pathfinding/ui/Grid.java +++ /dev/null @@ -1,81 +0,0 @@ -package fr.uha.gasser.pathfinding.ui; - -import javax.swing.*; -import javax.swing.border.Border; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.util.Iterator; - -public class Grid implements Iterable { - - private final static Border lineBorder = BorderFactory.createLineBorder(Color.BLACK, 1); - - private final JLabel[] cells; - private final Dimension size; - private int cellSize; - - Grid(int rows, int cols, int cellSize) { - this.cellSize = cellSize; - this.size = new Dimension(rows, cols); - int size = rows * cols; - this.cells = new JLabel[size]; - - initGrid(size); - } - - private void initGrid(int size) { - for (int i = 0; i < size; i++) { - final JLabel label = new JLabel(); - // Force cell size - label.setPreferredSize(new Dimension(cellSize, cellSize)); - label.setMinimumSize(label.getPreferredSize()); - label.setMaximumSize(label.getPreferredSize()); - label.setBorder(lineBorder); - label.setOpaque(true); - cells[i] = label; - } - } - - public Point findLoc(JLabel cell) { - for (int i = 0; i < cells.length; i++) { - JLabel label = cells[i]; - if (cell == label) return new Point(i % size.width, i / size.height); - } - // Not found - return new Point(-1, -1); - } - - public void setTile(Point loc, Color color) { - setTile(loc.x + size.width*loc.y, color); - } - - private void setTile(int index, Color color) { - final JLabel label = cells[index]; - label.setBackground(color); - label.revalidate(); - } - - void clear() { - for (JLabel cell : cells) { - cell.setBackground(null); - } - } - - @Override - public Iterator iterator() { - return new Iterator<>() { - private int pos = 0; - - @Override - public boolean hasNext() { - return cells.length > pos; - } - - @Override - public JLabel next() { - return cells[pos++]; - } - }; - } - -} \ No newline at end of file