├── Untitled Project.mp4 ├── MazeGenerator ├── bin │ ├── Cell.class │ ├── myMaze.class │ ├── maintest.class │ ├── myButton.class │ ├── myFrame.class │ └── myPanel.class └── src │ ├── maintest.java │ ├── myButton.java │ ├── myFrame.java │ ├── Cell.java │ ├── myMaze.java │ └── myPanel.java └── README.md /Untitled Project.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel-Pham831/Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java/HEAD/Untitled Project.mp4 -------------------------------------------------------------------------------- /MazeGenerator/bin/Cell.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel-Pham831/Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java/HEAD/MazeGenerator/bin/Cell.class -------------------------------------------------------------------------------- /MazeGenerator/bin/myMaze.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel-Pham831/Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java/HEAD/MazeGenerator/bin/myMaze.class -------------------------------------------------------------------------------- /MazeGenerator/src/maintest.java: -------------------------------------------------------------------------------- 1 | 2 | public class maintest { 3 | 4 | public static void main(String[] args) { 5 | new myFrame(); 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /MazeGenerator/bin/maintest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel-Pham831/Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java/HEAD/MazeGenerator/bin/maintest.class -------------------------------------------------------------------------------- /MazeGenerator/bin/myButton.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel-Pham831/Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java/HEAD/MazeGenerator/bin/myButton.class -------------------------------------------------------------------------------- /MazeGenerator/bin/myFrame.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel-Pham831/Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java/HEAD/MazeGenerator/bin/myFrame.class -------------------------------------------------------------------------------- /MazeGenerator/bin/myPanel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel-Pham831/Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java/HEAD/MazeGenerator/bin/myPanel.class -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | https://user-images.githubusercontent.com/83607627/117017528-dab6c500-ad1d-11eb-9d98-49dc1f1cb243.mp4 3 | 4 | # Visualize-a-Maze-Generator-and-some-Path-Finding-algorithms-In-Java 5 | 6 | I'm using eclipse IDE for this project 7 | Youtube link: https://youtu.be/OG30ZnSFFSs 8 | -------------------------------------------------------------------------------- /MazeGenerator/src/myButton.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | 3 | import javax.swing.*; 4 | 5 | public class myButton extends JButton{ 6 | myButton(String name,int x,int y,int width,int height,Color color){ 7 | this.setText(name); 8 | this.setBounds(x, y, width, height); 9 | this.setLocation(x, y); 10 | this.setBackground(color); 11 | this.setHorizontalTextPosition(JButton.CENTER); 12 | this.setVerticalTextPosition(JButton.CENTER); 13 | this.setFocusable(false); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MazeGenerator/src/myFrame.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class myFrame extends JFrame{ 4 | public myPanel panel; 5 | 6 | public final int windowWidthSize = 1000; 7 | public final int windowHeightSize = 600; 8 | myFrame(){ 9 | 10 | this.setTitle("Maze Generator and Pathfinding Algorithms"); 11 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 | this.setSize(windowWidthSize, windowHeightSize); 13 | this.setResizable(false); 14 | panel = new myPanel(windowWidthSize,windowHeightSize); 15 | this.add(panel); 16 | this.pack(); 17 | this.setLayout(null); 18 | this.setVisible(true); 19 | this.setLocationRelativeTo(null); 20 | 21 | 22 | 23 | 24 | 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /MazeGenerator/src/Cell.java: -------------------------------------------------------------------------------- 1 | import java.awt.BasicStroke; 2 | import java.awt.Color; 3 | import java.awt.Font; 4 | import java.awt.Graphics; 5 | import java.awt.Graphics2D; 6 | import java.awt.Rectangle; 7 | import java.util.ArrayList; 8 | import java.util.Random; 9 | 10 | public class Cell{ 11 | public int row,r,col,c,cellSize; 12 | public boolean[] Walls = {true,true,true,true}; //top right bottom left 13 | public ArrayList next; 14 | public Cell parent; 15 | public boolean visited = false; //for mazeDrawer 16 | private int weight = 1; 17 | private int fontWeight; 18 | private int strokeSize; 19 | public boolean visitedPath = false; //for pathFinder 20 | 21 | 22 | Cell(int row,int col,int cellSize){ 23 | this.row = row; 24 | this.col = col; 25 | r = row*cellSize; 26 | c = col*cellSize; 27 | this.cellSize = cellSize; 28 | next = new ArrayList(); 29 | parent = null; 30 | fontWeight = (int)(10+0.3*(cellSize-20)); //cái này để khi cellSize bự thì font bự theo 31 | strokeSize = (int)((5.0/9 * ((cellSize/10)-1) )+1); // cái này để khi cellsize bự thì stroke bự theo 32 | } 33 | 34 | public void drawCell(Graphics g) { 35 | Graphics2D g2 = (Graphics2D) g; 36 | 37 | g2.setColor(new Color(0,0,0)); 38 | g2.setStroke(new BasicStroke(strokeSize)); 39 | 40 | if(Walls[0] == true) 41 | g2.drawLine(c, r, c+cellSize, r); // top 42 | if(Walls[1] == true) 43 | g2.drawLine(c+cellSize, r, c+cellSize, r+cellSize); //right 44 | if(Walls[2] == true) 45 | g2.drawLine(c+cellSize, r+cellSize, c, r+cellSize); //bottom 46 | if(Walls[3] == true) 47 | g2.drawLine(c, r+cellSize, c, r); //left 48 | 49 | g2.setStroke(new BasicStroke(1)); 50 | } 51 | 52 | public void drawWeight(Graphics g) { 53 | g.setFont(new Font("",Font.BOLD,fontWeight)); 54 | g.drawString(""+weight, (int)(c+cellSize/2)-(int)(fontWeight/3), (int)(r+cellSize/2)+(int)(fontWeight/3)); 55 | 56 | } 57 | 58 | public void drawBox(Graphics g,Color color) { 59 | Graphics2D g2 = (Graphics2D) g; 60 | if(this.visited == true) { 61 | g2.setColor(color); 62 | g2.fillRect(c, r, cellSize, cellSize); 63 | } 64 | } 65 | 66 | public void drawPath(Graphics g,Color color) { 67 | Graphics2D g2 = (Graphics2D) g; 68 | g2.setColor(color); 69 | if(parent!=null) { 70 | if(this.visitedPath == true) { 71 | g2.fillRect(c+(int)(cellSize/3), r+(int)(cellSize/3), (int)(cellSize/3), (int)(cellSize/3)); 72 | } 73 | if(row - 1 == parent.row) { //top 74 | g2.fillRect(c+(int)(cellSize/3), r, (int)(cellSize/3), (int)(cellSize/3)); 75 | g2.fillRect(parent.c+(int)(cellSize/3), parent.r+((int)(cellSize/3)*2), (int)(cellSize/3), (int)(cellSize/3)); 76 | } 77 | 78 | if(col + 1 == parent.col) { //right 79 | g2.fillRect(c+((int)(cellSize/3)*2), r+(int)(cellSize/3), (int)(cellSize/3), (int)(cellSize/3)); 80 | g2.fillRect(parent.c, parent.r+(int)(cellSize/3), (int)(cellSize/3), (int)(cellSize/3)); 81 | } 82 | 83 | if(row + 1 == parent.row) { //bot 84 | g2.fillRect(c+(int)(cellSize/3), r+((int)(cellSize/3)*2), (int)(cellSize/3), (int)(cellSize/3)); 85 | g2.fillRect(parent.c+(int)(cellSize/3), parent.r, (int)(cellSize/3), (int)(cellSize/3)); 86 | } 87 | 88 | if(col - 1 == parent.col) { //left 89 | g2.fillRect(c, r+(int)(cellSize/3), (int)(cellSize/3), (int)(cellSize/3)); 90 | g2.fillRect(parent.c+((int)(cellSize/3)*2), parent.r+(int)(cellSize/3), (int)(cellSize/3), (int)(cellSize/3)); 91 | } 92 | 93 | 94 | } 95 | } 96 | 97 | 98 | public void resetCell() { 99 | visited = false; 100 | visitedPath = false; 101 | parent = null; 102 | next = new ArrayList<>(); 103 | } 104 | 105 | public int getWeight() { 106 | return weight; 107 | } 108 | 109 | public void setWeight() { 110 | this.weight = new Random().nextInt(9)+1; 111 | } 112 | 113 | 114 | } 115 | -------------------------------------------------------------------------------- /MazeGenerator/src/myMaze.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import java.util.*; 3 | 4 | public class myMaze{ 5 | private final int MAZE_SIZE; 6 | private int CELL_SIZE; 7 | private final int w ; 8 | private Cell[][] grids; 9 | private Cell current; 10 | private Cell next; 11 | private Stack visitedStack; 12 | private boolean running = false; 13 | private int countVisited=0; 14 | public boolean finish = false; 15 | private ArrayList pathsFromAtoB; 16 | 17 | private Cell start,end; 18 | private Queue visitedQueue; 19 | private Stack visitedS; 20 | 21 | myMaze(int mazeSize,int cellSize){ 22 | MAZE_SIZE = mazeSize; 23 | CELL_SIZE = cellSize; 24 | w = (int)(MAZE_SIZE/CELL_SIZE); 25 | init(); 26 | } 27 | 28 | private void init() { 29 | grids = new Cell[w][w]; 30 | for (int row=0;row(); 38 | running = true; 39 | 40 | } 41 | public void resetMaze(){ 42 | for (int row=0;row visitedList = new Stack<>(); 100 | current.visited = true; 101 | visitedList.push(current); 102 | while(!visitedList.isEmpty()) { 103 | current = visitedList.pop(); 104 | if (hasNeighbor(current)) { 105 | visitedList.push(current); 106 | next = getOneRandomNeighbor(current); 107 | wallBreaker(current, next); 108 | next.visited = true; 109 | visitedList.push(next); 110 | } 111 | } 112 | } 113 | 114 | 115 | 116 | public void update() { 117 | if (running == false) 118 | return; 119 | 120 | next = getOneRandomNeighbor(current); 121 | if(next != null) { 122 | visitedStack.push(next); 123 | countVisited++; 124 | next.visited = true; 125 | wallBreaker(current, next); 126 | current = next; 127 | }else { 128 | while(!hasNeighbor(current)) { 129 | if(!visitedStack.empty()) 130 | current = visitedStack.pop(); 131 | else { 132 | running = false; 133 | countVisited++; 134 | return; 135 | } 136 | } 137 | update(); 138 | } 139 | } 140 | 141 | public void mazeFinderBFS(Graphics g) { 142 | 143 | next = getOneNeighbor(current); 144 | luuVetFromAtoB(current,next); 145 | if(next == end) { 146 | System.out.println("Finished"); 147 | finish = true; 148 | taoPathTuEndToiStart(); 149 | return; 150 | } 151 | 152 | if(next != end && next != null) { 153 | visitedQueue.offer(next); 154 | next.visitedPath = true; 155 | next.visited = true; 156 | next.drawPath(g, Color.RED); 157 | }else { 158 | if(!visitedQueue.isEmpty()) 159 | current = visitedQueue.poll(); 160 | else { 161 | return; 162 | } 163 | } 164 | } 165 | 166 | 167 | public void mazeFinderDFS(Graphics g) { 168 | 169 | next = getOneNeighbor(current); 170 | luuVetFromAtoB(current,next); 171 | if(next == end) { 172 | System.out.println("Finished"); 173 | finish = true; 174 | taoPathTuEndToiStart(); 175 | return; 176 | } 177 | 178 | if(next != end && next != null) { 179 | visitedS.push(next); 180 | next.visitedPath = true; 181 | next.visited = true; 182 | next.drawPath(g, Color.RED); 183 | }else { 184 | if(!visitedS.isEmpty()) { 185 | current = visitedS.pop(); 186 | } 187 | else { 188 | return; 189 | } 190 | } 191 | } 192 | 193 | 194 | private void taoPathTuEndToiStart() { 195 | pathsFromAtoB.add(end); 196 | Cell tempParent = end.parent; 197 | while(tempParent != start) { 198 | pathsFromAtoB.add(tempParent); 199 | tempParent = tempParent.parent; 200 | } 201 | } 202 | 203 | 204 | private void luuVetFromAtoB(Cell A,Cell B) { 205 | if(B!=null) { 206 | B.parent = A; 207 | } 208 | if(A!=null) 209 | A.next.add(B); 210 | } 211 | 212 | 213 | public void initStartAndEnd() { 214 | 215 | 216 | start = grids[new Random().nextInt(w)][new Random().nextInt(w)]; 217 | end = grids[new Random().nextInt(w)][new Random().nextInt(w)]; 218 | 219 | if (start == end){ 220 | end = grids[new Random().nextInt(w)][new Random().nextInt(w)]; 221 | } 222 | 223 | for(int i = 0;i(); 233 | visitedS = new Stack(); 234 | pathsFromAtoB = new ArrayList(); 235 | current = start; 236 | next = current; 237 | 238 | } 239 | 240 | 241 | 242 | public Cell getOneNeighbor(Cell currentCell){ //for pathfinder 243 | ArrayList neighbors = new ArrayList<>(); 244 | 245 | if (currentCell.row-1 >= 0 && grids[currentCell.row-1][currentCell.col].visitedPath == false && currentCell.Walls[0] == false) { 246 | neighbors.add(grids[currentCell.row-1][currentCell.col]); // top 247 | } 248 | 249 | if (currentCell.col+1 < w && grids[currentCell.row][currentCell.col+1].visitedPath == false && currentCell.Walls[1] == false) { 250 | neighbors.add(grids[currentCell.row][currentCell.col+1]); //right 251 | } 252 | 253 | if (currentCell.row + 1 < w && grids[currentCell.row+1][currentCell.col].visitedPath == false && currentCell.Walls[2] == false) { 254 | neighbors.add(grids[currentCell.row+1][currentCell.col]); // bot 255 | } 256 | 257 | if (currentCell.col-1 >= 0 && grids[currentCell.row][currentCell.col-1].visitedPath == false && currentCell.Walls[3] == false) { 258 | neighbors.add(grids[currentCell.row][currentCell.col-1]); //left 259 | } 260 | 261 | 262 | if (neighbors.isEmpty()) 263 | return null; 264 | 265 | return neighbors.get(0); 266 | } 267 | 268 | public Cell getOneRandomNeighbor(Cell currentCell) { //for maze generator 269 | ArrayList neighbors = new ArrayList<>(); 270 | 271 | if (currentCell.row-1 >= 0 && grids[currentCell.row-1][currentCell.col].visited == false) { 272 | neighbors.add(grids[currentCell.row-1][currentCell.col]); // top 273 | } 274 | 275 | if (currentCell.col+1 < w && grids[currentCell.row][currentCell.col+1].visited == false) { 276 | neighbors.add(grids[currentCell.row][currentCell.col+1]); //right 277 | } 278 | 279 | if (currentCell.row + 1 < w && grids[currentCell.row+1][currentCell.col].visited == false) { 280 | neighbors.add(grids[currentCell.row+1][currentCell.col]); // bot 281 | } 282 | 283 | if (currentCell.col-1 >= 0 && grids[currentCell.row][currentCell.col-1].visited == false) { 284 | neighbors.add(grids[currentCell.row][currentCell.col-1]); //left 285 | } 286 | 287 | 288 | if (neighbors.isEmpty()) 289 | return null; 290 | 291 | return neighbors.get(new Random().nextInt(neighbors.size())); 292 | 293 | } 294 | public boolean hasNeighbor(Cell currentCell) { 295 | if (getOneRandomNeighbor(currentCell)!=null) 296 | return true; 297 | return false; 298 | } 299 | public void wallBreaker(Cell cellA,Cell cellB) { 300 | if (cellA.row == cellB.row+1) { // B top A 301 | cellA.Walls[0] = false; 302 | cellB.Walls[2] = false; 303 | return; 304 | } 305 | 306 | if (cellA.row == cellB.row-1) { // B bot A 307 | cellA.Walls[2] = false; 308 | cellB.Walls[0] = false; 309 | return; 310 | } 311 | 312 | if (cellA.col == cellB.col+1) { // B left A 313 | cellA.Walls[3] = false; 314 | cellB.Walls[1] = false; 315 | return; 316 | } 317 | 318 | if (cellA.col == cellB.col-1) { // B right A 319 | cellA.Walls[1] = false; 320 | cellB.Walls[3] = false; 321 | return; 322 | } 323 | 324 | 325 | 326 | } 327 | 328 | public int getCellSize() { 329 | return CELL_SIZE; 330 | } 331 | 332 | public void setCellSize(int cellSize) { 333 | CELL_SIZE = cellSize; 334 | } 335 | 336 | 337 | } 338 | -------------------------------------------------------------------------------- /MazeGenerator/src/myPanel.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | import javax.swing.*; 5 | import javax.swing.border.Border; 6 | import javax.swing.event.ChangeEvent; 7 | import javax.swing.event.ChangeListener; 8 | 9 | public class myPanel extends JPanel implements ActionListener,ChangeListener{ 10 | 11 | private final int mazeSize = 600; 12 | private int cellSize = 40; 13 | private int slowestDelay = 110; 14 | private int delay = 10; 15 | private int windowW,windowH; 16 | private int running = -1; 17 | 18 | private int switchCaseVar = 0; 19 | 20 | private myMaze maze; 21 | private Timer tm; 22 | 23 | 24 | private JPanel smallPanel; 25 | private myButton startButton; 26 | private myButton resetButton; 27 | private myButton reMazeButton; 28 | 29 | private JLabel cellLabel; 30 | private JSlider cellSlider; 31 | 32 | private JLabel speedLabel; 33 | private JSlider speedSlider; 34 | 35 | private JCheckBox mazeCheckbox; 36 | 37 | private JCheckBox BFSCheckbox; 38 | private JComboBox algoBox; 39 | private int mode ; 40 | private JLabel algoBoxLabel; 41 | 42 | 43 | private myButton startSolvingButton; 44 | private boolean flag = true; 45 | 46 | 47 | 48 | 49 | myPanel(int windowW,int windowH){ 50 | this.windowW = windowW; 51 | this.windowH = windowH; 52 | 53 | tm = new Timer(delay,this); 54 | 55 | this.setPreferredSize(new Dimension(windowW,windowH)); 56 | this.setBounds(0, 0, windowW, windowH); 57 | this.setBackground(new Color(250,250,250)); 58 | this.setLayout(null); 59 | 60 | 61 | //cái bảng bên phải 62 | smallPanel = new JPanel(); 63 | smallPanel.setPreferredSize(new Dimension((windowW-mazeSize),windowH)); 64 | smallPanel.setBounds(mazeSize, 0, windowW-mazeSize, windowH); 65 | smallPanel.setLayout(null); 66 | this.add(smallPanel); 67 | 68 | 69 | //nút start 70 | startButton = new myButton("Start",(int)(smallPanel.getSize().width/2)-120-30, 30, 120, 50,Color.PINK); 71 | smallPanel.add(startButton); 72 | startButton.addActionListener(this); 73 | 74 | 75 | 76 | //nút re-maze 77 | reMazeButton = new myButton("Re-Maze",(int)(smallPanel.getSize().width/2)+30, 30, 120, 50,Color.CYAN); 78 | smallPanel.add(reMazeButton); 79 | reMazeButton.addActionListener(this); 80 | reMazeButton.setEnabled(false); 81 | 82 | 83 | 84 | 85 | //Bảng lựa chọn cellSlider + Label của nó 86 | cellSlider = new JSlider(10,100,40); 87 | cellSlider.setBounds((windowW-mazeSize)/2-(350/2), 150, 350, 50); 88 | cellSlider.setPaintTicks(true); 89 | cellSlider.setMinorTickSpacing(5); 90 | cellSlider.setPaintTrack(true); 91 | cellSlider.setMajorTickSpacing(10); 92 | cellSlider.setSnapToTicks(true); 93 | cellSlider.setPaintLabels(true); 94 | cellSlider.addChangeListener(this); 95 | 96 | cellLabel = new JLabel(); 97 | cellLabel.setText("Cell's size adjustment: " + cellSlider.getValue()); 98 | cellLabel.setBounds(30, 130, 250, 20); 99 | cellLabel.setFont(new Font("",Font.BOLD,18)); 100 | 101 | smallPanel.add(cellLabel); 102 | smallPanel.add(cellSlider); 103 | 104 | 105 | //Bảng lựa chọn speed va label cua no 106 | speedSlider = new JSlider(1,5,5); 107 | speedSlider.setBounds((windowW-mazeSize)/2-(350/2), 230, 350, 40); 108 | speedSlider.setPaintTrack(true); 109 | speedSlider.setMajorTickSpacing(1); 110 | speedSlider.setSnapToTicks(true); 111 | speedSlider.setPaintLabels(true); 112 | speedSlider.addChangeListener(this); 113 | 114 | speedLabel = new JLabel(); 115 | speedLabel.setText("Speed: " + speedSlider.getValue()); 116 | speedLabel.setBounds(30, 210, 180, 20); 117 | speedLabel.setFont(new Font("",Font.BOLD,18)); 118 | 119 | smallPanel.add(speedLabel); 120 | smallPanel.add(speedSlider); 121 | 122 | 123 | //generate Check box 124 | mazeCheckbox = new JCheckBox(); 125 | mazeCheckbox.setBounds((int)(smallPanel.getSize().width/2)-120-30, 95, 250, 20); 126 | mazeCheckbox.setText("Generate instantly"); 127 | mazeCheckbox.setFont(new Font("",Font.BOLD,15)); 128 | mazeCheckbox.setFocusable(false); 129 | 130 | 131 | smallPanel.add(mazeCheckbox); 132 | 133 | /* 134 | //Check box 135 | BFSCheckbox = new JCheckBox(); 136 | BFSCheckbox.setBounds(smallPanel.getSize().width/2-30, 285, 250, 30); 137 | BFSCheckbox.setText("Breadth First Search"); 138 | BFSCheckbox.setFont(new Font("",Font.BOLD,15)); 139 | BFSCheckbox.setFocusable(false); 140 | smallPanel.add(BFSCheckbox); 141 | */ 142 | 143 | 144 | 145 | 146 | algoBoxLabel = new JLabel("Pathfinding Algorithms"); 147 | algoBoxLabel.setBounds(smallPanel.getSize().width/2-155, 370, 220, 30); 148 | algoBoxLabel.setFont(new Font("",Font.BOLD,15)); 149 | smallPanel.add(algoBoxLabel); 150 | 151 | 152 | 153 | String[] algoList = {"Breadth First Search(BFS)","Depth First Search(DFS)"};// (0 == BFS , 1 = DFS) 154 | algoBox = new JComboBox(algoList); 155 | algoBox.setBounds(smallPanel.getSize().width/2-160, 400, 220, 30); 156 | algoBox.setFont(new Font("",Font.BOLD,15)); 157 | algoBox.setFocusable(false); 158 | algoBox.addActionListener(this); 159 | //algoBox.setEnabled(false); 160 | mode = algoBox.getSelectedIndex(); // (0 == BFS , 1 = DFS) 161 | smallPanel.add(algoBox); 162 | 163 | 164 | 165 | 166 | 167 | //BFS button 168 | startSolvingButton = new myButton("Start",smallPanel.getSize().width/2-130-20, 300, 130, 60,new Color(255, 231, 122)); 169 | smallPanel.add(startSolvingButton); 170 | startSolvingButton.addActionListener(this); 171 | startSolvingButton.setEnabled(false); 172 | 173 | //nút resetMaze 174 | resetButton = new myButton("Reset Maze",smallPanel.getSize().width/2+20, 300, 130, 60,new Color(44, 95, 45)); 175 | smallPanel.add(resetButton); 176 | resetButton.addActionListener(this); 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | initMaze(); 185 | tm.start(); 186 | } 187 | 188 | 189 | private void initMaze() { 190 | maze = new myMaze(mazeSize,cellSize); 191 | } 192 | 193 | public void paintComponent(Graphics g) { 194 | 195 | if(maze.checkFinished()) { 196 | reMazeButton.setEnabled(true); 197 | startSolvingButton.setEnabled(true); 198 | algoBox.setEnabled(true); 199 | }else { 200 | startSolvingButton.setEnabled(false); 201 | resetButton.setEnabled(false); 202 | algoBox.setEnabled(false); 203 | } 204 | 205 | super.paintComponent(g); 206 | 207 | 208 | 209 | 210 | 211 | maze.drawMaze(g); 212 | 213 | 214 | if(!flag) { 215 | maze.drawPathFinder(g,mode); 216 | if(maze.finish) { 217 | mazeCheckbox.setEnabled(true); 218 | startSolvingButton.setEnabled(true); 219 | resetButton.setEnabled(true); 220 | tm.stop(); 221 | } 222 | } 223 | 224 | if(!mazeCheckbox.isSelected()) { 225 | maze.mazeAlgorithm(g); 226 | } 227 | else { 228 | maze.drawMazeInstantly(); 229 | } 230 | 231 | 232 | 233 | 234 | 235 | } 236 | private void reset() { 237 | tm.start(); 238 | running = -1; 239 | startButton.setEnabled(true); 240 | repaint(); 241 | } 242 | 243 | 244 | 245 | 246 | 247 | 248 | @Override 249 | public void actionPerformed(ActionEvent e) { 250 | 251 | 252 | 253 | 254 | 255 | //nhan nut re-maze, tạo lại maze mới 256 | if(e.getSource()==reMazeButton) { 257 | flag = true; 258 | mazeCheckbox.setEnabled(true); 259 | initMaze(); 260 | reset(); 261 | startButton.setText("Start"); 262 | } 263 | 264 | 265 | 266 | //nhấn nút start 267 | if (e.getSource()==startButton) { 268 | running *= -1; 269 | if(running == 1) 270 | startButton.setText("Pause"); 271 | else 272 | startButton.setText("Start"); 273 | } 274 | 275 | 276 | 277 | if(e.getSource()==algoBox) { 278 | mode = algoBox.getSelectedIndex(); 279 | } 280 | 281 | 282 | 283 | 284 | //start của bên thuật toán tìm đường 285 | if(e.getSource() == startSolvingButton) { 286 | mazeCheckbox.setSelected(false); 287 | mazeCheckbox.setEnabled(false); 288 | 289 | if(flag) { 290 | maze.initStartAndEnd(); 291 | flag = false; 292 | } 293 | 294 | } 295 | 296 | 297 | //nút reset maze 298 | if(e.getSource()==resetButton) { 299 | flag = true; 300 | maze.resetMaze(); 301 | tm.start(); 302 | } 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | if(running==1) 313 | repaint(); 314 | } 315 | 316 | 317 | @Override 318 | public void stateChanged(ChangeEvent e) { 319 | 320 | 321 | //cellSlider 322 | if(e.getSource()==cellSlider) { 323 | if(cellSlider.getValue()%5==0) { 324 | mazeCheckbox.setEnabled(true); 325 | cellLabel.setText("Cell's size adjustment: " + cellSlider.getValue()); 326 | cellSize = cellSlider.getValue(); 327 | startButton.setText("Start"); 328 | flag = true; 329 | initMaze(); 330 | reset(); 331 | } 332 | } 333 | 334 | 335 | 336 | //SpeedSlider 337 | if(e.getSource()==speedSlider) { 338 | speedLabel.setText("Speed: " + speedSlider.getValue()); 339 | delay = slowestDelay - (speedSlider.getValue()-1)* ((slowestDelay-10)/4) ; 340 | tm.setDelay(delay); 341 | System.out.println(delay); 342 | } 343 | 344 | 345 | 346 | 347 | 348 | } 349 | 350 | 351 | 352 | 353 | 354 | } 355 | --------------------------------------------------------------------------------