0

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.

Turtle
  • 33
  • 1
  • 6
  • You may be looking for `pickOnBounds` - see [this question](https://stackoverflow.com/questions/15525001/javafx-how-to-make-a-node-partially-mouse-transparent) – Itai Jun 26 '17 at 15:49
  • @sillyfly that sounds like what I want, but unfortunately doesn't work. It still clicks outside the bounds of the shape when `pickOnBounds = true` – Turtle Jun 26 '17 at 16:33
  • I am wondering why you are doing all this at all. Why do you set your polygonal geometry as the shape of a HBox? What do you want to achieve with that. To me your whole approach looks more than strange and I suspect you won't get very far with it. – mipa Jun 26 '17 at 17:59
  • @mipa I agree it seems strange to me as well, but this is the only way I've seen that is able to accomplish what I want. I've edited my question with a bit more detail about what I am trying to accomplish. In short, I want to be able to accurately click on irregular shapes while maintaining the benefits of using a pane. Is there a better method I am overlooking? – Turtle Jun 26 '17 at 20:18

1 Answers1

0

I could not figure out how to get the Hbox to accurately change it's on click geometric bounds to my polygon shape. However, a work around is simply to change the hbox to a Stackpane and add the polygon as a child node. Then set the polygon's onMouseClick instead. This way I can still add children relative to the bounding box at least.

Turtle
  • 33
  • 1
  • 6