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
| Public MustInherit Class Shape
Protected _pen As Pen
Public MustOverride Sub draw(ByRef g As Graphics)
End Class
Public Class Line : Inherits Shape
Private _x1, _y1, _x2, _y2 As Single
Public Sub New(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByVal pen As Pen)
_x1 = x1
_y1 = y1
_x2 = x2
_y2 = y2
_pen = pen
End Sub
Public Overrides Sub Draw(ByRef g As Graphics)
g.DrawLine(_pen, _x1, _y1, _x2, _y2)
End Sub
End Class
Public Class Circle : Inherits Shape
Private _xc, _yc, _ray As Single
Public Sub New(ByVal xc As Single, ByVal yc As Single, ByVal ray As Single, ByVal pen As Pen)
_xc = xc
_yc = yc
_ray = ray
_pen = pen
End Sub
Public Overrides Sub Draw(ByRef g As Graphics)
g.DrawEllipse(_pen, _xc - _ray, _yc - _ray, _ray * 2, _ray * 2)
End Sub
End Class
Public Class Form1
Private shapes As New System.Collections.ObjectModel.Collection(Of Shape)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
shapes.Add(New Line(100, 100, 200, 200, Pens.Red))
shapes.Add(New Circle(150, 150, 50, Pens.Blue))
For Each shape As Shape In shapes
shape.draw(Me.CreateGraphics)
Next
End Sub
End Class |
Partager