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

View File

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