Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2005, 8:55 AM   #1
Gapper
Newbie
 
Join Date: Mar 2005
Location: Ireland
Posts: 5
Rep Power: 0 Gapper is on a distinguished road
Smile creating a chess board(Have first row)

Am able to draw first line of eight squares but cannot create next rows:
Here is the code that im using.


Private Rects() As Rectangle

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)

Dim w As Integer = Me.pbMain.Width
Dim h As Integer = Me.pbMain.Height

Dim numVlines As Integer = CInt(w / 8)
Dim numHLines As Integer = CInt(h / 8)
Dim b As New Bitmap(w, h)
Dim ppi As Single = w / 8
Dim startX As Integer = 0
Dim clr As Color = Color.Black
Dim switchClr As Boolean = False
Dim g As Graphics = Graphics.FromImage(b)
g.Clear(Color.White)
For i As Integer = 1 To numVlines
If switchClr Then
clr = Color.Green
switchClr = False
Else
clr = Color.GreenYellow
switchClr = True
End If

Dim rect As New Rectangle(startX, 0, ppi, ppi)
g.FillRectangle(New SolidBrush(clr), rect)
g.DrawRectangle(New Pen(Color.White), rect)
ReDim Preserve Rects(i)
Rects(i) = rect
startX += rect.Width
Next

Me.pbMain.Image = b
Gapper is offline   Reply With Quote
Old Apr 23rd, 2005, 10:39 AM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Perhaps this will work better


Dim w As Integer = Me.pbMain.Width
Dim h As Integer = Me.pbMain.Height

Dim SizeX as integer = w / 8
Dim SizeY as integer = w / 8

Const columns As Integer = 8
Const rows As Integer = 8

Dim b As New Bitmap(w, h)
Dim g As Graphics = Graphics.FromImage(b)

Dim clr As Color = Color.Black
Dim switchClr As Boolean = False

dim rects(row - 1, columns - 1) as rectangle

g.Clear(Color.White)

For i As Integer = 1 To rows
For j as integer = 1 to columns

If switchClr Then
clr = Color.Green
switchClr = False
Else
clr = Color.GreenYellow
switchClr = True
End If

Dim rect As New Rectangle(i * sizex, j * sizey, sizex, sizey)
g.FillRectangle(New SolidBrush(clr), rect)
g.DrawRectangle(New Pen(Color.White), rect)

rects(i,j) = rect
Next
next

Me.pbMain.Image = b

8x8 grid that scales to the picture box. It was supposed to be 8x8, right? Or did you want the number of rectangles to depend on the size of the box?
Dameon is offline   Reply With Quote
Old May 10th, 2005, 12:58 PM   #3
Daniel_kd
Newbie
 
Daniel_kd's Avatar
 
Join Date: Apr 2005
Posts: 22
Rep Power: 0 Daniel_kd is on a distinguished road
I'd say this works better

Dim i, j As Integer
        Dim w As Integer = Me.pbmain.Width
        Dim h As Integer = Me.pbmain.Height

        Dim SizeX As Integer = w / 8
        Dim SizeY As Integer = h / 8

        Const columns As Integer = 8
        Const rows As Integer = 8

        Dim b As New Bitmap(w, h)
        Dim g As Graphics = Graphics.FromImage(b)

        Dim clr As Color = Color.Black
        Dim switchClr As Boolean = False


        g.Clear(Color.White)

        For i = 0 To rows - 1
            switchClr = Not switchClr

            For j = 0 To columns - 1

                If switchClr Then
                    clr = Color.Green
                    switchClr = False
                Else
                    clr = Color.GreenYellow
                    switchClr = True
                End If

                Dim rect As New Rectangle(i * SizeX, j * SizeY, SizeX, SizeY)
                g.FillRectangle(New SolidBrush(clr), rect)
                g.DrawRectangle(New Pen(Color.Black), rect)

            Next
        Next

        Me.pbmain.Image = b
Daniel_kd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC