I am making an interactive map of the United States. I have mapped out the coordinates of each state into a polygon shape. If I add an onMouseClick to the polygon shape, everything works fine, however I would like to be able to easily add children such as text or a button. Therefore, I have set the shape of the hbox to the appropriate polygon shape. I set the border in order to verify the shape of the hbox is correct.
val polygon = new Polygon(new javafx.scene.shape.Polygon())
origPoints.foreach{case (x,y) => polygon.getPoints.addAll(x,y)}
shape = polygon
styleClass.setAll("country")
This works fine and there is a red border around the hbox. However, when I set the onMouseClicked event like so:
this.setOnMouseClicked(new EventHandler[MouseEvent] {
override def handle(event: MouseEvent): Unit = {//action here}
})
It performs the action when clicked, but it is not bound by the shape any longer. It will execute the action as long as I click within a rectangle determined by the max width and max height of the shape, rather than the defined polygon shape.
Note: I am using scalafx, so the syntax may be a little different but I do not believe that is affecting any code.