[Pathfinding] Replace findLoc with get(JLabel)

This commit is contained in:
Thibaud Gasser 2018-09-30 16:08:54 +02:00
parent 815c381d1a
commit c69a9ab56f
2 changed files with 9 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import java.util.List;
public class Grid implements Iterable<Cell> {
private final static Border lineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private static final Point INVALID = new Point(-256, -256);
private final List<Cell> cells;
private final Dimension size;
@ -36,14 +37,16 @@ public class Grid implements Iterable<Cell> {
}
}
public Point findLoc(JLabel content) {
int index = 0;
public Cell get(Point loc) {
return cells.get(loc.x + size.width * loc.y);
}
public Cell get(JLabel content) {
for (Cell c : cells) {
if (c.getContent() == content) return new Point(index % size.width, index / size.height);
index++;
if (c.getContent() == content) return c;
}
// Not found
return new Point(-1, -1);
return new Cell(INVALID, null);
}
public void setColor(Point loc, Color color) {

View File

@ -44,7 +44,7 @@ public class App extends JFrame {
@Override
public void mousePressed(MouseEvent e) {
final JLabel at = (JLabel) gridPane.getComponentAt(e.getPoint());
final Point loc = grid.findLoc(at);
final Point loc = grid.get(at).getPos();
if (startNode == null) {
grid.setColor(loc, Color.RED);
startNode = new Node(loc);