JavaFX: Putting drawings into group

This entry is part 49 of 55 in the series JavaFX

A group is an ObservableList and will take on the collective bounds of its children and is not directly resizable.

In another words, will will be able to set the absolute positions of the drawings if we put them into group.  So this allows us to better control the positions of the drawings.

package drawing;

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;

public class Drawing extends Application {

    @Override
    public void start(Stage stage) {

        Group group = new Group();

        Rectangle rect = new Rectangle(150, 100, 200, 150);
        rect.setStroke(Color.BLACK);
        rect.setFill(null);
        rect.setStrokeWidth(1);
        group.getChildren().add(rect);

        Circle circle = new Circle(150, 250, 75);
        circle.setStroke(Color.BLACK);
        circle.setFill(null);
        circle.setStrokeWidth(3);
        group.getChildren().add(circle);

        Scene scene = new Scene(group, 400, 400);
        stage.setScene(scene);
        stage.setTitle("Codecrawl.com");
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

group

 

JavaFX: Setting absolute position for circle

This entry is part 47 of 55 in the series JavaFX

In our previous example, we have put the circle in a stack pane.  If you look at the constructors for Circle object, we should be able to set the position of the circle in the scene.

Circle(double centerX, double centerY, double radius, Paint fill)

As was mentioned in an earlier post, we can put the circle in a Pane object and this will allow the absolute position of the circle to take effect.

package javafxapplication12;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class CirclePosition extends Application {

    @Override
    public void start(Stage stage) {

        stage.setTitle("Codecrawl.com");

        Pane pane = new Pane();

        Scene scene = new Scene(pane, 500, 400);
        stage.setScene(scene);

        pane.setStyle("-fx-background-color: lightgray;");

        Circle circle = new Circle();
        circle.setCenterX(120);
        circle.setCenterY(250);
        circle.setRadius(50);
        circle.setFill(Color.CORNFLOWERBLUE);

        pane.getChildren().add(circle);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

circleposition

 

JavaFX: Creating A Circle

This entry is part 46 of 55 in the series JavaFX

In creating a JavaFX circle, we would usually use the constructor below.

Circle(double radius, Paint fill)

It is usually sufficed for us to only provide a radius and a color for the circle.

So in the example below, we are using

new Circle(150, Color.DARKKHAKI)

where 150 is the radius of the circle in pixel.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class DrawCircle extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane(new Circle(150, Color.DARKKHAKI));
        stage.setScene(new Scene(root, 500, 400));
        stage.show();
    }

    public static void main(String... args) {
        launch(args);
    }
}

circle

 

 

 

HTML5 Canvas: Drawing a circle

This entry is part 11 of 17 in the series HTML5 Canvas

Using the same arc() method, we can draw a circle.

canvas.arc(x, y, radius, startAngle, endAngle, antiClockwise(Boolean));

Now we need to start the angle with 0, and end it with 2*Math.PI:

Codecrawl.com: Arcs or circles

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>Codecrawl.com: Arcs or circles</title>
    <style>
        canvas {
            border: 1px solid #000;
        }
    </style>
</head>
<body>
<canvas id='canvas' width='250' height='250'></canvas>
<script>
    var canvas = document.getElementById('canvas').getContext('2d');
    canvas.arc(100, 100, 60, 0, 2 * Math.PI, false);
    canvas.fillStyle = '#000';
    canvas.fill();
    canvas.strokeStyle = '#000';
    canvas.lineWidth = 2;
</script>
</body>
</html>