📄 mazegui.java
字号:
throw new WrongThreadException("MazeGui.updateBenchmark()");
lblBuildTotal.setText(Long.toString(buildTime));
lblBuildTickCount.setText(Long.toString(buildTick));
if(buildTick > 0)
lblBuildTimePerTick.setText(Long.toString(buildTime/buildTick));
lblSolveTotal.setText(Long.toString(solveTime));
lblSolveTickCount.setText(Long.toString(solveTick));
if(solveTick > 0)
lblSolveTimePerTick.setText(Long.toString(solveTime/solveTick));
}
/*
* ###### INITIALISATION FUNCTIONS ######
*/
/**
* Used to adjust the GUI to a maze object.
* Does not remove the old maze.
* @param maze
*/
private void insertMazePanel(Maze maze)
{
frame.setVisible(false);
this.maze = maze;
double xSizeMaze = maze.getSizeX();
double ySizeMaze = maze.getSizeY();
//Default size is the minimum size of the menu panel.
double xSizeMazePanel = MIN_Y_SIZE;
double ySizeMazePanel = MIN_Y_SIZE;
//A tall maze
if(xSizeMaze < ySizeMaze)
{
double ratio = (xSizeMaze / ySizeMaze);
xSizeMazePanel = xSizeMazePanel * ratio;
}
//A wide maze
else if(xSizeMaze > ySizeMaze)
{
double ratio = (xSizeMaze / ySizeMaze);
xSizeMazePanel = xSizeMazePanel * ratio;
//Enforce a max width
Dimension screenSize = frame.getToolkit().getScreenSize();
int maxWidth = (screenSize.width - MENU_PANEL_WIDTH) - 60;
xSizeMazePanel = Math.min(xSizeMazePanel, maxWidth);
}
//Frame size
int xSize = MENU_PANEL_WIDTH + 20 + (int)xSizeMazePanel;
int ySize = (int)ySizeMazePanel + STATUS_PANEL_WIDTH;
frame.setSize(xSize, ySize);
mazePanel = new MazeGraph(maze);
mazePanel.setDrawClrSquares( this.tbtnBuildColors.isSelected() );
mazePanel.setPreferredSize(new Dimension((int)xSizeMazePanel, (int)ySizeMazePanel));
frame.getContentPane().add( mazePanel, BorderLayout.CENTER );
//frame.pack()
mazePanel.invalidate();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* Generates the JFrame.
* Called from constructor.
*/
private void makeFrame()
{
frame = new JFrame();
frame.setTitle("AD2 Maze assignment");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
Container c = frame.getContentPane();
c.setLayout(new BorderLayout());
c.add( makeMenuPanel(), BorderLayout.WEST );
c.add( makeBenchmarkPanel(), BorderLayout.SOUTH);
}
/**
* Generates the menu JPanel.
* Called from constructor -> makeFrame().
*/
private JPanel makeMenuPanel()
{
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.PAGE_AXIS));
menuPanel.setPreferredSize(new Dimension(MENU_PANEL_WIDTH, 0));
menuPanel.add( makeGeneralOptionsPanel() );
menuPanel.add( makeBuildOptionsPanel() );
menuPanel.add( makePostBuildOptionsPanel() );
menuPanel.add( makeSolveOptionsPanel() );
//Enable Debug maze import/exporing
enableDebugImportExport(menuPanel);
return menuPanel;
}
private JPanel makeGeneralOptionsPanel()
{
JPanel generalOptionsPanel = new JPanel();
generalOptionsPanel.setBorder(BorderFactory.createTitledBorder("General Options"));
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
generalOptionsPanel.setLayout(gridbag);
//Don't stretch Components & Align to the left
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(INSET_SIZE, INSET_SIZE, 0, 0);
//Speed slider label
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
generalOptionsPanel.add(new JLabel("Algorithm delay (in ms):"), c);
c.gridy = 1;
delaySlider = new JSlider(SLIDER_MIN, SLIDER_MAX, Manager.DEFAULT_DELAY);
delaySlider.setMajorTickSpacing(20);
delaySlider.setMinorTickSpacing(5);
delaySlider.setPaintLabels(true);
delaySlider.setPaintTicks(true);
generalOptionsPanel.add(delaySlider, c);
c.gridwidth = 1;
//Maze breath
c.gridx = 0;
c.gridy = 2;
generalOptionsPanel.add(new JLabel("Maze breadth:"), c);
c.gridx = 1;
txtMazeBreadth = new JTextField( Integer.toString(Manager.DEFAULT_MAZE_SIZE_X) );
txtMazeBreadth.setColumns(8);
txtMazeBreadth.setHorizontalAlignment(JTextField.RIGHT);
generalOptionsPanel.add(txtMazeBreadth, c);
//Maze height
c.gridx = 0;
c.gridy = 3;
generalOptionsPanel.add(new JLabel("Maze height:"), c);
c.gridx = 1;
txtMazeHeight = new JTextField( Integer.toString(Manager.DEFAULT_MAZE_SIZE_Y) );
txtMazeHeight.setColumns(8);
txtMazeHeight.setHorizontalAlignment(JTextField.RIGHT);
generalOptionsPanel.add(txtMazeHeight, c);
//Update size
c.gridx = 0;
c.gridy = 4;
btnUpdateSize = new JButton("Update size");
btnUpdateSize.addActionListener(this);
generalOptionsPanel.add(btnUpdateSize, c);
//Build colors toggle button
c.gridx = 1;
tbtnBuildColors = new JToggleButton("Build colors");
tbtnBuildColors.setSelected(false);
tbtnBuildColors.addActionListener(this);
generalOptionsPanel.add(tbtnBuildColors, c);
return generalOptionsPanel;
}
private JPanel makeBuildOptionsPanel()
{
JPanel buildOptionsPanel = new JPanel();
buildOptionsPanel.setBorder(BorderFactory.createTitledBorder("Build Options"));
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
buildOptionsPanel.setLayout(gridbag);
//Don't stretch Components & Align to the left
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(INSET_SIZE, INSET_SIZE, 0, 0);
//Generate the radio buttons
rbtnBuildAlgorithms = new JRadioButton[3];
rbtnBuildAlgorithms[0] = new JRadioButton("Kruskal-based algorithm");
rbtnBuildAlgorithms[0].setSelected(true);
rbtnBuildAlgorithms[1] = new JRadioButton("Prim-based algorithm");
rbtnBuildAlgorithms[2] = new JRadioButton("Depth-First search");
//Put the buttons in a buttongroup
buildButtonGroup = new ButtonGroup();
for(JRadioButton rbutton : rbtnBuildAlgorithms)
{
buildButtonGroup.add(rbutton);
}
//Add the radio buttons to the panel
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
buildOptionsPanel.add(new JLabel("Algorithm:"), c);
//Add the radio buttons
for(JRadioButton rbutton : rbtnBuildAlgorithms)
{
c.gridx = 0;
c.gridy++;
buildOptionsPanel.add(rbutton, c);
}
c.gridwidth = 1;
//Build / Stop button
c.gridx = 0;
c.gridy++;
btnBuildAndPause = new JButton("Build");
btnBuildAndPause.addActionListener(this);
buildOptionsPanel.add(btnBuildAndPause, c);
//Reset button
c.gridx = 1;
btnResetAll = new JButton("Reset all");
btnResetAll.addActionListener(this);
buildOptionsPanel.add(btnResetAll, c);
return buildOptionsPanel;
}
private JPanel makePostBuildOptionsPanel()
{
JPanel postBuildOptionsPanel = new JPanel();
postBuildOptionsPanel.setBorder(BorderFactory.createTitledBorder("Post Build Options"));
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
postBuildOptionsPanel.setLayout(gridbag);
//Don't stretch Components & Align to the left
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(INSET_SIZE, INSET_SIZE, 0, 0);
//Walls to remove
c.gridx = 0;
c.gridy = 0;
postBuildOptionsPanel.add(new JLabel("Walls to try to remove:"), c);
c.gridx = 1;
txtRemoveWalls = new JTextField("0");
txtRemoveWalls.setColumns(8);
txtRemoveWalls.setHorizontalAlignment(JTextField.RIGHT);
postBuildOptionsPanel.add(txtRemoveWalls, c);
//Generate the checkbox
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 1;
cboxRandomizeStartEnd = new JCheckBox("Randomize start and end");
postBuildOptionsPanel.add(cboxRandomizeStartEnd, c);
c.gridwidth = 1;
//Process button
c.gridx = 0;
c.gridy = 2;
btnPostBuild = new JButton("Process");
btnPostBuild.setEnabled(false);
btnPostBuild.addActionListener(this);
postBuildOptionsPanel.add(btnPostBuild, c);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -