Line widget is used to draw straight line between two points.
Line class provides following constructors to define Line widget.
public Line()
public Line(double startX, double startY, double endX, double endY)
startX the horizontal coordinate of the start point of the line segment
startY the vertical coordinate of the start point of the line segment
endX the horizontal coordinate of the end point of the line segment
endY the vertical coordinate of the end point of the line segment
You can define a line in FXML like below.
<Line fx:id="line1" stroke="black" startX="10" startY="50" endX="350" endY="50" strokeWidth="10"/>
Find the below working application.
lineDemo.fxml
<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.LineController">
<Line fx:id="line1" stroke="black" startX="10" startY="50" endX="350" endY="50" strokeWidth="10"/>
<Line fx:id="line2" stroke="green" startX="10" startY="90" endX="350" endY="90" strokeWidth="10" strokeLineCap="round"/>
<Line fx:id="line3" stroke="red" startX="10" startY="130" endX="350" endY="130" strokeWidth="10"/>
</Group>
LineController.java
package com.sample.app.controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.shape.Line;
public class LineController implements Initializable {
@FXML
private Line line1;
@FXML
private Line line2;
@FXML
private Line line3;
@Override
public void initialize(URL location, ResourceBundle resources) {
line3.getStrokeDashArray().addAll(15d);
}
}
LineDemo.java
package com.sample.app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class LineDemo extends Application {
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(LineDemo.class.getResource("/lineDemo.fxml"));
Scene scene = new Scene(root, 400, 300, Color.WHITE);
primaryStage.setTitle("Line Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Run above application, you will see below screen.
Previous Next Home
No comments:
Post a Comment