📄 mazegraph.java
字号:
/** ver: 0.1, date 19-12-2007 by Jeroen W
* 23-12-2007 - Jeroen S - Added some initial methods
* 27-12-2007 - Jeroen S - Further updates to paintComponent
* ver 0.4, dat 2 jan 2008 by Jeroen S
* Activate graphics; minor related fixes
* ver 0.5, dat 2 jan 2008 by Jeroen S
* Solve state colours
* ver 0.6, dat 2 jan 2008 by Jeroen S
* Coloured squares made optional
* ver 0.7, dat 03-01-2008 by Jeroen S
* Coloured squares ignoring only applies to building, not solving
* Adjusted several colour assignments
*/
package mazeAssignment;
import java.awt.*;
import javax.swing.*;
/**
* MazeGraph, responsible for giving a visual representation
* of the maze.
* Extends from JPanel. To use, initialize with the Maze and
* place on an appropriate window. Call repaint() after
* every update of the Maze to refresh this on the screen.
*
* @author: jeroen, jeroen, marcel en teun
*/
public class MazeGraph extends JPanel {
public static final int ELEMENT_WIDTH = 16; //Feel free to adjust.
private Maze maze;
private boolean drawColouredSquares;
static final long serialVersionUID = 1;
public MazeGraph(Maze maze)
{
this.maze = maze;
drawColouredSquares = true;
}
public void paintComponent(Graphics g)
{
//Clears the panel
super.paintComponent(g);
//Retrieve sizes for ease of access later
int mazesizex = maze.getSizeX();
int mazesizey = maze.getSizeY();
int mywidth = this.getWidth()-2;
int myheight = this.getHeight()-2;
for(int x = 0; x < mazesizex; x++)
{
for(int y = 0; y < mazesizey; y++)
{
MazeElement element = maze.getElement(x, y);
// Drawing coloured squares on the maze.
// This can be distracting, and thus optional
if(drawColouredSquares || element.getSolveState() != MazeElement.SOLVE_UNVISITED)
{
// Set colour based on status
// (Colours chosen arbitrarily)
if(element.getSolveState() == MazeElement.SOLVE_UNVISITED)
{
switch(element.getBuildState())
{
case 0:
g.setColor(Color.GRAY);
break;
case 1:
g.setColor(Color.YELLOW);
break;
case 2:
g.setColor(Color.ORANGE);
break;
case 3:
g.setColor(Color.CYAN);
break;
case 4:
g.setColor(Color.MAGENTA);
break;
}
} else {
switch(element.getSolveState())
{
case MazeElement.SOLVE_UNVISITED:
// Not reached (handled as default case)
g.setColor(Color.WHITE);
break;
case MazeElement.SOLVE_START:
g.setColor(Color.RED);
break;
case MazeElement.SOLVE_END:
g.setColor(Color.GREEN);
break;
case MazeElement.SOLVE_VISITED:
g.setColor(Color.BLACK);
break;
case MazeElement.SOLVE_INVALID:
g.setColor(Color.BLUE);
}
}
// Lack of parentheses intentional, to
// force higher accuracy
// Size (arg 3 + 4) includes a 1 pixel border
g.fillRect(x * mywidth / mazesizex,
y * myheight / mazesizey,
mywidth / mazesizex,
myheight / mazesizey);
}
//Draw walls
g.setColor(Color.BLACK);
if(element.getNorth())
{
g.drawLine(x*mywidth / mazesizex,
y*myheight / mazesizey,
(x+1)*mywidth/mazesizex,
y*myheight / mazesizey);
}
if(element.getWest())
{
g.drawLine(x*mywidth / mazesizex,
y*myheight / mazesizey,
x*mywidth/mazesizex,
(y+1)*myheight / mazesizey);
}
if(element.getSouth())
{
g.drawLine(x*mywidth / mazesizex,
(y+1)*myheight / mazesizey,
(x+1)*mywidth/mazesizex,
(y+1)*myheight / mazesizey);
}
if(element.getEast())
{
g.drawLine((x+1)*mywidth / mazesizex,
y*myheight / mazesizey,
(x+1)*mywidth/mazesizex,
(y+1)*myheight / mazesizey);
}
}
}
}
public boolean getDrawClrSquares()
{
return drawColouredSquares;
}
public void setDrawClrSquares(boolean draw)
{
drawColouredSquares = draw;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -