为什么我的 2D 迷宫没有使用 JComponent 绘制?
Why is my 2D Maze not being painted with JComponent?
更新:我找出了问题并将其写出来 below.If 你想在这里看到完整的兼容代码 GitHub:https://github.com/scohen40/cohen-mco364-fall-2018/tree/mazeGUI/src/main/java/cohen/maze
我有一个二维单元格阵列,每个单元格有 4 面墙。我的 generateMaze() class 从一个随机点开始并挖出一个迷宫。那部分工作正常,当我在控制台中打印出迷宫时,一切都很好。
我的下一个目标是使用 JPanel 中的 JComponent 绘制迷宫。问题是我得到的只是左上角的一个粗线框。
下面是绘画代码:
public class AnimatedMaze extends JComponent {
private Maze maze;
private int componentHeight;
private int componentWidth;
private int seventhHeight;
private int seventhWidth;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
componentHeight = this.getHeight();
componentWidth = this.getWidth();
seventhHeight = componentHeight/7;
seventhWidth = componentWidth/7;
maze = new Maze(7, 7);
g.setColor(Color.black);
paintMaze(g);
}
/**
* The paintMaze() method runs through the generated maze and paints the existing walls.
* @param g
*/
void paintMaze(Graphics g) {
for (int x = 0; x < maze.getHeight(); x++) {
System.out.println("|");
for (int y = 0; y < maze.getWidth(); y++) {
Cell current = maze.getMaze()[x][y];
if(current.isWestWall()) {
g.drawLine(x, y, x, y + seventhHeight);
}
if(current.isNorthWall()){
g.drawLine(x, y,x + seventhWidth, y);
}
if(current.isEastWall()) {
g.drawLine(x + seventhWidth, y, x+ seventhWidth, y + seventhHeight);
}
if(current.isSouthWall()) {
g.drawLine(x, y + seventhHeight, x + seventhWidth, y +seventhHeight);
}
}
}
}
}
您可以在控制台中看到生成的迷宫,但在 JPanel 中它只是一个框。
在您的绘画代码中,您需要将每个 x 和 y 坐标乘以 'seventhHeight',否则您将无法正确绘画坐标。
基于 Krzysztof Cichocki 的回答,意识到坐标与行和表的工作方式不同。在将所有内容乘以 seventhHeight 之后,我将 x 换成了 y,反之亦然。此外,我对任何坐标所做的任何添加现在始终是 seventhHeight,以使所有内容成比例。
这是绘画方法的当前代码:
void paintMaze(Graphics g) {
for (int x = 0; x < maze.getHeight(); x++) {
System.out.println("|");
for (int y = 0; y < maze.getWidth(); y++) {
Cell current = maze.getMaze()[x][y];
if(current.isWestWall()) {
g.setColor(Color.black);
g.drawLine((y+1)*seventhHeight, x*seventhHeight, (y+1)*seventhHeight, x*seventhHeight + seventhHeight);
}
if(current.isNorthWall()){
g.drawLine((y+1)*seventhHeight, x*seventhHeight,(y+1)*seventhHeight + seventhHeight, x*seventhHeight);
}
if(current.isEastWall()) {
g.drawLine((y+1)*seventhHeight + seventhHeight, x*seventhHeight, (y+1)*seventhHeight + seventhHeight, x*seventhHeight + seventhHeight);
}
if(current.isSouthWall()) {
g.drawLine((y+1)*seventhHeight, x*seventhHeight + seventhHeight, (y+1)*seventhHeight + seventhHeight, x*seventhHeight +seventhHeight);
}
}
}
}
现在是这样的:
更新:我找出了问题并将其写出来 below.If 你想在这里看到完整的兼容代码 GitHub:https://github.com/scohen40/cohen-mco364-fall-2018/tree/mazeGUI/src/main/java/cohen/maze
我有一个二维单元格阵列,每个单元格有 4 面墙。我的 generateMaze() class 从一个随机点开始并挖出一个迷宫。那部分工作正常,当我在控制台中打印出迷宫时,一切都很好。
我的下一个目标是使用 JPanel 中的 JComponent 绘制迷宫。问题是我得到的只是左上角的一个粗线框。
下面是绘画代码:
public class AnimatedMaze extends JComponent {
private Maze maze;
private int componentHeight;
private int componentWidth;
private int seventhHeight;
private int seventhWidth;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
componentHeight = this.getHeight();
componentWidth = this.getWidth();
seventhHeight = componentHeight/7;
seventhWidth = componentWidth/7;
maze = new Maze(7, 7);
g.setColor(Color.black);
paintMaze(g);
}
/**
* The paintMaze() method runs through the generated maze and paints the existing walls.
* @param g
*/
void paintMaze(Graphics g) {
for (int x = 0; x < maze.getHeight(); x++) {
System.out.println("|");
for (int y = 0; y < maze.getWidth(); y++) {
Cell current = maze.getMaze()[x][y];
if(current.isWestWall()) {
g.drawLine(x, y, x, y + seventhHeight);
}
if(current.isNorthWall()){
g.drawLine(x, y,x + seventhWidth, y);
}
if(current.isEastWall()) {
g.drawLine(x + seventhWidth, y, x+ seventhWidth, y + seventhHeight);
}
if(current.isSouthWall()) {
g.drawLine(x, y + seventhHeight, x + seventhWidth, y +seventhHeight);
}
}
}
}
}
您可以在控制台中看到生成的迷宫,但在 JPanel 中它只是一个框。
在您的绘画代码中,您需要将每个 x 和 y 坐标乘以 'seventhHeight',否则您将无法正确绘画坐标。
基于 Krzysztof Cichocki 的回答,意识到坐标与行和表的工作方式不同。在将所有内容乘以 seventhHeight 之后,我将 x 换成了 y,反之亦然。此外,我对任何坐标所做的任何添加现在始终是 seventhHeight,以使所有内容成比例。 这是绘画方法的当前代码:
void paintMaze(Graphics g) {
for (int x = 0; x < maze.getHeight(); x++) {
System.out.println("|");
for (int y = 0; y < maze.getWidth(); y++) {
Cell current = maze.getMaze()[x][y];
if(current.isWestWall()) {
g.setColor(Color.black);
g.drawLine((y+1)*seventhHeight, x*seventhHeight, (y+1)*seventhHeight, x*seventhHeight + seventhHeight);
}
if(current.isNorthWall()){
g.drawLine((y+1)*seventhHeight, x*seventhHeight,(y+1)*seventhHeight + seventhHeight, x*seventhHeight);
}
if(current.isEastWall()) {
g.drawLine((y+1)*seventhHeight + seventhHeight, x*seventhHeight, (y+1)*seventhHeight + seventhHeight, x*seventhHeight + seventhHeight);
}
if(current.isSouthWall()) {
g.drawLine((y+1)*seventhHeight, x*seventhHeight + seventhHeight, (y+1)*seventhHeight + seventhHeight, x*seventhHeight +seventhHeight);
}
}
}
}
现在是这样的: