[Pathfinding] New model with Grid and Cell classes

This commit is contained in:
Thibaud Gasser 2018-09-30 15:54:29 +02:00
parent a347d04bd9
commit 815c381d1a
5 changed files with 124 additions and 87 deletions

View File

@ -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<Node>, List<Node>> {
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);
});
}
}

View File

@ -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;
}
}

View File

@ -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<Cell> {
private final static Border lineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private final List<Cell> 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<Cell> iterator() {
return cells.iterator();
}
}

View File

@ -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);
}
}

View File

@ -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<JLabel> {
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<JLabel> iterator() {
return new Iterator<>() {
private int pos = 0;
@Override
public boolean hasNext() {
return cells.length > pos;
}
@Override
public JLabel next() {
return cells[pos++];
}
};
}
}