How To Draw A Line In Javafx
This is our third tutorial in Java GUI, in this tutorial we are going to learn bout JavaFX Shape
Drawing Line. and then drawing dissimilar shapes, especially lines are so easy in JavaFX. When drawing
on the JavaFX scene graph, lines are rendered using the screen coordinate space (organization).
In geometry, a line is defined every bit a segment continued by two points in space, although it lacks
a width (thickness) and color value.
As well you tin can bank check Python GUI Development Tutorials in the below link.
ane: PyQt5 GUI Evolution Tutorials
2: TKinter GUI Evolution Tutorials
3: Pyside2 GUI Evolution Tutorials
4: Kivy GUI Evolution Tutorials
5: wxPython GUI Development Tutorials
You can draw line by creating the object of the Line class in the JavaFX. and yous need to requite
the start and end position for the x and y.
| Line redLine = new Line ( 10 , 10 , 200 , 10 ) ; |
Also you can fix the properties like this.
| line . setStartX ( 100 ) ; line . setStartY ( 10 ) ; line . setEndX ( ten ) ; line . setEndY ( 100 ) ; |
So now this is the complete code for Coffee GUI – JavaFX Shape Drawing Line.
| 1 two three 4 5 half dozen vii 8 9 10 11 12 thirteen 14 15 16 17 18 nineteen twenty 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 | import javafx . application . Application ; import javafx . scene . Group ; import javafx . scene . Scene ; import javafx . scene . pigment . Color ; import javafx . scene . shape . Line ; import javafx . scene . shape . StrokeLineCap ; import javafx . stage . Stage ; public class DrawingLines extends Application { public static void main ( Cord [ ] args ) { launch ( args ) ; } @Override public void start ( Phase stage ) { stage . setTitle ( "Drawing Lines In JavaFx" ) ; Group root = new Group ( ) ; Scene scene = new Scene ( root , 400 , 400 , Colour . Light-green ) ; // Common Line Line line = new Line ( 100 , 10 , 10 , 100 ) ; //line.setStartX(100); //line.setStartY(10); //line.setEndX(10); //line.setEndY(100); line . setStroke ( Colour . RED ) ; line . setStrokeWidth ( 10 ) ; line . setStrokeLineCap ( StrokeLineCap . ROUND ) ; // DashedLine line . getStrokeDashArray ( ) . addAll ( 10d , 5d , 15d , 20d ) ; line . setStrokeDashOffset ( 0 ) ; root . getChildren ( ) . add ( line ) ; stage . setScene ( scene ) ; stage . show ( ) ; } } |
Every JavaFX application should have a container, a container is like layout. as we
take created a Group container in the higher up code. but yous tin use different containers
from JavaFX.
| Grouping root = new Grouping ( ) ; |
Likewise for every JavaFX application we need to create a Scene object. in the scene we need
to add together our container with the width and height of the window, if you lot want to colorize your
window, you can do information technology in hither. y'all tin see that we have given 400 width and 400 height for the
window, likewise a greenish colour for the window.
| Scene scene = new Scene ( root , 400 , 400 , Color . Light-green ) ; |
This is for creating line , also we have used some properties for the line like given the stroke
colour for line, the width of stroke and the way of the stroke.
| Line line = new Line ( 100 , 10 , x , 100 ) ; line . setStroke ( Color . Scarlet ) ; line . setStrokeWidth ( 10 ) ; line . setStrokeLineCap ( StrokeLineCap . ROUND ) ; |
Using this code you lot tin can create dashed line.
| // DashedLine line . getStrokeDashArray ( ) . addAll ( 10d , 5d , 15d , 20d ) ; line . setStrokeDashOffset ( 0 ) ; |
When you create a widget in JavaFX, for example button,label,combobox or some other
widgets, you need to add that to your container, as nosotros have added our this push button in the
Group container.
| root . getChildren ( ) . add ( line ) ; |
At the stop y'all need to set your scene object to the stage of the window, and show the window.
| phase . setScene ( scene ) ; stage . testify ( ) ; |
Run the consummate code and this will be the effect.
Source: https://codeloop.org/java-gui-javafx-shape-drawing-line/
Posted by: fernandezexedger.blogspot.com

0 Response to "How To Draw A Line In Javafx"
Post a Comment