How to Draw a Circle Javafx

JavaFX Canvas

last modified January six, 2022

Canvas is an prototype that can be drawn on using a prepare of graphics commands provided by a GraphicsContext. Information technology is a high-level tool for doing painting.

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

JavaFX simple lines

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

com/zetcode/SimpleLinesEx.java

packet com.zetcode;  import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvass.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.Stage;  public course SimpleLinesEx extends Application {      @Override     public void start(Stage stage) {          initUI(stage);     }      private void initUI(Stage stage) {          var root = new Pane();          var sail = new Canvas(300, 300);         var gc = sail.getGraphicsContext2D();         drawLines(gc);          root.getChildren().add(canvas);          var scene = new Scene(root, 300, 250, Color.WHITESMOKE);          stage.setTitle("Lines");         phase.setScene(scene);         stage.evidence();     }      private void drawLines(GraphicsContext gc) {          gc.beginPath();         gc.moveTo(30.v, 30.5);         gc.lineTo(150.5, 30.5);         gc.lineTo(150.five, 150.5);         gc.lineTo(thirty.5, xxx.5);         gc.stroke();     }      public static void main(String[] args) {         launch(args);     } }        

The case draws 3 lines which form a rectangle.

var canvas = new Canvass(300, 300);        

A Sail is constructed with a width and top that specifies the size of the image into which the canvas cartoon commands are rendered. All drawing operations are clipped to the bounds of that paradigm.

var gc = sheet.getGraphicsContext2D();        

The getGraphicsContext2D returns a GraphicsContext associated with the canvas.

drawLines(gc);        

The drawing is delegated to the drawLines method.

gc.beginPath();        

A line primitive is represented as a path element. The beginPath method starts a new path.

gc.moveTo(30.5, thirty.five);        

The moveTo method moves the starting point of the current path to the specified coordinate.

gc.lineTo(150.5, 30.five); gc.lineTo(150.v, 150.5); gc.lineTo(xxx.5, xxx.5);        

The lineTo methods add line segments to the electric current path.

gc.stroke();        

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

Lines
Figure: Lines

JavaFX stroke and fill up

A stroke is used to draw outlines of shapes. A fill is used to pigment interiors of shapes.

com/zetcode/StrokeFillEx.java

packet com.zetcode;  import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.sheet.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.pigment.Color; import javafx.stage.Phase;  public class StrokeFillEx extends Application {      @Override     public void starting time(Phase phase) {          initUI(stage);     }      private void initUI(Phase stage) {          var root = new Pane();          var canvas = new Canvas(300, 300);         var gc = canvas.getGraphicsContext2D();         doDrawing(gc);          root.getChildren().add(canvas);          var scene = new Scene(root, 300, 250, Color.WHITESMOKE);          stage.setTitle("Stroke and fill up");         stage.setScene(scene);         stage.show();     }      private void doDrawing(GraphicsContext gc) {          gc.setStroke(Colour.FORESTGREEN.brighter());         gc.setLineWidth(5);         gc.strokeOval(30, 30, 80, fourscore);         gc.setFill(Color.FORESTGREEN);         gc.fillOval(130, 30, 80, 80);     }      public static void main(String[] args) {         launch(args);     } }        

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

gc.setStroke(Color.FORESTGREEN.brighter());        

The setStroke method sets the current stroke paint aspect. The default colour is black. The attribute is used by the stroke methods of the GraphicsContext.

gc.setLineWidth(5);        

The setLineWidth sets the current line width.

gc.strokeOval(130, 30, 80, 80);        

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

gc.setFill(Color.FORESTGREEN);        

The setFill method sets the current fill pigment aspect. The default colour is black. The attribute is used by the fill methods of the GraphicsContext.

gc.fillOval(xxx, thirty, fourscore, 80);        

The fillOval fills an oval using the current fill paint.

Stroke and fill
Figure: Stroke and make full

JavaFX colours

The Color class is used to work with colours in JavaFX. At that place are many predefined colours. Custom colour values tin be created using the RGB or HSB colour model.

com/zetcode/ColoursEx.java

bundle com.zetcode;  import javafx.awarding.Application; import javafx.scene.Scene; import javafx.scene.canvas.Sheet; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.pigment.Colour; import javafx.stage.Phase;   public form ColoursEx extends Application {      @Override     public void start(Stage phase) {          initUI(stage);     }      private void initUI(Stage stage) {          var root = new Pane();          var sail = new Canvas(300, 300);         var gc = canvas.getGraphicsContext2D();         drawShapes(gc);          root.getChildren().add(canvas);          Scene scene = new Scene(root, 280, 200, Color.WHITESMOKE);          stage.setTitle("Colours");         stage.setScene(scene);         stage.show();     }      private void drawShapes(GraphicsContext gc) {          gc.setFill(Colour.CADETBLUE);         gc.fillOval(30, 30, l, fifty);          gc.setFill(Colour.DARKRED);         gc.fillOval(110, xxx, 50, l);          gc.setFill(Color.STEELBLUE);         gc.fillOval(190, thirty, 50, l);          gc.setFill(Color.BURLYWOOD);         gc.fillOval(30, 110, 50, fifty);          gc.setFill(Color.LIGHTSEAGREEN);         gc.fillOval(110, 110, 50, 50);          gc.setFill(Color.CHOCOLATE);         gc.fillOval(190, 110, l, 50);     }      public static void main(String[] args) {         launch(args);     } }        

The example draws vi circles using predefined colour values.

gc.setFill(Colour.CADETBLUE);        

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

gc.fillOval(30, 30, fifty, fifty);        

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

Colours
Effigy: Colours

JavaFx gradients

In figurer graphics, a gradient is a smoothen blending of shades from low-cal to dark or from one color to another. In drawing and paint programs, gradients are used to create colourful backgrounds and special effects as well 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. Information technology is defined by the LinearGradient class.

com/zetcode/LinearGradientEx.java

packet com.zetcode;  import javafx.application.Awarding; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.pigment.CycleMethod; import javafx.scene.pigment.LinearGradient; import javafx.scene.paint.Terminate; import javafx.stage.Phase;  public course LinearGradientEx extends Awarding {      @Override     public void starting time(Stage stage) {          initUI(stage);     }      private void initUI(Stage stage) {          var root = new Pane();          var canvas = new Sheet(300, 300);         var gc = sheet.getGraphicsContext2D();         doDrawing(gc);          root.getChildren().add(canvas);          var scene = new Scene(root, 300, 250, Color.WHITESMOKE);          stage.setTitle("Linear gradient");         stage.setScene(scene);         stage.show();     }      private void doDrawing(GraphicsContext gc) {          var stops1 = new Stop[] { new Stop(0.2, Colour.Black),                 new Stop(0.five, Color.Cerise), new Cease(0.viii, Color.Blackness)};          var lg1 = new LinearGradient(0, 0, i, 0, true,                 CycleMethod.NO_CYCLE, stops1);          gc.setFill(lg1);         gc.fillRect(l, 30, 200, 180);     }      public static void principal(String[] args) {         launch(args);     } }        

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

var stops1 = new Stop[] { new Stop(0.2, Color.Black),      new End(0.5, Color.RED), new Stop(0.viii, Colour.Blackness)};        

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

var lg1 = new LinearGradient(0, 0, i, 0, truthful,          CycleMethod.NO_CYCLE, stops1);        

The first four parameters specify the line forth which the gradient is painted. The fifth parameter is the proportional parameter, which sets whether the coordinates are proportional to the shape which this slope fills. The sixth parameter sets the cycle method of the gradient. The last parameter takes the stop points.

LinearGradient
Effigy: LinearGradient

Radial gradient

A radial gradient is a smoothen blending of colours or shades of colours betwixt a circle and a focal point. A radial gradient is divers past the RadialGradient class.

com/zetcode/RadialGradientEx.coffee

parcel com.zetcode;  import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.sheet.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.pigment.Colour; import javafx.scene.pigment.CycleMethod; import javafx.scene.paint.RadialGradient; import javafx.scene.paint.Terminate; import javafx.stage.Stage;  public class RadialGradientEx extends Awarding {      @Override     public void outset(Stage stage) {          initUI(stage);     }      private void initUI(Phase stage) {          var root = new Pane();          var canvas = new Sheet(300, 300);         var gc = canvas.getGraphicsContext2D();         doDrawing(gc);          root.getChildren().add together(canvas);          var scene = new Scene(root, 300, 250, Color.WHITESMOKE);          phase.setTitle("Radial gradient");         stage.setScene(scene);         stage.bear witness();     }      individual void doDrawing(GraphicsContext gc) {          var stops1 = new Stop[] { new Finish(0, Color.RED),                 new Cease(ane, Color.BLACK)};          var lg1 = new RadialGradient(0, 0, 0.5, 0.v, 0.viii, true,                 CycleMethod.NO_CYCLE, stops1);          gc.setFill(lg1);         gc.fillOval(thirty, 30, 150, 150);     }       public static void master(String[] args) {         launch(args);     } }        

The example fills a circle with a radial slope.

var stops1 = new Stop[] { new Stop(0, Colour.Ruby),      new Stop(1, Color.BLACK)};        

Nosotros define stop values for the gradient.

var lg1 = new RadialGradient(0, 0, 0.five, 0.5, 0.8, true,          CycleMethod.NO_CYCLE, stops1);        

A radial gradient is created. The first ii parameters are the focus angle and focus altitude. The next 2 parameters are the ten and y coordinates of the centre point of the gradient's circumvolve. The fifth parameter is the radius of the circumvolve defining the extents of the color gradient.

RadialGradient
Figure: RadialGradient

JavaFX shapes

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

com/zetcode/ShapesEx.java

packet com.zetcode;  import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.sail.Canvas; import javafx.scene.sheet.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.ArcType; import javafx.stage.Stage;  public class ShapesEx extends Application {      @Override     public void starting time(Stage phase) {          initUI(stage);     }      private void initUI(Stage stage) {          var root = new Pane();          var canvas = new Sheet(320, 300);         var gc = canvass.getGraphicsContext2D();         drawShapes(gc);          root.getChildren().add(canvas);          var scene = new Scene(root, 300, 200, Color.WHITESMOKE);          stage.setTitle("Shapes");         stage.setScene(scene);         phase.show();     }      individual void drawShapes(GraphicsContext gc) {          gc.setFill(Color.GRAY);          gc.fillOval(30, 30, 50, 50);         gc.fillOval(110, 30, 80, 50);         gc.fillRect(220, xxx, 50, 50);         gc.fillRoundRect(thirty, 120, 50, l, 20, xx);         gc.fillArc(110, 120, lx, threescore, 45, 180, ArcType.Open up);         gc.fillPolygon(new double[]{220, 270, 220},                 new double[]{120, 170, 170}, iii);     }      public static void main(String[] args) {         launch(args);     } }        

The example paints six different shapes using the graphics context'south make full methods.

gc.setFill(Color.Greyness);        

The shapes are painted in gray colour.

gc.fillOval(30, 30, l, l); gc.fillOval(110, 30, fourscore, 50);        

The fillOval method paints a circumvolve and an ellipse. The beginning ii parameters are the x and y coordinates. The third and the quaternary parameter are the width and height of the oval.

gc.fillRect(220, thirty, 50, 50);        

The fillRect fills a rectangle using the current fill up pigment.

gc.fillRoundRect(30, 120, 50, 50, 20, xx);        

The fillRoundRect paints a rectangle, whose corners are rounded. The last ii parameters of the method are the arc width and arc meridian of the rectangle corners.

gc.fillArc(110, 120, 60, 60, 45, 180, ArcType.Open);        

The fillArc method fills an arc using the current fill pigment. The last iii parameters are the starting angle, the angular extend, and the closure type.

gc.fillPolygon(new double[]{220, 270, 220},          new double[]{120, 170, 170}, 3);        

The fillPolygon method fills a polygon with the given points using the currently set fill pigment. In our case, it paints a correct-angled triangle. The first parameter is an array containing the ten coordinates of the polygon points, the second parameter is an array containing the y coordinates of the polygon points. The concluding parameter is the number of points that form a polygon.

Colurs
Figure: Colours

JavaFX star shape

More circuitous shapes can be drawn with the strokePolygon and fillPolygon methods. The adjacent example draws a Star shape.

com/zetcode/StarShapeEx.java

bundle com.zetcode;  import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.Stage;  public class StarShapeEx extends Awarding {      @Override     public void start(Stage stage) {          initUI(stage);     }      private void initUI(Stage stage) {          var root = new Pane();          var canvas = new Canvas(300, 300);         var gc = canvass.getGraphicsContext2D();         drawStarShape(gc);          root.getChildren().add(canvas);          var scene = new Scene(root, 300, 250, Colour.WHITESMOKE);          stage.setTitle("Star");         stage.setScene(scene);         stage.show();     }      private void drawStarShape(GraphicsContext gc) {          double[] xpoints = {ten, 85, 110, 135, 210, 160,                 170, 110, 50, 60};         double[] ypoints = {85, 75, 10, 75, 85, 125,                 190, 150, 190, 125};          gc.strokePolygon(xpoints, ypoints, xpoints.length);     }      public static void chief(String[] args) {         launch(args);     } }        

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

double[] xpoints = {10, 85, 110, 135, 210, 160,         170, 110, 50, 60}; double[] ypoints = {85, 75, 10, 75, 85, 125,         190, 150, 190, 125};        

These are the ten and y coordinates of the shape.

gc.strokePolygon(xpoints, ypoints, xpoints.length);        

The shape is fatigued with the strokePolygon method.

Star shape
Figure: Star shape

JavaFX transparent rectangles

Transparency is the quality of beingness able to see through a material. In computer graphics, we can achieve transparency effects using alpha compositing. Alpha compositing is the process of combining an image with a background to create the advent of fractional transparency.

com/zetcode/TransparentRectanglesEx.java

package com.zetcode;  import javafx.application.Awarding; import javafx.scene.Scene; import javafx.scene.sail.Sail; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.paint.Colour; import javafx.stage.Phase;  public class TransparentRectanglesEx extends Application {      @Override     public void start(Stage stage) {          initUI(stage);     }      private void initUI(Stage phase) {          var root = new Pane();          var canvas = new Canvas(600, 300);         var gc = sheet.getGraphicsContext2D();         drawRectangles(gc);          root.getChildren().add(canvas);          var scene = new Scene(root, 600, 100, Colour.WHITESMOKE);          stage.setTitle("Transparent rectangles");         stage.setScene(scene);         phase.prove();     }      individual void drawRectangles(GraphicsContext gc) {          for (int i = 1; i <= ten; i++) {              float alpha = i * 0.1f;              gc.setFill(Color.FORESTGREEN);             gc.setGlobalAlpha(blastoff);             gc.fillRect(50 * i, 20, forty, 40);         }     }      public static void primary(Cord[] args) {         launch(args);     } }        

The instance paints ten rectangles with dissimilar levels of transparency.

bladder alpha = i * 0.1f;        

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

gc.setGlobalAlpha(alpha);        

The setGlobalAlpha method sets the global alpha of the current land.

Transparent rectangles
Figure: Transparent rectangles

In this chapter, nosotros performed drawing operations on a Canvass node.

yimhatemselithe1943.blogspot.com

Source: https://zetcode.com/gui/javafx/canvas/

Related Posts

0 Response to "How to Draw a Circle Javafx"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel