22.10 Drawing on a Canvas

So far, you’ve displayed and manipulated JavaFX two-dimensional shape objects that reside in the scene graph. In this section, we demonstrate similar drawing capabilities using the javafx.scene.canvas package, which contains two classes:

  • Class Canvas is a subclass of Node in which you can draw graphics.

  • Class GraphicsContext performs the drawing operations on a Canvas.

As you’ll see, a GraphicsContext object enables you to specify the same drawing characteristics that you’ve previously used on Shape objects. However, with a GraphicsContext, you must set these characteristics and draw the shapes programmatically. To demonstrate various Canvas capabilities, Fig. 22.15 re-implements Section 22.3’s BasicShapes example. ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.