WPF Graphics: Draw some lines using pure XAML

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

I just realised that it is possible to draw lines using pure XAML.  No code-behind is needed.

This example is taken from Microsoft site.

MainWindow.xaml:

<Window x:Class="WpfApplication8.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:WpfApplication8"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Canvas Height="300" Width="300">

        <!-- Draws a diagonal line from (10,10) to (50,50). -->
        <Line
    X1="10" Y1="10"
    X2="250" Y2="250"
    Stroke="Black"
    StrokeThickness="4" />

        <!-- Draws a diagonal line from (10,10) to (50,50)
       and moves it 100 pixels to the right. -->
        <Line
    X1="10" Y1="10"
    X2="280" Y2="280"
    StrokeThickness="4"
    Canvas.Left="100">
            <Line.Stroke>
                <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
                    <RadialGradientBrush.GradientStops>
                        <GradientStop Color="Red" Offset="0" />
                        <GradientStop Color="Blue" Offset="0.25" />
                    </RadialGradientBrush.GradientStops>
                </RadialGradientBrush>
            </Line.Stroke>
        </Line>

        <!-- Draws a horizontal line from (10,60) to (150,60). -->
        <Line
     X1="10" Y1="60"
     X2="350" Y2="60"
     Stroke="Black"
     StrokeThickness="4"/>

    </Canvas>

</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

xamlline

WPF Graphics: Drawing a line

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

In windows, you can draw a line using line or pen.  The example here shows using a line with a StrokeThickness set to 5px.

MainWindow.xaml:

<Window x:Class="WpfApplication7.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:WpfApplication7"
        mc:Ignorable="d"
        Loaded="OnLoad"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Name="stack">

    </StackPanel>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Line line;

        public MainWindow()
        {
            InitializeComponent();
        }

        void OnLoad(object sender, RoutedEventArgs e)
        {
            line = new Line();
            line.Stroke = Brushes.LightSteelBlue;

            line.X1 = 10;
            line.X2 = 500;
            line.Y1 = 10;
            line.Y2 = 300;

            line.StrokeThickness = 5;
            stack.Children.Add(line);
        }
    }
}

line

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

HTML5 Canvas: Line tool and coordinates

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

Using the same line tool, we draw a horizontal line across a 500 by 500px square.

We first position the starting point of the line at (0,250) using moveTo and draw the line using lineTo.

We must keep in mind that the (0,0) point is located in the top left corner of the canvas by default, with the X value increasing as you move right and the Y value increasing as you move down.

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>Codecrawl.com: Canvas Line</title>
    <style>
        canvas {
            border: 1px solid #000;
        }
    </style>
</head>
<body>
<canvas id='myCanvas' width='500' height='500'></canvas>
<script>
    var canvas = document.getElementById('myCanvas').getContext('2d');
    canvas.moveTo(0, 250);
    canvas.lineTo(500, 250);
    canvas.stroke();
</script>
</body>
</html>