-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
187 lines (166 loc) · 6.87 KB
/
Copy pathGrid.java
File metadata and controls
187 lines (166 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Grid extends JPanel implements MouseListener, MouseMotionListener {
static final int ROWS = 20;
static final int COLS = 20;
static final Color BG = new Color(10, 20, 40);
static final Color EMPTY = new Color(20, 40, 80);
static final Color GRID_LINE = new Color(30, 55, 110);
static final Color WALL_TOP = new Color(180, 190, 210);
static final Color WALL_FACE = new Color(90, 100, 130);
static final Color WALL_SIDE = new Color(40, 50, 75);
static final Color START_COL = new Color(50, 200, 100);
static final Color END_COL = new Color(220, 60, 60);
static final Color PATH_COL = new Color(255, 200, 50);
static final Color VISITED_COL = new Color(30, 80, 140);
int[][] cells = new int[ROWS][COLS];
boolean[][] path = new boolean[ROWS][COLS];
boolean[][] visited = new boolean[ROWS][COLS];
String mode = "wall";
int startR = -1, startC = -1, endR = -1, endC = -1;
public Grid() {
setBackground(BG);
addMouseListener(this);
addMouseMotionListener(this);
}
boolean isWall(int r, int c) {
if (r < 0 || r >= ROWS || c < 0 || c >= COLS) return false;
return cells[r][c] == 1;
}
void dijkstra() {
if (startR == -1 || endR == -1) return;
Dijkstra d = new Dijkstra();
path = new boolean[ROWS][COLS];
visited = new boolean[ROWS][COLS];
boolean[][] result = d.run(cells, startR, startC, endR, endC);
if (result != null) {
path = result;
visited = d.visited;
} else {
visited = d.visited;
JOptionPane.showMessageDialog(this, "No path found!", "Pathfinder", JOptionPane.WARNING_MESSAGE);
}
repaint();
}
void drawCell(Graphics2D g2, int i, int j, Color face, Color top, Color side) {
int cellW = getWidth() / COLS;
int cellH = getHeight() / ROWS;
int depth = 6;
int x = j * cellW, y = i * cellH;
if (!isWall(i, j + 1)) {
g2.setColor(side);
int[] xR = {x+cellW, x+cellW+depth, x+cellW+depth, x+cellW};
int[] yR = {y, y-depth, y+cellH-depth, y+cellH};
g2.fillPolygon(xR, yR, 4);
}
if (!isWall(i - 1, j)) {
g2.setColor(top);
int[] xT = {x, x+cellW, x+cellW+depth, x+depth};
int[] yT = {y, y, y-depth, y-depth};
g2.fillPolygon(xT, yT, 4);
}
g2.setColor(face);
g2.fillRect(x, y, cellW, cellH);
g2.setColor(side);
g2.drawRect(x, y, cellW, cellH);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int cellW = getWidth() / COLS;
int cellH = getHeight() / ROWS;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
int x = j * cellW, y = i * cellH;
if (cells[i][j] == 1) {
drawCell(g2, i, j, WALL_FACE, WALL_TOP, WALL_SIDE);
} else if (cells[i][j] == 2) {
drawCell(g2, i, j, START_COL, START_COL.brighter(), START_COL.darker());
} else if (cells[i][j] == 3) {
drawCell(g2, i, j, END_COL, END_COL.brighter(), END_COL.darker());
} else if (path[i][j]) {
drawCell(g2, i, j, PATH_COL, PATH_COL.brighter(), PATH_COL.darker());
} else if (visited[i][j]) {
g2.setColor(VISITED_COL);
g2.fillRect(x, y, cellW, cellH);
g2.setColor(GRID_LINE);
g2.drawRect(x, y, cellW, cellH);
} else {
g2.setColor(EMPTY);
g2.fillRect(x, y, cellW, cellH);
g2.setColor(GRID_LINE);
g2.drawRect(x, y, cellW, cellH);
}
}
}
}
void handleMouse(MouseEvent e) {
int cellW = getWidth() / COLS;
int cellH = getHeight() / ROWS;
int col = e.getX() / cellW;
int row = e.getY() / cellH;
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) return;
if (mode.equals("start")) {
if (startR != -1) cells[startR][startC] = 0;
startR = row; startC = col;
cells[row][col] = 2;
mode = "wall";
} else if (mode.equals("end")) {
if (endR != -1) cells[endR][endC] = 0;
endR = row; endC = col;
cells[row][col] = 3;
mode = "wall";
} else {
if (cells[row][col] == 2 || cells[row][col] == 3) return;
cells[row][col] = SwingUtilities.isLeftMouseButton(e) ? 1 : 0;
}
path = new boolean[ROWS][COLS];
visited = new boolean[ROWS][COLS];
repaint();
}
public void mousePressed(MouseEvent e) { handleMouse(e); }
public void mouseDragged(MouseEvent e) { handleMouse(e); }
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Pathfinder");
Grid grid = new Grid();
JButton startBtn = new JButton("Set Start");
JButton endBtn = new JButton("Set End");
JButton runBtn = new JButton("Run");
JButton clearBtn = new JButton("Clear");
for (JButton btn : new JButton[]{startBtn, endBtn, runBtn, clearBtn}) {
btn.setBackground(new Color(20, 40, 80));
btn.setForeground(new Color(160, 180, 220));
btn.setFocusPainted(false);
btn.setBorder(BorderFactory.createLineBorder(new Color(60, 90, 150)));
}
startBtn.addActionListener(e -> grid.mode = "start");
endBtn.addActionListener(e -> grid.mode = "end");
runBtn.addActionListener(e -> grid.dijkstra());
clearBtn.addActionListener(e -> {
grid.cells = new int[ROWS][COLS];
grid.path = new boolean[ROWS][COLS];
grid.visited = new boolean[ROWS][COLS];
grid.startR = grid.startC = grid.endR = grid.endC = -1;
grid.mode = "wall";
grid.repaint();
});
JPanel bottom = new JPanel();
bottom.setBackground(new Color(10, 20, 40));
bottom.add(startBtn);
bottom.add(endBtn);
bottom.add(runBtn);
bottom.add(clearBtn);
frame.add(grid, BorderLayout.CENTER);
frame.add(bottom, BorderLayout.SOUTH);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}