Main Site Documentation

WPF Polygons Not showing


#1

Has anyone ever tried drawing a polygon using WPF? I am happily able to draw text rectangles etc but polygons just dont show. No exceptions etc either.
Polygons take an array of corordinates even for X and odd for Y so the folowing should work but it doesnt.

            
            MyPoints = new Int32[6]; // Coords array Even X Odd Y
            MyPoints[0] = 1;
            MyPoints[1] = 1;
            MyPoints[2] = 120;
            MyPoints[3] = 120;
            MyPoints[4] = 1;
            MyPoints[5] = 100;
 

            Polygon Poly = new Polygon();
            Poly.Points = MyPoints;
            Poly.Fill = new LinearGradientBrush(Colors.Orange, Colors.Red);
            Poly.Stroke.Color = Colors.Red;
            canvas.Children.Add(Poly);

Any advice muchly appreciated.


#2

Try “closing” it by adding first point at the end


#3

@ Architect - Already tried that :slight_smile: i then made an asumption that it auto closes the poly.
Tried it again just to make sure.


#4

@ HughB - This is from the MSDN documentation for full .NET on drawing shapes with WPF. There seems to be some stuff not in your your code.

PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(new Point(0,0));
myPointCollection.Add(new Point(0,1));
myPointCollection.Add(new Point(1,1));

Polygon myPolygon = new Polygon();
myPolygon.Points = myPointCollection;
myPolygon.Fill = Brushes.Blue;
myPolygon.Width = 100;
myPolygon.Height = 100;
myPolygon.Stretch = Stretch.Fill;
myPolygon.Stroke = Brushes.Black;
myPolygon.StrokeThickness = 2;

// add to canvas


#5

@ Mike - I did look at that but i dont think that the WPF implementation on .Netmf supports PointCollections.

This is the description of teh polygon constructor http://msdn.microsoft.com/en-us/library/cc544251.aspx

Polygon (Int32[]) Initializes a new instance of the Polygon class with specified points.

Now not being much of a coder im probably getting into “punching above my weight” territory :), but unless im way off base this operates differently to the Point Collections which take an X and Y coord as individual items in the pointcollection array. If i understand it correctly that is.


#6

@ HughB - I think that the more important code is definition of the height and width of the Polygon object.


#7

@ Mike - Fantastic, thanks for the pointer. At first it didnt make sense as to why I would need to specify the height and width of the polygon object because I was givng it the co’ords but then I realised that its the object and the coords a relative to the object size.
Again many thanks for the help :slight_smile:

Incase anyone is interested here is the test code. and yes i already know about the spelling mistake :wink:

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace WPF_Guage
{
    public partial class Program
    {
        private Brush PolyFillBrush;
        Window mainWindow;
        Canvas canvas;
        Int32[] MyPoints;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/

            SetupDisplay();
   
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
            
            ShowPoly();
        }
        private void SetupDisplay()
        {
            mainWindow = display_T35.WPFWindow;
            mainWindow.Height = display_T35.WPFWindow.Height;
            mainWindow.Width = display_T35.WPFWindow.Width;
            canvas = new Canvas();
            mainWindow.Child = canvas;  
        }
        private void ShowPoly()
        {
           
            PolyFillBrush = new SolidColorBrush(Colors.Red);
            
            MyPoints = new Int32[8]; // Coords array Even X Odd Y
            MyPoints[0] = 50;
            MyPoints[1] = 50;
            MyPoints[2] = 120;
            MyPoints[3] = 120;
            MyPoints[4] = 50;
            MyPoints[5] = 120;
            MyPoints[6] = 40;
            MyPoints[7] = 10;
            //Polygons Auto close so you dont have to specify last coords.

            Polygon Poly = new Polygon();
            Poly.Width = 320;
            Poly.Height = 240;
            Poly.Points = MyPoints;
            Poly.Fill = PolyFillBrush; //use this to fill the whole poly with a colour;
            Poly.Stroke.Thickness = 2;
            Poly.Stroke.Color = Colors.Red;
            canvas.Children.Add(Poly);
        }
    }
}