IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Dessiner un polygon dans un canevas


Sujet :

VB.NET

  1. #1
    Membre éprouvé

    Homme Profil pro
    Inscrit en
    Mars 2012
    Messages
    691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Israël

    Informations forums :
    Inscription : Mars 2012
    Messages : 691
    Points : 929
    Points
    929
    Par défaut Dessiner un polygon dans un canevas
    Bonjour

    Mon premiere application uwp le but dessiner un chiffre (Afficheur 7 segments) represente par des polygons.
    j'ai une class nommee Classsevensegment ayant une methode publique drawdigit qui dessine des polygons et des autres methodes GetwidthBox et GetHeightBox.
    dans le MainPage.xaml j'ai place un canvas nomme canvas1.
    dans le mainpage je declare un objet de la class Classsevensegment
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Private digitobj As Classsevensegment
      private brushseg As SolidColorBrush
     Private Sub InitPanelTimer()
            brushseg = New SolidColorBrush(Windows.UI.Colors.SteelBlue)
            digitobj = New Classsevensegment(SEGMENTTIMER, brushseg)
            canvas1.Width = digitobj.GetwidthBox
            canvas1.Height = digitobj.GetHeightBox
        End Sub
     
    Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
            InitPanelTimer()
        End Sub
    je voudrais appeler la methode digitobj.drawdigit pour dessiner dans le canvas1 comment proceder ?

    merci

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour

    Ou est le problème ? il n' y a qu'à appeler ta méthode de dessin.
    Poste le code de ton class Classsevensegment....

  3. #3
    Membre éprouvé

    Homme Profil pro
    Inscrit en
    Mars 2012
    Messages
    691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Israël

    Informations forums :
    Inscription : Mars 2012
    Messages : 691
    Points : 929
    Points
    929
    Par défaut
    Bonjour

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    Imports Windows.UI.Xaml.Shapes
    Public Class Classsevensegment
        Private digitlength As Integer
        Private width As Single
        Private brushseg As SolidColorBrush
     
        Private listbooleansegment As New List(Of Boolean)
        Private isfilled As Boolean
        'listbooleansegment est une list de boolean qui indique si le segment est allumé ou éteint 
     
        Private Sub segment()
            'draws digit as seven segmemt 
            Dim poly As New Polygon
            Dim polyline As New Polyline
            Dim x1, y1 As Single
            Dim vertex As New PointCollection
            Dim space As Single
            space = 2
            polyline.Stroke = brushseg
            polyline.StrokeThickness = 2
            'horizontal up 
            vertex.Add(New Windows.Foundation.Point(width / 2 + 12, space))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X + digitlength, vertex(0).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X + width / 2, vertex(1).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(2).X - width / 2, vertex(2).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X, vertex(3).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(4).X - width / 2, vertex(2).Y))
            x1 = vertex(2).X + space
            y1 = vertex(2).Y
     
            If listbooleansegment(0) = True Then
                If isfilled Then
                    poly.Fill() = brushseg
                    poly.Points = vertex
                Else
                    polyline.Points = vertex
     
                End If
     
            End If
     
            'vertical up left
            Dim pt As New Point(vertex(4).X, vertex(4).Y)
            vertex.Clear()
            vertex.Add(New Windows.Foundation.Point(pt.X - 2, pt.Y))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X, vertex(0).Y + digitlength))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X - width / 2, vertex(1).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(2).X - width / 2, vertex(2).Y - width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(3).X, vertex(0).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(4).X + width / 2, vertex(4).Y - width / 2))
     
            If listbooleansegment(3) = True Then
                If isfilled Then
                    poly.Fill() = brushseg
                    poly.Points = vertex
                Else
                    polyline.Points = vertex
                End If
     
            End If
            'middle horizontal
            pt.X = vertex(1).X + space
            pt.Y = vertex(1).Y
     
            vertex.Clear()
            vertex.Add(New Windows.Foundation.Point(pt.X, pt.Y))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X + digitlength, vertex(0).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X + width / 2, vertex(1).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X, vertex(2).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(3).X - digitlength, vertex(3).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(4).X + width / 2, vertex(4).Y - width / 2))
            If listbooleansegment(1) = True Then
                If isfilled Then
                    poly.Fill() = brushseg
                    poly.Points = vertex
                Else
                    polyline.Points = vertex
                End If
     
            End If
            pt.X = vertex(4).X - space
            pt.Y = vertex(4).Y
            'vertical left down 
            vertex.Clear()
            vertex.Add(New Windows.Foundation.Point(pt.X, pt.Y))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X - width / 2, vertex(0).Y - width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X - width / 2, vertex(1).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(2).X, vertex(2).Y + digitlength))
            vertex.Add(New Windows.Foundation.Point(vertex(3).X + width / 2, vertex(3).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(4).X + width / 2, vertex(4).Y - width / 2))
            If listbooleansegment(5) = True Then
                If isfilled Then
                    poly.Fill() = brushseg
                    poly.Points = vertex
                Else
                    polyline.Points = vertex
                End If
     
            End If
            pt.X = vertex(5).X + space
            pt.Y = vertex(5).Y
            'horizontal bottom 
     
            vertex.Clear()
            vertex.Add(New Windows.Foundation.Point(pt.X, pt.Y))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X + digitlength, vertex(0).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X + width / 2, vertex(1).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X, vertex(2).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X, vertex(3).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(4).X - width / 2, vertex(4).Y - width / 2))
     
            If listbooleansegment(2) = True Then
                If isfilled Then
                    poly.Fill() = brushseg
                    poly.Points = vertex
                Else
                    polyline.Points = vertex
                End If
     
            End If
     
            'vertical right bottom
            pt.X = vertex(1).X + space
            pt.Y = vertex(1).Y
     
            vertex.Clear()
            vertex.Add(New Windows.Foundation.Point(pt.X, pt.Y))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X + width / 2, vertex(0).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X + width / 2, vertex(1).Y - width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(2).X, vertex(2).Y - digitlength))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X, vertex(3).Y - width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(4).X - width / 2, vertex(4).Y + width / 2))
     
            If listbooleansegment(6) = True Then
                If isfilled Then
                    poly.Fill() = brushseg
                    poly.Points = vertex
                Else
                    polyline.Points = vertex
                End If
     
            End If
            ' vertical right up
            vertex.Clear()
            vertex.Add(New Windows.Foundation.Point(x1, y1))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X + width / 2, vertex(0).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X, vertex(1).Y + digitlength))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X, vertex(2).Y + width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(3).X - width / 2, vertex(3).Y - width / 2))
            vertex.Add(New Windows.Foundation.Point(vertex(4).X, vertex(1).Y))
     
            If listbooleansegment(4) = True Then
                If isfilled Then
                    poly.Fill() = brushseg
                    poly.Points = vertex
                Else
                    polyline.Points = vertex
                End If
     
            End If
        End Sub
     
        Public Sub New(ByVal segemtlength As Integer, ByVal brushsegment As SolidColorBrush)
            digitlength = segemtlength
            brushseg = brushsegment
     
            width = Convert.ToSingle(digitlength / 5)
     
        End Sub
        Private Sub UpdateBooleanVector(ByVal digit As Integer)
            ' met à jour  listbooleansegment selon le chiffre à afficher
            ' segment horizontal du haut index 0
            ' segment horizontal du milieu index 1
            ' segment horizontal du bas index 2
            ' segment vertical du haut à gauche index 3
            ' segment vertical du haut à droite index 4
            ' segment vertical du bas à gauche index 5
            ' segment vertical du bas à droite  index 6
     
            '  Dim sevensegbool As New List(Of Boolean)
            listbooleansegment.Clear()
            Select Case digit
     
                Case 0
                    For iter = 0 To 6
                        If iter <> 1 Then
                            listbooleansegment.Add(True)
                        Else
                            listbooleansegment.Add(False)
                        End If
     
                    Next
     
                Case 1
                    For iter = 0 To 6
                        If iter <> 4 And iter <> 6 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
     
                Case 2
                    For iter = 0 To 6
                        If iter = 3 Or iter = 6 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
                Case 3
                    For iter = 0 To 6
                        If iter = 3 Or iter = 5 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
                Case 4
                    For iter = 0 To 6
                        If iter = 0 Or iter = 2 Or iter = 5 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
                Case 5
                    For iter = 0 To 6
                        If iter = 4 Or iter = 5 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
                Case 6
                    For iter = 0 To 6
                        If iter = 4 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
                Case 7
                    For iter = 0 To 6
                        If iter <> 4 And iter <> 6 And iter <> 0 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
                Case 8
                    For iter = 0 To 6
                        listbooleansegment.Add(True)
                    Next
     
                Case 9
                    For iter = 0 To 6
                        If iter = 5 Then
                            listbooleansegment.Add(False)
                        Else
                            listbooleansegment.Add(True)
                        End If
     
                    Next
            End Select
     
     
        End Sub
        Public Sub DrawDigit(ByVal digit As Integer, ByVal filled As Boolean, ByVal brushsegment As SolidColorBrush)
            isfilled = filled
            brushseg = brushsegment
            UpdateBooleanVector(digit)
            segment()
        End Sub
        Public Sub DrawDrawColon(ByVal filled As Boolean, ByVal brushsegment As SolidColorBrush)
     
            Dim poly As New Polygon
            Dim polyline As New Polyline
            Dim vertex As New PointCollection
            polyline.Stroke = brushseg
            polyline.StrokeThickness = 2
            isfilled = filled
            brushseg = brushsegment
            vertex.Add(New Windows.Foundation.Point(digitlength \ 3, 3 * Convert.ToInt32(digitlength / 4)))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X + 6, vertex(0).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X, vertex(1).Y + 6))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X, vertex(2).Y))
            If isfilled Then
                poly.Fill() = brushseg
                poly.Points = vertex
            Else
                polyline.Points = vertex
            End If
            vertex.Clear()
            vertex.Add(New Windows.Foundation.Point(digitlength \ 3, 6 * Convert.ToInt32(digitlength / 4)))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X + 6, vertex(0).Y))
            vertex.Add(New Windows.Foundation.Point(vertex(1).X, vertex(1).Y + 6))
            vertex.Add(New Windows.Foundation.Point(vertex(0).X, vertex(2).Y))
            If isfilled Then
                poly.Fill() = brushseg
                poly.Points = vertex
            Else
                polyline.Points = vertex
            End If
        End Sub
        Public Function GetHeightBox() As Integer
            Return Convert.ToInt32(digitlength * 2 + 3 * width + 12)
        End Function
        Public Function GetwidthBox() As Integer
            Return Convert.ToInt32(digitlength + 2 * width + 20)
        End Function
    End Class

  4. #4
    Membre éprouvé

    Homme Profil pro
    Inscrit en
    Mars 2012
    Messages
    691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Israël

    Informations forums :
    Inscription : Mars 2012
    Messages : 691
    Points : 929
    Points
    929
    Par défaut
    J'ai trouvé la solution
    il faut passer le polygon comme parametre à canvas1.Children.Add(le polygon)

  5. #5
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour

    Ton class data doit exposer une prop CLR publique "polygon" qui sera accessible à l'instance du Canvas
    code du class revu :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Public Class ClasssEvenSegment
        Private digitlength As Integer
        Private width As Single
        Private brushseg As SolidColorBrush
     
        Private listbooleansegment As New List(Of Boolean)
        Private isfilled As Boolean
         'prop clr publique
        Public Property PlgSegment() As Polygon
     
    ...suite ton code...
    End Class
    code du form xaml:
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Canvas  x:Name="chart"
                     Background="Transparent" >
     
            </Canvas>
        </Grid>
    </Window>
    code behind.vb du form :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    Class MainWindow
        Private tmr As DispatcherTimer
        Private SEGMENTTIMER As Integer
        Private digitobj As ClasssEvenSegment
        Private brushseg As SolidColorBrush
     
        Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
            InitPanelTimer()
        End Sub
        Private Sub InitPanelTimer()
            brushseg = New SolidColorBrush(Colors.SteelBlue)
            digitobj = New ClasssEvenSegment(SEGMENTTIMER, brushseg)
            chart.Width = digitobj.GetwidthBox
            chart.Height = digitobj.GetHeightBox
            chart.Children.Add(digitobj.PlgSegment)
     
        End Sub
     
     
    End Class
    bon code...

Discussions similaires

  1. dessiner dans le canevas d'un composant
    Par chekkal dans le forum Composants VCL
    Réponses: 8
    Dernier message: 04/11/2015, 10h50
  2. Commander au clavier un dessin dans un canevas
    Par cal23 dans le forum Tkinter
    Réponses: 10
    Dernier message: 19/01/2015, 20h37
  3. Réponses: 4
    Dernier message: 10/09/2006, 19h06
  4. Dessin ne s'affiche pas dans le canevas
    Par Nadjib dans le forum Langage
    Réponses: 3
    Dernier message: 24/03/2006, 21h08
  5. Texte dans un canevas
    Par Bernard M dans le forum Langage
    Réponses: 4
    Dernier message: 28/11/2002, 17h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo