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