⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manager.java

📁 简单的迷宫生成算法、复杂的迷宫生成算法、简单的迷宫搜索算法、复杂的迷宫搜索算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        * Pauses a build operation
        * Called from the event thread.
        */
       public void pauseBuild()
       {
               if(state != STATE_BUILDING)
               {
                       signalError( "pauseBuild: Unable to pause, state is: "+state+" instead of STATE_BUILDING." );
                       updateGui();
                       return;
               }
               this.state = STATE_BUILD_PAUSED;
               updateGui();
       }
       
       
       /**
        * Starts a solve operation, calls the initialize function of the algorithm.
        * Not called from the event thread.
        * @param algorithm The solve algorithm object.
        * @param delay The algorithm delay in milliseconds.
        */
       public void startSolve(Solve algorithm, int delay)
       {
               //Abort if the maze is not finished
               if(state < STATE_BUILD_FINISHED)
               {
                       this.signalError( "startSolve: Maze build is unfinished!" );
                       updateGui();
                       return;
               }
               //Clear solve state if any.
               else if(state > STATE_BUILD_FINISHED)
               {
                       clearSolveState();
               }
                       
               
               solveAlgorithm = algorithm;
               algorithmDelay = delay;

               state = STATE_SOLVING;
               solveTime = 0;
               solveTickCount = 0;
               updateGui();
               
               try
               {
                       solveAlgorithm.initialise(maze);
               }
               catch (SolveException se)
               {
                       this.state = STATE_SOLVE_PAUSED;
                       this.signalError( "startSolve: SolveException: " + se.getMessage() );
                       updateGui();
                       return;
               }
               
               continueSolve(delay);
       }
       
       /**
        * Continues a solve operation after initialization or pause.
        * Not called from the event thread.
        */
       public void continueSolve(int delay)
       {
               algorithmDelay = delay;
               
               if(solveAlgorithm == null)
               {
                       this.state = STATE_SOLVE_PAUSED;
                       this.signalError( "continueSolve: solveAlgorithm == null." );
                       updateGui();
                       return;
               }
               
               state = STATE_SOLVING;
               updateGui();
               
               while(this.state == STATE_SOLVING && ! solveAlgorithm.isDone())
               {
                       try
                       {
                               long startTime = System.currentTimeMillis();
                               solveAlgorithm.tick();
                               solveTime += System.currentTimeMillis() - startTime;
                               solveTickCount++;
                               
                               if(algorithmDelay != 0)
                               {
                                       mazeGui.repaintMazeGraph();
                                       SwingUtilities.invokeLater(new GTUpdateBenchmark());
                                       Thread.sleep( algorithmDelay );
                               }
                       }
                       catch (InterruptedException e) {
                               e.printStackTrace();
                       }
                       catch (SolveException se)
                       {
                               this.state = STATE_SOLVE_PAUSED;
                               this.signalError( "continueSolve: SolveException: " + se.getMessage() );
                               updateGui();
                               return;
                       }
               }
               
               //Check if we're finished and not paused
               if(solveAlgorithm.isDone() && this.state != STATE_SOLVE_PAUSED)
               {
                       this.state = STATE_SOLVED;
                       solveAlgorithm = null;
                       updateGui();
               }
               mazeGui.repaintMazeGraph();
       }
       
       /**
        * Pauses a solve operation
        * Called from the event thread.
        */
       public void pauseSolve()
       {
               if(state != STATE_SOLVING)
               {
                       signalError( "pauseSolve: Unable to pause, state is: "+state+" instead of STATE_SOLVING." );
                       updateGui();
                       return;
               }
               this.state = STATE_SOLVE_PAUSED;
               updateGui();
       }
       
       /**
        * Clears everything in the maze.
        * Can be called from the event thread or not.
        */
       public void clearAll()
       {
               buildAlgorithm = null;
               solveAlgorithm = null;
               maze.clear();
               state = STATE_CLEAR;
               updateGui();
               mazeGui.repaintMazeGraph();
       }
       
       /**
        * Pauses a solve operation
        * Can be called from the event thread or not.
        */
       public void clearSolveState()
       {
               maze.clearSolveState();
               
               solveAlgorithm = null;
               state = STATE_BUILD_FINISHED;
               updateGui();
               mazeGui.repaintMazeGraph();
       }
       
       public boolean isBusy()
       {
               return this.state == STATE_BUILDING || this.state == STATE_SOLVING;
       }
       public int getState()
       {
               return this.state;
       }
       
       
       /**
        * Function that signals the gui the state has changed.
        */
       private void updateGui()
       {
               SwingUtilities.invokeLater( new GTUpdateGui() );
               // Update for final benchmarks as well
               SwingUtilities.invokeLater(new GTUpdateBenchmark());
       }
       
       /**
        * Function that signals the gui that an error has occurred.
        */
       private void signalError(String message)
       {
               SwingUtilities.invokeLater( new GTSignalError(message) );
       }
       
       /*
        * Runnable objects designed to run on the EVT thread.
        * Because we are not allowed to alter swing components directly from this thread.
        * (GT stands for GuiThread)
        */
       
       /**
        * Initialize the GUI.
        */
       private class GTInitGui implements Runnable
       {
               @Override
               public void run()
               {
                       mazeGui = new MazeGui(Manager.this, maze);
               }
       }
       
       /**
        * Updates GUI benchmarks
        */
       private class GTUpdateBenchmark implements Runnable
       {
               @Override
               public void run()
               {
                       mazeGui.updateBenchmark(buildTime, buildTickCount,
                                       solveTime, solveTickCount);
               }
       }
       
       
       /**
        * Update the GUI.
        */
       private class GTUpdateGui implements Runnable
       {
               @Override
               public void run()
               {
                       mazeGui.reflectStateChange();
               }
       }
       
       /**
        * Signal an error to the GUI.
        */
       private class GTSignalError implements Runnable
       {
               private String errorMessage;
               
               public GTSignalError(String msg)
               {
                       this.errorMessage = msg;
               }
               
               @Override
               public void run()
               {
                       mazeGui.displayErrorMessage( errorMessage, "Manager error" );
               }
       }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -