[Pathfinding] Replace findLoc with get(JLabel)
This commit is contained in:
parent
815c381d1a
commit
c69a9ab56f
@ -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) {
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user