We are still cooking the magic in the way!
Basic Shapes & the Canvas
Basic Shapes & the Canvas
JavaFX ships with two distinct drawing systems. The first is the shape node system: declarative objects like Rectangle, Circle, and Line that live as first-class nodes in the scene graph, respond to CSS, receive mouse events, and can be animated with the same transition API you use for any other node. The second is the Canvas: an immediate-mode pixel surface that works like the HTML5 canvas — you obtain a GraphicsContext and issue drawing commands imperatively. Both approaches have their place, and understanding which to reach for is a key skill.
Shape Nodes: Declarative Vector Graphics
All shape classes extend javafx.scene.shape.Shape, which in turn extends Node. That means every shape automatically participates in layout, hit-testing, and animations. The most commonly used shapes are:
Rectangle— defined byx,y,width,height, plus optionalarcWidth/arcHeightfor rounded corners.Circle— defined by centre coordinatescenterX,centerYandradius.Ellipse— like Circle but with independentradiusXandradiusY.Line— connects two points:(startX, startY)to(endX, endY).Polygon/Polyline— arbitrary sequences of x/y coordinate pairs.Path— the most powerful shape: a sequence ofPathElementobjects (MoveTo, LineTo, CubicCurveTo, ArcTo, etc.) that can describe any outline.
Here is a small scene that places three shapes in a Pane and applies fills and strokes:
setFill() paints the inside of a shape; setStroke() paints its outline. Both accept any Paint — a Color, a LinearGradient, or a RadialGradient. Setting fill to Color.TRANSPARENT is the correct way to create an outline-only shape.
Working with LinearGradient
JavaFX paints are first-class objects. A LinearGradient sweeps between colour stops along a direction vector. The constructor arguments are: start-X, start-Y, end-X, end-Y (all in the range 0–1 when proportional=true), cycleMethod, proportional flag, and a list of Stop objects.
Stroke Properties
The Shape class exposes fine-grained stroke control that mirrors the SVG/canvas model:
setStrokeLineCap(StrokeLineCap.ROUND)— rounded ends on open paths and lines.setStrokeLineJoin(StrokeLineJoin.MITER)— how corners are joined.setStrokeDashArray(Double...)— alternating dash and gap lengths.setStrokeDashOffset(double)— phase shift into the dash pattern, useful for animated "marching ants".
The Canvas API: Immediate-Mode Drawing
Canvas is a Node with a fixed pixel buffer. You do not add child shapes to it; instead you issue drawing commands on its GraphicsContext. Changes are immediate and permanent — there is no retained shape hierarchy to query or animate. That makes Canvas the right choice for dense visualisations (charts, game sprites, heatmaps) where managing thousands of individual scene-graph nodes would be too expensive.
GraphicsContext maintains a current drawing state (fill paint, stroke paint, line width, font, transform, clip). Call setters before each draw call that needs them. Use gc.save() / gc.restore() to push and pop the entire state stack, which is the cleanest way to apply a temporary transform or clip without polluting subsequent draw calls.
Clearing and Redrawing the Canvas
Because the canvas is a pixel buffer, updating it means repainting. The standard pattern is to clear the dirty region and repaint everything:
Saving and Restoring GraphicsContext State
Choosing Between Shape Nodes and Canvas
- Use shape nodes when you need click/hover events on individual shapes, CSS styling, or smooth property animations via the transitions API.
- Use Canvas when you are drawing hundreds of items per frame, implementing a custom chart or a game loop, or need precise pixel-level control.
- Mix both freely: a
Canvasis just aNode— you can place it in the samePaneas shape nodes, layering a pixel-drawn background behind interactive vector controls.
Summary
JavaFX gives you two powerful drawing tools. Shape nodes — Rectangle, Circle, Path, and their siblings — live in the scene graph, respond to CSS and events, and are the right default for interactive UI elements. The Canvas with its GraphicsContext is an immediate-mode surface that excels for dense, high-frequency rendering. Master both, understand the state-machine model of GraphicsContext, and use save()/restore() to keep complex drawing routines clean and composable.