Java Reference
In-Depth Information
12 Scene scene = new Scene( new LinePane(), 200 , 200 );
13 primaryStage.setTitle( "ShowLine" ); // Set the stage title
14 primaryStage.setScene(scene); // Place the scene in the stage
15 primaryStage.show(); // Display the stage
16 }
17 }
18
19 class LinePane extends Pane {
20 public LinePane() {
21 Line line1 = new Line( 10 , 10 , 10 , 10 );
22 line1.endXProperty().bind(widthProperty().subtract( 10 ));
23 line1.endYProperty().bind(heightProperty().subtract( 10 ));
24 line1.setStrokeWidth( 5 );
25 line1.setStroke(Color.GREEN);
26
create a pane in scene
define a custom pane
create a line
set stroke width
set stroke
add line to pane
getChildren().add(line1);
27
28 Line line2 = new Line( 10 , 10 , 10 , 10 );
29 line2.startXProperty().bind(widthProperty().subtract( 10 ));
30 line2.endYProperty().bind(heightProperty().subtract( 10 ));
31
create a line
line2.setStrokeWidth( 5 );
32
line2.setStroke(Color.GREEN);
33
getChildren().add(line2);
add line to pane
34 }
35 }
The getter and setter methods for property values
and a getter for property itself are provided in the class,
but omitted in the UML diagram for brevity.
javafx.scene.shape.Line
-startX: DoubleProperty
-startY: DoubleProperty
-endX: DoubleProperty
-endY: DoubleProperty
The x-coordinate of the start point.
The y-coordinate of the start point.
The x-coordinate of the end point.
The y-coordinate of the end point.
+Line()
+Line(startX: double, startY:
double, endX: double, endY:
double)
Creates an empty Line .
Creates a Line with the specified starting and ending points.
F IGURE 14.28
The Line class defines a line.
(0, 0)
(getWidth(), 0)
(startX, startY)
(endX, endY)
(0, getHeight())
(getWidth(), getHeight())
(a) Line(startX, startY, endX, endY)
(b) Two lines are displayed
across the pane.
F IGURE 14.29
A Line object is created to display a line.
The program defines a custom pane class named LinePane (line 19). The custom pane
class creates two lines and binds the starting and ending points of the line with the width and
height of the pane (lines 22-23, 29-30) so that the two points of the lines are changed as the
pane is resized.
 
 
Search WWH ::




Custom Search