📄 manager.java
字号:
/** ver: 0.1, date 19-12-2007 by Jeroen W
* - autogenerated, no changes made
* ver: 0.2, date 02-01-2008 by Marcel Hekman
* - Manager states (and other constants).
* - GuiThreads.
* - resizeMaze().
* - clear functions.
* - start, pause and continue build functions
* - start, pause and continue solve functions
* ver: 0.3, date 02-01-2008 by Jeroen S
* Added maze to build's initialise
* ver: 0.4, date 03-01-2008 by Marcel Hekman
* - set state to PAUSED after an algorithm exception.
* ver: 0.5, date 04-01-2008 by Jeroen S
* Benchmarking
*/
package mazeAssignment;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;
/**
*
* @author: jeroen, jeroen, marcel en teun
*/
public class Manager
{
//Defaults
public static final int DEFAULT_MAZE_SIZE_X = 40;
public static final int DEFAULT_MAZE_SIZE_Y = 40;
public static final int DEFAULT_DELAY = 10;
//Manager states
public static final int STATE_CLEAR = 0;
public static final int STATE_BUILDING = 1;
public static final int STATE_BUILD_PAUSED = 2;
public static final int STATE_BUILD_FINISHED = 3;
public static final int STATE_SOLVING = 4;
public static final int STATE_SOLVE_PAUSED = 5;
public static final int STATE_SOLVED = 6;
//The volatile keyword here is important because it is accessed by multiple threads.
private volatile int state;
private Maze maze;
private MazeGui mazeGui;
private Build buildAlgorithm;
private Solve solveAlgorithm;
private int algorithmDelay;
// Timer values
// Timer values are in milliseconds
private long buildTime;
private long buildTickCount;
private long solveTime;
private long solveTickCount;
public Manager()
{
buildAlgorithm = null;
solveAlgorithm = null;
buildTime = 0;
buildTickCount = 0;
solveTime = 0;
solveTickCount = 0;
maze = new SingleArrayMaze(DEFAULT_MAZE_SIZE_X, DEFAULT_MAZE_SIZE_Y);
state = STATE_CLEAR;
algorithmDelay = DEFAULT_DELAY;
SwingUtilities.invokeLater( new GTInitGui() );
/*
try
{
SwingUtilities.invokeAndWait( new GTInitGui() );
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
*/
}
/**
* Used in debugging to load prebuild mazes.
*/
public Manager(Maze m)
{
buildAlgorithm = null;
solveAlgorithm = null;
buildTime = 0;
buildTickCount = 0;
solveTime = 0;
solveTickCount = 0;
maze = m;
state = STATE_BUILD_FINISHED;
algorithmDelay = DEFAULT_DELAY;
try
{
SwingUtilities.invokeAndWait( new GTInitGui() );
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
/**
* Called by MazeGui to start an IntermediaryStep.
*/
public void intermediaryStep(boolean doRandomExit, int wallsToKnock)
{
IntermediaryStep postBuildStep = new IntermediaryStep( this.maze );
postBuildStep.updateMaze(doRandomExit, wallsToKnock);
}
/**
* Called by MazeGui to update the maze size.
* Sets state to STATE_CLEAR.
* @param x The width of the new maze.
* @param y The height of the new maze.
* @return The new maze.
*/
public Maze resizeMaze(int x, int y)
{
buildAlgorithm = null;
solveAlgorithm = null;
state = STATE_CLEAR;
maze = new SingleArrayMaze(x, y);
return maze;
}
/**
* Called by MazeGui when importing a maze.
* Sets state to STATE_BUILD_FINISHED.
* @param m The new maze.
*/
public void setMaze(Maze m)
{
if(!this.isBusy())
{
buildAlgorithm = null;
solveAlgorithm = null;
state = STATE_BUILD_FINISHED;
this.maze = m;
//Make sure the state change is noticed by the gui
mazeGui.reflectStateChange();
}
}
/**
* Starts a build operation, calls the initialize function of the algorithm.
* Not called from the event thread.
* @param algorithm The build algorithm object.
* @param delay The algorithm delay in milliseconds.
*/
public void startBuild(Build algorithm, int delay)
{
if(state != STATE_CLEAR)
{
clearAll();
}
buildAlgorithm = algorithm;
algorithmDelay = delay;
state = STATE_BUILDING;
buildTime = 0;
buildTickCount = 0;
updateGui();
//try
//{
buildAlgorithm.initialise(maze);
//}
//catch (BuildException be)
//{
// this.state = STATE_BUILD_PAUSED;
// updateGui();
// this.signalError( "startBuild: BuildException: " + be.getMessage() );
// return;
//}
continueBuild(delay);
}
/**
* Continues a build operation after initialization or pause.
* Not called from the event thread.
*/
public void continueBuild(int delay)
{
algorithmDelay = delay;
if(buildAlgorithm == null)
{
this.state = STATE_BUILD_PAUSED;
this.signalError( "continueBuild: buildAlgorithm == null." );
updateGui();
return;
}
state = STATE_BUILDING;
updateGui();
while(this.state == STATE_BUILDING && ! buildAlgorithm.isDone())
{
try
{
long startTime = System.currentTimeMillis();
buildAlgorithm.tick();
buildTime += System.currentTimeMillis() - startTime;
buildTickCount++;
if(algorithmDelay != 0)
{
mazeGui.repaintMazeGraph();
SwingUtilities.invokeLater(new GTUpdateBenchmark());
Thread.sleep( algorithmDelay );
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
//catch (BuildException be)
//{
// this.state = STATE_BUILD_PAUSED;
// updateGui();
// this.signalError( "continueBuild: BuildException: " + be.getMessage() );
// return;
//}
}
//Check if we're finished and not paused
if(buildAlgorithm.isDone() && this.state != STATE_BUILD_PAUSED)
{
this.state = STATE_BUILD_FINISHED;
buildAlgorithm = null;
updateGui();
}
mazeGui.repaintMazeGraph();
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -