Search code examples
javajavafxfxml

Drawing lines in JavaFX with FXML


I'm trying to draw some Lines on the screen for JavaFX project. I've found documentations about how to do this with java code, but I need to do it in the FXML file. I can't find any documentation on the attributes (e.g. x and y positioning) for creating a < Line /> in FXML.


Solution

  • startX, startY - starting point coordinates, endX, endY - endpoint coordinates; stroke - line color; strokeLineCap (BUTT, SQUARE, ROUND); strokeWidth - width line:

    <Line startX="41.0" startY="56.0" endX="143.0" endY="56.0" 
        layoutX="67.0" layoutY="154.0" opacity="0.5" 
        stroke="#346e3c" strokeLineCap="BUTT" strokeWidth="4.0"/>
    

    Lines (length = 100) indented from the beginning of the layout (x = 50, y = 50):

    <!-- horizontal line -->
    <Line layoutX="50.0" layoutY="50.0" endX="100" />
    <!-- vertical line -->
    <Line layoutX="50.0" layoutY="50.0" endY="100" />
    

    Cursor and effect attributes:

    <Line ...>
        <cursor>
            <Cursor fx:constant="HAND" />
            <!-- other, such as: DEFAULT, V_RESIZE, H_RESIZE, MOVE -->
        </cursor>
        <effect>
            <GaussianBlur /> 
            <!-- other, such as: Blend, Bloom, Glow, Reflection, Shadow -->
        </effect>
    </Line>