Search code examples
javafxscalafxhbox

Setting the shape property of an Hbox - JavaFX


I'm trying to set the shape property of an hbox to a polygon. The following code is within a class that extends Hbox with a constructor that requires a list of points called origPoints.

val polygon = new javafx.scene.shape.Polygon()
origPoints.foreach{case (x,y) => polygon.getPoints.addAll(x,y)}
setShape(polygon)
setStyle("-fx-border-color: red")

Sorry if the syntax is a little different. I am using ScalaFX, but I don't believe that is causing any issues in this case.

For more information: I am making an intractable map of the United States. I have the coordinates mapped out for each state. I would like to have the advantages of using an hbox, such as being able to add children such as text.


Solution

  • As Sedrick Jefferson pointed out, the minWidth and minHeight need to be set or else the hbox prefers a 0 height/width. I wanted my countries to be able to scale when the window was resized so I binded their minHeight/minWidth properties to a SimpleDoubleProperty that kept track of the scale ratio of the background in order to scale the height/width accordingly.

    origPoints.foreach{case (x,y) => polygon.getPoints.addAll(x,y)}
    shape = polygon
    styleClass.setAll("country")
    minHeight.bind(yScale.multiply(origHeight))
    minWidth.bind(xScale.multiply(origWidth))