[Pathfinding] Add "place wall" action

This commit is contained in:
Thibaud Gasser 2018-09-30 15:25:52 +02:00
parent c69a9ab56f
commit 937e72dfcc
3 changed files with 32 additions and 16 deletions

View File

@ -7,27 +7,18 @@ public class Cell {
private final Point pos;
private final JLabel content;
private boolean selected;
private boolean wall;
public Cell(Point pos, JLabel content) {
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) {
void setWall(boolean wall) {
this.wall = wall;
}

View File

@ -56,10 +56,10 @@ public class Grid implements Iterable<Cell> {
content.revalidate();
}
public void setWall(Point loc) {
public void setWall(Point loc, boolean wall) {
final Cell c = cells.get(loc.x + size.width * loc.y);
c.setWall(true);
c.getContent().setBackground(Color.BLACK);
c.setWall(wall);
c.getContent().setBackground(wall ? Color.BLACK : null);
c.getContent().revalidate();
}
@ -67,7 +67,6 @@ public class Grid implements Iterable<Cell> {
for (Cell cell : cells) {
cell.getContent().setBackground(null);
cell.setWall(false);
cell.setSelected(false);
}
}
@ -75,4 +74,5 @@ public class Grid implements Iterable<Cell> {
public Iterator<Cell> iterator() {
return cells.iterator();
}
}

View File

@ -24,6 +24,7 @@ public class App extends JFrame {
private Node startNode;
private Node endNode;
private JPanel gridPane;
private boolean wallPlacing = false;
private App() throws HeadlessException {
super("Pathfinding");
@ -34,6 +35,7 @@ public class App extends JFrame {
}
private void setupInterface() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
gridPane = new JPanel();
gridPane.setLayout(new GridLayout(ROWS, COLS));
for (Cell c : grid) {
@ -45,7 +47,9 @@ public class App extends JFrame {
public void mousePressed(MouseEvent e) {
final JLabel at = (JLabel) gridPane.getComponentAt(e.getPoint());
final Point loc = grid.get(at).getPos();
if (startNode == null) {
if (wallPlacing) {
doPlaceWall(loc);
} else if (startNode == null) {
grid.setColor(loc, Color.RED);
startNode = new Node(loc);
} else if (endNode == null) {
@ -56,6 +60,9 @@ public class App extends JFrame {
});
final JMenuBar menuBar = new JMenuBar();
final JMenuItem placeWallItem = new JMenuItem("Place wall");
menuBar.add(placeWallItem);
placeWallItem.addActionListener(actionEvent -> setWallPlacing(placeWallItem));
final JMenuItem runItem = new JMenuItem("Run");
menuBar.add(runItem);
runItem.addActionListener(actionEvent -> doSearch());
@ -70,6 +77,24 @@ public class App extends JFrame {
getContentPane().add(gridPane);
}
private void setWallPlacing(JMenuItem placeWallItem) {
// Invert selection state
placeWallItem.setSelected(!placeWallItem.isSelected());
final boolean selected = placeWallItem.isSelected();
if (selected) {
placeWallItem.setBackground(Color.BLUE);
} else {
placeWallItem.setBackground(null);
}
this.wallPlacing = selected;
}
private void doPlaceWall(Point loc) {
final boolean wall = grid.get(loc).isWall();
// Toggle "wall" state
grid.setWall(loc, !wall);
}
private void doReset() {
pathfinder = null;
startNode = null;