WPF Graphics: Drawing a circle with code

This entry is part 5 of 8 in the series WPF Graphics

If you have like to use the traditional method to draw a circle instead of using XAML.

Notice that I did not declare a Stackpanel in the xaml.  The stackpanel is displayed using

this.Content = stack;

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApplication10
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void OnLoad(object sender, RoutedEventArgs e)
        {
            StackPanel stack = new StackPanel();

            // Create a red Ellipse.
            Ellipse ellipse = new Ellipse();

            // Create a SolidColorBrush with a yellow color to fill the 
            // Ellipse with.
            SolidColorBrush colorBrush = new SolidColorBrush();

            // Describes the brush's color using RGB values. 
            // Each value has a range of 0-255.
            colorBrush.Color = Color.FromArgb(255, 255, 255, 0);
            ellipse.Fill = colorBrush;
            ellipse.StrokeThickness = 2;
            ellipse.Stroke = Brushes.Black;

            // Set the width and height of the Ellipse.
            ellipse.Width = 250;
            ellipse.Height = 250;

            // Add the Ellipse to the StackPanel.
            stack.Children.Add(ellipse);

            this.Content = stack;
        }

    }
}

MainWindow.xaml

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication10"
        mc:Ignorable="d"
        Loaded="OnLoad"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

circle1

WPF Graphics: Drawing a circle

This entry is part 4 of 8 in the series WPF Graphics

So using the earlier ellipse example, we can draw a circle.

<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication9"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Ellipse
        Fill="Yellow"
        Height="200"
        Width="200"
        StrokeThickness="2"
        Stroke="Black"/>
</Window>

circle

Drawing line, circle and rectangle

This is a relatively simple JavaFX program that draws out a line, a circle and a rectangle.

package javafxapplication7;

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

public class JavaFXApplication7 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        initUI(primaryStage);
    }

    public void initUI(Stage stage) {
        Pane root = new Pane();

        Rectangle rect = new Rectangle(50, 100, 100, 100);
        rect.setFill(Color.CADETBLUE);

        Line line = new Line(90, 40, 300, 40);
        line.setStroke(Color.BLACK);

        Circle circle = new Circle(230, 230, 30);
        circle.setFill(Color.CHOCOLATE);

        root.getChildren().addAll(rect, line, circle);

        Scene scene = new Scene(root, 400, 300, Color.WHITESMOKE);

        stage.setTitle("Absolute layout");
        stage.setScene(scene);
        stage.show();
    }

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

drawing

JavaFX: Adding circle to line group

This entry is part 52 of 55 in the series JavaFX

For purpose of demonstration, we see how easy it is to add another drawing to the line group.

The main line that we are using is

group.getChildren().add(circle);

This put the circle to be in the same group as the line group.

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();

        // The background grid
        for (int i = 0; i < 400; i += 20) {
            Line hline = new Line(i, 0, i, 360);
            hline.setStroke(Color.LIGHTGRAY);
            Line vline = new Line(0, i, 400, i);
            vline.setStroke(Color.DARKBLUE);
            group.getChildren().addAll(hline, vline);
        }
        
        Circle circle = new Circle(200, 180, 75);
        circle.setStroke(Color.BLACK);
        circle.setFill(null);
        circle.setStrokeWidth(3);
        group.getChildren().add(circle);

        // Create the scene and the stage
        Scene scene = new Scene(group, 400, 360);
        stage.setScene(scene);
        stage.setTitle("Grid Lines and Circle");
        stage.show();
    }

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

}

group1

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