How to draw a grid out of grid squares seamlessly in JavaFX

113 views Asked by At

I'm been writing maze algorithms, and want to draw the mazes generated using JavaFX.

To begin with, I'm attempting to draw a simple grid - but made up of smaller shapes so that I'll later be able to change the shape of the grid into that of a maze.

I'm using little upper left corner shapes (like ┏) and a GridPane, but this is leading to small discontinuities between the cells. (screenshot and code below). How can I wedge these shapes together seamlessly? Am I barking up the wrong tree with the Gridpane idea?

attempted grid drawing so far

My code, below, is actually written in Scala; I'm using ScalaFX, but finding help for ScalaFX online is a nightmare so I've been going solely off JavaFX docs - they are pretty much the same thing as far as I've gathered.

val lineLength: Int = 30

def makeClosedCell(length: Int = lineLength): Shape = {
    val wallN = Line(0,0,length,0)
    val wallW = Line(0,0,0,length)
    val closedCell: Shape = Shape.union(wallN, wallW)
    return closedCell
}

def makeOpenW(length: Int = lineLength): Shape = Line(0,0,length,0)

def makeOpenN(length: Int = lineLength): Shape = Line(0,0,0,length)

def initialiseGrid(r: GridPane, sizex: Int, sizey: Int): Unit = {
    for (i <- 0 until sizex) {
        val colConst = new ColumnConstraints()
        colConst.setPercentWidth(100.0 / sizex)
        r.getColumnConstraints().add(colConst)
    }
    for (i <- 0 until sizey) {
        val rowConst = new RowConstraints()
        rowConst.setPercentHeight(100.0 / sizey)
        r.getRowConstraints().add(rowConst)     
    }
    for(j <- sizey-1 to 0 by -1){
        for(i <- 0 until sizex){
            r.add(makeClosedCell(),i,j)
        }
        r.add(makeOpenN(),sizex,j)
    }
    for(i <- 0 until sizex){
        r.add(makeOpenW(),i,sizey)
    }
}
1

There are 1 answers

0
user18898152 On

Just found a solution. I've found this can be solved by fixing the exact column and row widths and heights to the same value as the line lengths, like so:

val rowConst = new RowConstraints()
// remove: rowConst.setPercentHeight(100.0 / sizey)
rowConst.setMinHeight(length)
rowConst.setMaxHeight(length)
r.getRowConstraints().add(rowConst) 

With length passed into the initialisation function, of course.