I have created a Triangle using the Polygon object, along with onMousePressed, Dragged, Released events that allow the Triangle to be MouseDragged around the screen. Works fine in the JavaFX Application (for standalone Laptop) game I have created.
The problem is, I want the Triangle MouseEvents to fire only WITHIN the Triangle shape boundary, NOT outside the Triangle. Currently, Events seem to be firing anywhere within the Polygon-Triangle non-visible RECTANGLE layout bounds (ie the non-visible rectangle that embeds/surrounds the Triangle). This behavior allows the Triangle to be dragged with: 1) the cursor OUTSIDE of the Triangle & 2) the cursor ANYWHERE within the Polygon-Triangle non-visible rectangle bounds.
This behavior confuses the User by: 1) Unintentionally dragging the Triangle, 2) Blocking events of other game pieces (that are overlapped by the non-visible Triangle rectangle bounds.) making them unplayable, until Triangle is dragged out of the way.
I have accomplished this MouseEvent Restriction to Shape using the Ellipse object & the "setPickOnBounds(true); " property. Doesn't seem to work the same with Polygon objects.
I have checked on stackOverflow.com, & the closest article addressing this issue, that I could find, is the following, it does not address MouseEvent firing inside/outside Shapes.
StackOverflow.com: 1. How to smooth drag a JavaFX polygon? How to smooth drag a JavaFX polygon? Article did not address Shape Clickable area.
- Android Custom Shape Button
Android Custom Shape Button
Is 5 years old & Android specific, Is there a more efficient way with javaFX8 & Polygons?
Also, my Triangle dimensions are dynamic, claculated to match the size of embeded User formated LabelText.
So to use a Triangle image file, would required hundreds of Triangle files, for a best fit option.
Please HELP... Thanks. Michael
My code for creating Triangle:
public static class StrategySelfNote_Element extends Group {
public Polygon triangle;
public Label label;
public StrategySelfNote_Element(int id) {
// Triangle ********************************************************************************
triangle = new Polygon();
triangle.setId(Integer.toString(id)+";StrategySelfNote");
triangle.setFill(Color.AQUA);
triangle.setCursor(Cursor.HAND);
triangle.setVisible(false);
triangle.setLayoutX(0);
triangle.setLayoutY(0);
triangle.setCache(true);
// NOTE: This allowed Ellipse to be Dragged by MouseEvent, else only by Label (not sure if required w/Triangle???)
triangle.setPickOnBounds(true);
// Label **********************************************************************************
label = new Label();
label.setId(Integer.toString(id)+";StrategySelfNote");
label.setFont(Font.font ("Calibri", FontWeight.BOLD, IntroQuestions.sizeFont_strategySelfNotes));//28
label.setWrapText(true);
label.setMaxWidth(MAX_WIDTH_STRATEGY_SELF_NOTE); // To Prevent HUGE TextRectangle
label.setMaxHeight(MAX_HEIGHT_STRATEGY_SELF_NOTE);
label.setAlignment(Pos.TOP_LEFT);
label.setStyle("-fx-background-color: aqua;"); // -fx-background-color: AQUA; // light-yellow
//-------------
label.setCursor(Cursor.HAND);
label.setVisible(false);
label.setLayoutX(0); //debug: TRY 0 like before---to FIX When Dragging to 3/4 Quad, FREEZES & PANs scrlPane !!!
label.setLayoutY(0);
label.setCache(true);
//-------------
getChildren().addAll(triangle, label);
//------------- paneElement
this.setLayoutX(0.0); //10-26
this.setLayoutY(0.0);
this.setId(Integer.toString(id)+";StrategySelfNote");
this.setPickOnBounds(true); // NOTE: This allows Polygon to be Dragged by MouseEvent, else only by Label
this.setVisible(false);
this.setCache(true);
//-----
this.setOnMousePressed(boardStrategySelfNote_OnMousePressedEventHandler);
this.setOnMouseDragged(boardStrategySelfNote_OnMouseDraggedEventHandler);
this.setOnMouseReleased(boardStrategySelfNote_OnMouseReleasedEventHandler);
this.addEventFilter(InputEvent.ANY, boardStrategySelfNote_OnMouseFilterEventHandler);
}
}
. . . This is from another function, to Calculate & Set Triangle:
llStrategySelfNotes.get(strategySelfNote_id-1).groupStrategySelfNote_Element.triangle.getPoints().addAll(
triBottomLeftX, triBottomLeftY,
triBottomRightX, triBottomRightY,
triTopX, triTopY);