JavaFX Canvas GraphicsContext fill circle

Description

JavaFX Canvas GraphicsContext fill circle

// Demonstrate drawing. 

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Main extends Application {
   public static void main(String[] args) {
      launch(args);//w w w  . j  a  v  a 2 s .  c  om
   }

   public void start(Stage myStage) {
      myStage.setTitle("Draw to a Canvas.");
      FlowPane rootNode = new FlowPane();
      rootNode.setAlignment(Pos.CENTER);
      Scene myScene = new Scene(rootNode, 450, 450);
      myStage.setScene(myScene);
      Canvas myCanvas = new Canvas(400, 400);
      GraphicsContext gc;
      // Get the graphics context for the canvas.
      gc = myCanvas.getGraphicsContext2D();

      // Set the stroke and fill color.
      gc.setStroke(Color.BLUE);
      gc.setFill(Color.RED);

      gc.fillOval(0, 0, 200, 200);


      // Add the canvas and button to the scene graph.
      rootNode.getChildren().addAll(myCanvas);

      // Show the stage and its scene.
      myStage.show();
   }
}



PreviousNext

Related