The JavaFX application framework was designed from the ground up to handle graphical elements. JavaFX provides inherent support for basic geometrical shapes as a first-class API. This recipe shows how to programmatically draw lines, rectangles, circles, and ellipses using the Shape
APIs found in the javafx.scene.shape
package.
Before you can draw your shapes using JavaFX, you must know how to create a basic JavaFX application and know how to add content to the application's scene. To refresh your memory, see the first recipe of this chapter, Building a JavaFX application.
The following code snippet shows how to draw a line, a rectangle, a circle, and an ellipse on the screen. You can get the full listing of the code from ch002/source-code/src/shapes/SimpleShapes.fx
.
def spacer = 100; Stage { title: "Simple Shapes!" width: 500 height: 300 x: 100; y: 200 scene: Scene { content: [ Line { startX: 10 startY: 10 endX: 10 + spacer endY: 10 + spacer...