GraphicsPath - Random problem

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I havent used GDI much other than simple drawing, but I now have an urgent requirement to be able to click and move shapes on screen. I thought I'd start by creating a simple application with 3 random shapes, which can then be clicked on and moved. The code is as follows :-

VB.NET:
Option Explicit On
Option Strict On

Imports System.Drawing.Drawing2D

Public Class cShape

    Public Sub New()
        Dim RN As New Random
        Dim Point1 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point2 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point3 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point4 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim pts As PointF() = New PointF() {Point1, Point2, Point3, Point4}
        RN = Nothing
        _GraphicsPath.AddPolygon(pts)
    End Sub

    Private _ID As Integer = 0
    Public Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property

    Private _GraphicsPath As GraphicsPath = New GraphicsPath
    Public ReadOnly Property GraphicsPath() As GraphicsPath
        Get
            Return _GraphicsPath
        End Get
    End Property

    Private _Selected As Boolean = False
    Public Property Selected() As Boolean
        Get
            Return _Selected
        End Get
        Set(ByVal value As Boolean)
            _Selected = value
        End Set
    End Property

    Public Function Contains(ByVal whatLocation As Point) As Boolean
        Using _Region As Region = New Region(_GraphicsPath)
            Return _Region.IsVisible(whatLocation)
        End Using
    End Function

    Public Sub SaveNewLocation(ByVal whatX As Integer, ByVal whatY As Integer)
        Dim mxTransform As New Matrix
        mxTransform.Translate(whatX, whatY)
        _GraphicsPath.Transform(mxTransform)
    End Sub

End Class

and

VB.NET:
Option Explicit On
Option Strict On

Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1

    Private ShapeList As List(Of cShape) = New List(Of cShape)

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)

        Dim NewShape As cShape


        NewShape = New cShape
        NewShape.ID = 1
        ShapeList.Add(NewShape)
        NewShape = Nothing

        NewShape = New cShape
        NewShape.ID = 2
        ShapeList.Add(NewShape)
        NewShape = Nothing

        NewShape = New cShape
        NewShape.ID = 3
        ShapeList.Add(NewShape)
        NewShape = Nothing

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        With e.Graphics
            For Each _Shape As cShape In ShapeList
                .TranslateTransform(50, 50)
                .DrawPath(Pens.Black, _Shape.GraphicsPath)
            Next

        End With

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each _Shape As cShape In ShapeList
            'MessageBox.Show(_Shape.ID.ToString)
            Dim pp() As PointF = _Shape.GraphicsPath.PathPoints
            MessageBox.Show(String.Format("X {0}, Y {1}", pp(0).X, pp(0).Y))
        Next
    End Sub
End Class

If I run it, it gives me the correct IDs (1, 2 and 3), but all the shapes are drawn the same :confused:

If I add change
VB.NET:
    Public Sub New()
        Dim RN As New Random
        Dim Point1 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point2 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point3 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point4 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim pts As PointF() = New PointF() {Point1, Point2, Point3, Point4}
        RN = Nothing
        _GraphicsPath.AddPolygon(pts)
    End Sub

to
VB.NET:
    Public Sub New()
        Dim RN As New Random
        Dim Point1 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point2 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point3 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim Point4 As Point = New Point(RN.Next(1, 1024), RN.Next(1, 768))
        Dim pts As PointF() = New PointF() {Point1, Point2, Point3, Point4}
        RN = Nothing
        _GraphicsPath.AddPolygon(pts)
        [COLOR="Red"][B]Messagebox.Show ("OK")[/B][/COLOR]
    End Sub

the shapes are then all different :confused:

It's late and I just cant see my mistake. Any ideas?
 
Back
Top