📄 frmmaze.java
字号:
.addComponent(pnlToolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pnlMaze, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void mnuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuExitActionPerformed // TODO add your handling code here: if(JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Exit?", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION ){ System.exit(0); } }//GEN-LAST:event_mnuExitActionPerformed private void mnuHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuHelpActionPerformed // TODO add your handling code here: }//GEN-LAST:event_mnuHelpActionPerformed private void mnuAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAboutActionPerformed // TODO add your handling code here: JOptionPane.showMessageDialog(null, "Developed by Trinh Hoang Nhu\nIT060085","About",JOptionPane.INFORMATION_MESSAGE); }//GEN-LAST:event_mnuAboutActionPerformed private void mnuEuclideanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuEuclideanActionPerformed // TODO add your handling code here: Global.heuristics = 0; }//GEN-LAST:event_mnuEuclideanActionPerformed private void mnuManhattanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuManhattanActionPerformed // TODO add your handling code here: Global.heuristics = 1; }//GEN-LAST:event_mnuManhattanActionPerformed private void btnSolveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSolveActionPerformed // TODO add your handling code here: if(maze==null || (maze!=null && !maze.ready)){ JOptionPane.showMessageDialog(null, "Please load maze first"); return; } maze.findBestPath(); createMazePanel(); if(maze.bestList.size()<1) JOptionPane.showMessageDialog(null, "Maze is unsolve"); }//GEN-LAST:event_btnSolveActionPerformed private void mnuNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuNewActionPerformed // TODO add your handling code here: String mazew = JOptionPane.showInputDialog("Input maze width","10"); String mazeh = JOptionPane.showInputDialog("Input maze height","10"); try{ int w = new Integer(mazew); int h = new Integer(mazeh); String startx = JOptionPane.showInputDialog("Input start x (coordiante x of Start point)","0"); String starty = JOptionPane.showInputDialog("Input start y (coordiante y of Start point)","0"); String goalx = JOptionPane.showInputDialog("Input goal x (coordiante x of Goal point)",w-1); String goaly = JOptionPane.showInputDialog("Input goal y (coordiante y of Goal point)",h-1); int sx = new Integer(startx); int sy = new Integer(starty); int gx = new Integer(goalx); int gy = new Integer(goaly); int[][] map = new int[w][h]; for(int i = 0;i < w; i++) for(int j = 0 ; j < h; j ++){ map[i][j] = 0; } maze = new Maze(h, w, map, sx, sy, gx, gy); createMazePanel(); }catch(Exception ex){ JOptionPane.showMessageDialog(null,ex.getMessage()+ "\nYou must enter a number"); } }//GEN-LAST:event_mnuNewActionPerformed private void mnuRanMazeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuRanMazeActionPerformed // TODO add your handling code here: String mazew = JOptionPane.showInputDialog("Input maze width","10"); String mazeh = JOptionPane.showInputDialog("Input maze height","10"); //try{ int w = new Integer(mazew); int h = new Integer(mazeh); int[][] map = new int[h][w]; int startx = 0; int starty = 0; int goalx = 0; int goaly = 0; while (goalx == 0 && goaly == 0) { goalx = new Random().nextInt(h); goaly = new Random().nextInt(w); } for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if((i == goalx && j == goaly) || (i == startx && j == starty)) map[i][j] = -1; else if(new Random().nextInt(w*h) > (w*h/3)) map[i][j]=new Random().nextInt(2); else map[i][j] =0 ; } /*Just test map data loaded correctly*/ System.out.println("Loaded random map"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { System.out.print(map[i][j] + " "); } System.out.println(); } this.maze = new Maze(h, w, map, startx, starty, goalx, goaly); createMazePanel(); /* }catch(Exception ex){ JOptionPane.showMessageDialog(null, ex.getMessage()); }*/ }//GEN-LAST:event_mnuRanMazeActionPerformed private void mnuLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuLoadActionPerformed // TODO add your handling code here: JFileChooser fc = new JFileChooser(); fc.setDialogTitle("Select maze file"); if(fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){ try { this.maze = new Maze(fc.getSelectedFile().getAbsolutePath()); createMazePanel(); } catch (FileNotFoundException ex) { Logger.getLogger(frmmaze.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_mnuLoadActionPerformed private void mnuSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSaveActionPerformed // TODO add your handling code here: if (!maze.ready){ JOptionPane.showMessageDialog(null, "There is no maze ready"); return; } JFileChooser fc = new JFileChooser(); if(fc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){ BufferedWriter write = null; try { File a = fc.getSelectedFile(); write = new BufferedWriter(new FileWriter(a)); write.write(maze.getColumns() + " " + maze.getRows() + "\n"); write.write(maze.startx + " " + maze.starty + " " + maze.goalx + " " + maze.goaly + "\n"); for(int i = 0; i < maze.getRows();i++){ for(int j=0; j < maze.getColumns();j++){ if(j<maze.getColumns()-1) write.write(maze.map[i][j] + " "); else write.write(maze.map[i][j] + "\n"); } } write.close(); } catch (IOException ex) { Logger.getLogger(frmmaze.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(null, "Can not write to file"); } finally { try { write.close(); } catch (IOException ex) { Logger.getLogger(frmmaze.class.getName()).log(Level.SEVERE, null, ex); } } } }//GEN-LAST:event_mnuSaveActionPerformed private void createMazePanel(){ if(maze == null || (maze!=null && !maze.ready)){ JOptionPane.showMessageDialog(null, "Maze is not loaded, please load it first"); } pnlMaze.removeAll(); pnlMaze.setSize(maze.getColumns() * MazeCell.CELL_W, maze.getRows() * MazeCell.CELL_H); pnlMaze.setVisible(true); pnlMaze.setLayout(new GridLayout(maze.getRows(), maze.getColumns())); boolean isSolve = false; for (int i = 0; i < maze.getRows(); i++) { for (int j = 0; j < maze.getColumns(); j++) { MazeCell cell = new MazeCell(i, j); cell.setSize(MazeCell.CELL_W, MazeCell.CELL_H); Square s = maze.getSquare(i, j); cell.setLocation(j*MazeCell.CELL_W, i*MazeCell.CELL_H); if(s.isStart()) cell.setStart(true); if(s.isEnd()) cell.setFinish(true); boolean isPath = false; for(int t = 0; t < maze.bestList.size(); t++){ Square b = maze.bestList.get(t); if(b.getX()==i && b.getY()==j) { System.out.println("Got path: " + b.getX() + " " + b.getY()); isPath=true; } } if(isPath){ cell.setUsed(true); cell.setVisited(true); cell.setWall(false); } else if(maze.map[i][j]==1) cell.setWall(true); else cell.setWall(false); cell.revalidate(); cell.repaint(); pnlMaze.add(cell); } } pnlMaze.repaint(); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new frmmaze().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.ButtonGroup HeuGroup; private javax.swing.JDialog MyDialog; private javax.swing.JButton btnSolve; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JMenuItem mnuAbout; private javax.swing.JMenu mnuEdit; private javax.swing.JRadioButtonMenuItem mnuEuclidean; private javax.swing.JMenuItem mnuExit; private javax.swing.JMenuItem mnuExport; private javax.swing.JMenu mnuFile; private javax.swing.JMenu mnuHelp; private javax.swing.JMenuItem mnuLoad; private javax.swing.JRadioButtonMenuItem mnuManhattan; private javax.swing.JMenuItem mnuMazeEdit; private javax.swing.JMenuItem mnuNew; private javax.swing.JMenuItem mnuRanMaze; private javax.swing.JMenuItem mnuSave; private javax.swing.JMenu mnuheuristics; private javax.swing.JPanel pnlMaze; private javax.swing.JPanel pnlToolbar; // End of variables declaration//GEN-END:variables}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -