' D. de Haas
' Amsterdam, Netherlands
' 29 October 2002
' ddhadam@yahoo.com

Option Explicit On 
Option Strict On

Imports System
Imports System.Drawing

Namespace Graphics

    Public Class Block
        ' Graphical Block

        Private mX, mY, mBlockSize As Integer
        Private mColor As Color

        '==== Events

        '==== Constructors

        Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal blockSize As Integer, ByVal blockColor As Color)
            ' Create a new Block on postition (x,y) and with size (blocksize) in pixels

            Me.BlockSize = blockSize

            mX = x
            mY = y
            mColor = blockColor
        End Sub

        '==== Private Methods/Functions

        '==== Public Functions

        Public Function Clone() As Block
            ' Creates a copy

            Dim bc As New Block(mX, mY, mBlockSize, mColor)
            Return bc
        End Function

        '==== Public Methodes

        Public Sub SetPos(ByVal x As Integer, ByVal y As Integer)
            ' Set the block to the give position (x,y) in pixels

            mX = x
            mY = y
        End Sub

        Public Sub Draw(ByRef graph As System.Drawing.Graphics)
            ' Draws the block

            ' Filled innerblock
            Dim r As New Rectangle(mX + 1, mY + 1, mBlockSize - 1, mBlockSize - 2)
            Dim sb As New Drawing.Drawing2D.LinearGradientBrush(r, mColor, Color.WhiteSmoke, 40)
            graph.FillRectangle(sb, r)

            ' Black outerline
            Dim pb As New Pen(Color.Black)
            graph.DrawLine(pb, mX + 1, mY, mX + mBlockSize - 2, mY)
            graph.DrawLine(pb, mX, mY + 1, mX, mY + mBlockSize - 2)
            graph.DrawLine(pb, mX + mBlockSize - 1, mY + 1, mX + mBlockSize - 1, mY + mBlockSize - 2)
            graph.DrawLine(pb, mX + 1, mY + mBlockSize - 1, mX + mBlockSize - 2, mY + mBlockSize - 1)

            ' White innerline
            Dim pw As New Pen(Color.White)
            graph.DrawLine(pw, mX + mBlockSize - 4, mY + 4, mX + mBlockSize - 4, mY + mBlockSize - 5)
            graph.DrawLine(pw, mX + 4, mY + mBlockSize - 4, mX + mBlockSize - 5, mY + mBlockSize - 4)

            ' Clean up memory
            pw.Dispose()
            pb.Dispose()
            sb.Dispose()
        End Sub

        '==== Properties

        Public ReadOnly Property X() As Integer
            Get
                Return mX
            End Get
        End Property

        Public ReadOnly Property Y() As Integer
            Get
                Return mY
            End Get
        End Property

        Public ReadOnly Property BlockColor() As Color
            Get
                Return mColor
            End Get
        End Property

        Public Property BlockSize() As Integer
            Get
                Return mBlockSize
            End Get
            Set(ByVal Value As Integer)
                If Value < 5 Then Throw New Exception("Blocksize can't be smaller than 5 !!!")

                mBlockSize = Value
            End Set
        End Property

    End Class

End Namespace
