Canvas Draw 5 0 2 – Universal Graphics Tool Set

last modified July 16, 2020

Canvas is an image that can be drawn on using a set of graphics commands provided by a GraphicsContext. It is a high-level toolfor doing painting.

' Your Universal Graphics Tool ' Visualize your workflow with a diverse content creation tool able to combine adaptable text, image, object, and effect elements in a single document. Canvas Draw 5 services the creation of mechanical documentation, including assembly, maintenance, and repair instructions. The following example produces three Rectangle elements, each element 100 pixels. The first Rectangle is red, and its upper-left (x, y) position is specified as (0, 0).The second Rectangle is green, and its upper-left position is (100, 100), which is just below, and to the right of, the first square. The third Rectangle is blue, and its upper-left position is (50, 50). Fabric also has SVG-to-canvas (and canvas-to-SVG) parser Using Fabric.js, you can create and populate objects on canvas; objects like simple geometrical shapes or complex shapes consisting of hundreds or thousands of simple paths.

GraphicsContext is used to issue draw calls to aCanvas using a buffer.

JavaFX simple lines

In the first example, we draw simple lines. A line is a basic graphicsprimitive. Two coordinates are needed to form a line.

The example draws three lines which form a rectangle.

A Canvas is constructed with a width and height that specifies thesize of the image into which the canvas drawing commands are rendered. Alldrawing operations are clipped to the bounds of that image.

The getGraphicsContext2D() returns a GraphicsContextassociated with the canvas.

The drawing is delegated to the drawLines() method.

A line primitive is represented as a path element. The beginPath() methodstarts a new path.

The moveTo() method moves the starting point of the current pathto the specified coordinate.

The lineTo() methods add line segments to the current path.

The stroke() method strokes the path with the current stroke paint.

JavaFX stroke and fill

A stroke is used to draw outlines of shapes. A fill is used to paintinteriors of shapes.

com/zetcode/StrokeFillEx.java

The example draws an outline of a circle and fills an interior of a circle.

The setStroke() method sets the current stroke paint attribute.The default colour is black. The attribute is used by the stroke methodsof the GraphicsContext.

The setLineWidth() sets the current line width.

The strokeOval() method strokes an oval using the current stroke paint.

The setFill() method sets the current fill paint attribute.The default colour is black. The attribute is used by the fill methods of the GraphicsContext.

The fillOval() fills an oval using the current fill paint.

JavaFX colours

The Color class is used to work with colours in JavaFX.There are many predefined colours. Custom colour values canbe created using the RGB or HSB colour model.

The example draws six circles using predefined colour values.

A predefined Color.CADETBLUE colour is set to be the current fill.

An interior of a circle object is filled with the current fill attribute.

JavaFx gradients

In computer graphics, a gradient is a smooth blending of shades from lightto dark or from one colour to another. In drawing and paint programs,gradients are used to create colourful backgrounds and special effects aswell as to simulate lights and shadows. There are two types of gradients:linear gradients and radial gradients.

Linear gradient

A linear gradient is a smooth blending of colours along a line. It is defined by the LinearGradient class.

com/zetcode/LinearGradientEx.java

In the example, we fill a rectangular shape with a linear gradient.

We define stop points for the gradient. They specify how to distribute the colors along the gradient.

The first four parameters specify the line along which the gradient is painted.The fifth parameter is the proportional parameter, which sets whether thecoordinates are proportional to the shape which this gradient fills. The sixthparameter sets the cycle method of the gradient. The last parameter takes thestop points.

Radial gradient

A radial gradient is a smooth blending of colours or shades of coloursbetween a circle and a focal point. A radial gradient is defined by theRadialGradient class.

The example fills a circle with a radial gradient.

We define stop values for the gradient.

Canvas Draw 5 0 2 – Universal Graphics Tool Settings

A radial gradient is created. The first two parameters are the focus angle andfocus distance. The next two parameters are the x and y coordinates of the center point of the gradient's circle. The fifth parameter is the radius of the circledefining the extents of the color gradient.

JavaFX shapes

Rectangles, ovals, arcs are basic geometric shapes. The GraphicsContextcontains methods for drawing outlines and interiors of these shapes.

com/zetcode/ShapesEx.java

The example paints six different shapes using the graphics context's fill methods.

The shapes are painted in gray colour.

The fillOval() method paints a circle and an ellipse.The first two parameters are the x and y coordinates. The third andthe fourth parameter are the width and height of the oval.

The fillRect() fills a rectangle using the current fill paint.

The fillRoundRect() paints a rectangle, whose corners are rounded.The last two parameters of the method are the arc width and arc height of therectangle corners.

The fillArc() method fills an arc using the current fill paint. Thelast three parameters are the starting angle, the angular extend, and theclosure type.

Canvas Draw 5 0 2 – Universal Graphics Tool Set

The fillPolygon() method fills a polygon with the given pointsusing the currently set fill paint. In our case, it paints a right-angledtriangle. The first parameter is an array containing the x coordinates of thepolygon points, the second parameter is an array containing the y coordinates ofthe polygon points. The last parameter is the number of points that form apolygon.

JavaFX star shape

More complex shapes can be drawn with the strokePolygon() andfillPolygon() methods. The next example draws a Star shape.

The example draws an outline of a Star shape. The shape consists often coordinates.

These are the x and y coordinates of the shape.

The shape is drawn with the strokePolygon() method.

JavaFX transparent rectangles

Transparency is the quality of being able to see through a material. In computergraphics, we can achieve transparency effects using alpha compositing. Alphacompositing is the process of combining an image with a background to create theappearance of partial transparency.

com/zetcode/TransparentRectanglesEx.java

The example paints ten rectangles with different levels of transparency.

Canvas Draw 5 0 2 – Universal Graphics Tool Setup

An alpha value is computed in each of the for cycles.

Canvas draw 5 0 2 – universal graphics tool settings

The setGlobalAlpha() method sets the global alpha of the current state.

Canvas Draw 5 0 2 – Universal Graphics Tool Sets

In this chapter, we performed drawing operations on a Canvas node.