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);
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.
@ 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
Incase anyone is interested here is the test code. and yes i already know about the spelling mistake
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);
}
}
}