Java GUI – JavaFX Shape Drawing Line

This is our third tutorial in Java GUI, in this tutorial we are going to learn bout JavaFX Shape

Drawing Line. so drawing different shapes, especially lines are so easy in JavaFX.  When drawing  

on the JavaFX scene graph, lines are rendered using  the screen coordinate space (system).

In geometry, a line is defined as a segment connected by two points  in space, although it lacks

a width (thickness) and color value.

 

 

 

Also you can check Python GUI Development Tutorials in the below link.

1: PyQt5 GUI Development Tutorials

2: TKinter GUI Development Tutorials

3: Pyside2 GUI Development Tutorials 

4: Kivy GUI Development Tutorials

5: wxPython GUI Development Tutorials

 

 

 

You can draw line by creating the object of the Line class in the JavaFX. and you need to give 

the start and end position for the x and y.

 

 

 

 

Also you can set the properties like this.

 

 

 

 

 

So now this is the complete code for Java GUI – JavaFX Shape Drawing Line.

 

 

 

 

Every JavaFX application should have a container, a container is like layout. as we

have created a Group container in the above code. but you can use different containers

from JavaFX.

 

 

 

 

Also for every JavaFX application we need to create a Scene object. in the scene we need

to add our container with the width and height of the window, if you want to colorize your

window, you can do it in here. you can see that we have given 400 width and 400 height for the

window, also a green color for the window.

 

 

 

 

This is for creating line , also we have used some properties for the line like given the stroke

color for line, the width of stroke and the style of the stroke.

 

 

 

 

Using this code you can create dashed line.

 

 

 

 

 

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 we have added our this button in the

Group container.

 

 

 

 

At the end you need to set your scene object to the stage of the window, and show the window.

 

 

 

 

 

Run the complete code and this will be the result.

Java GUI - JavaFX Shape Drawing Line
Java GUI – JavaFX Shape Drawing Line

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Comments are closed.

Codeloop
Share via
Copy link
Powered by Social Snap
×