[英]Why is my 2D Maze not being painted with JComponent?
UPDATE: I Figured out the problem and wrote it out below.If you'd like to see the full compliable code here it is on GitHub: https://github.com/scohen40/cohen-mco364-fall-2018/tree/mazeGUI/src/main/java/cohen/maze 更新:我想出了问题并将其写在下面,如果您想在此处查看完整的可兼容代码,请访问GitHub: https : //github.com/scohen40/cohen-mco364-fall-2018/tree/ mazeGUI / src / main / java / cohen / maze
I have a 2D Array of Cells, each with 4 walls. 我有一个2D细胞单元阵列,每个单元有4面墙。 My generateMaze() class starts at a random point and digs out a maze. 我的generateMaze()类从随机点开始,并挖出一个迷宫。 That part works correctly, and when I print out the maze in the console everything is fine. 该部分正常工作,当我在控制台中打印出迷宫时,一切都很好。
My next goal is to have the maze painted out with a JComponent in a JPanel. 我的下一个目标是用JPanel中的JComponent绘制迷宫。 The problem is that all I'm getting is one thick-lined box in the top left corner. 问题是我得到的只是左上角的一个粗线框。 Here's the painting code: 这是绘画代码:
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);
}
}
}
}
}
You can see in the console the generated maze but in the JPanel it'sjust a box. 您可以在控制台中看到生成的迷宫,但是在JPanel中它只是一个盒子。
在绘制代码中,您需要将每个x和y坐标乘以'seventhHeight',否则就无法绘制正确的坐标了。
Building on the answer by Krzysztof Cichocki, realized that coordinates work differently from rows and tables. 在Krzysztof Cichocki的答案的基础上,意识到坐标的作用不同于行和表。 I switched the x's for y's and vice versa, after multiplying everything by seventhHeight. 在将所有内容乘以SeventhHeight之后,我将x换为y,反之亦然。 Also any additions that I did to any coordinate is always seventhHeight now, to make everything proportional. 而且,我对任何坐标所做的任何添加现在始终都是第七高度,以使所有内容成比例。 Here's the current code for the painting method: 这是绘画方法的当前代码:
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.