Creating A Grid For 2d Graphics
My Problem is when i click zoom the Retangles location get Thrown Off somtimes and messes with my Grid.
Code:
' Draw Graphics To Screen
Public DRAW As BufferedGraphics
' Array Holds of Rectangles Used For tiles to hold Graphics
Public TileRtang(5 * 5) As Rectangle
' Drawn larger than Screen inorder to move view left,right,up,down,
Public PaintPad As Rectangle
' Used to Create The PaintPad As my Drawing Surface
Dim Context As BufferedGraphicsContext
' Loop Control Integers
Public YG, XG, TG As Integer
' used For Rescaling
Public H, W, MDown, MLeft As Integer
' To Prevent From zooming in to much or out to Far
Public Z As Integer = 0
' Number of Rows x, Number of Rows y , PaintPad width, PaintPad Heigth
Public Rx, Ry, PPw, PPh As Integer
'Index Holds textures index works with TileRtang Index to Tell computer where to draw the Texture
Public TileIndex(0) As Bitmap
' Is placed in the same place as the cursor to create a modified click event to determin which tile you clicked.
Dim CRtang As Rectangle
Public brus As System.Drawing.Drawing2D.HatchBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.Percent50, Color.Blue, Color.Transparent)
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
MyBase.OnClick(e)
Dim MM As Integer
Dim Path As Bitmap
Path = Image.FromFile("C:\Users\Joseph Maxwell\Desktop\Animations\Textures\GrassBad.bmp")
For MM = 0 To TileRtang.GetLongLength(0) - 1 Step 1
If CRtang.IntersectsWith(TileRtang(MM)) Then
TileIndex(MM) = Path
End If
Next
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Draw_Grid()
End Sub
Public Sub Mouse_Rtang()
CRtang.Size = New Size(1, 1)
CRtang.Location = Cursor.Position
End Sub
Public Sub Draw_Grid()
Context = BufferedGraphicsManager.Current
DRAW = Context.Allocate(Me.CreateGraphics, PaintPad)
DRAW.Graphics.FillRectangle(Brushes.White, PaintPad)
'Loads Tiles to Rectangle structers From and Array Holding The textures to be drawn to the Rectangle
Fill_Tiles()
For TG = 0 To TileRtang.GetLength(0) - 1 Step 1
DRAW.Graphics.DrawImage(TileIndex(TG), TileRtang(TG))
Next
'___________________________________________________________________________________________________________
'DRAW(GRID_______________________________________________________________________________________________________)
For YG = 0 To PaintPad.Width Step W
DRAW.Graphics.DrawLine(Pens.Black, PaintPad.Location.X, PaintPad.Location.Y + YG, PaintPad.X + PaintPad.Width, PaintPad.Location.Y + YG)
Next
For XG = 0 To PaintPad.Height Step H
DRAW.Graphics.DrawLine(Pens.Black, PaintPad.Location.X + XG, PaintPad.Location.Y, PaintPad.Location.X + XG, PaintPad.Y + PaintPad.Height)
Next
'_____________________________________________________________________________________________________________________
' Hilights selected Rectangle From the TileRtang Array
Mouse_Rtang()
For XG = 0 To TileRtang.GetLongLength(0) - 1 Step 1
If CRtang.IntersectsWith(TileRtang(XG)) Then
DRAW.Graphics.FillRectangle(brus, TileRtang(XG))
End If
Next
DRAW.Render()
DRAW.Dispose()
End Sub
Public Sub Fill_Tiles()
' set loop control Values to 0
XG = 0
YG = 0
'Creates Retangles at the correct location to create a grid of tiles for painting.
For TG = 0 To TileRtang.GetLength(0) - 2 Step 1
TileRtang(TG).Size = New Size(W, H)
If XG >= PaintPad.Width Then
YG += H
XG = 0
End If
TileRtang(TG).Location = New Point(PaintPad.Location.X + XG, PaintPad.Location.Y + YG)
XG += W
Next
End Sub
Public Sub Draw_PLayers()
End Sub
Public Sub Move_Players()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PPw = 500
PPh = 500
PaintPad.Location = New Point(0, 0)
PaintPad.Size = New Size(PPw, PPh)
H = 100
W = 100
Rx = PPw \ W
Ry = PPh \ H
ReDim TileIndex(Rx * Ry)
Dim MM As Integer
For MM = 0 To TileIndex.GetLength(0) - 1 Step 1
TileIndex(MM) = Image.FromFile("C:\Users\Joseph Maxwell\Desktop\Animations\Bounce\Ball4.bmp")
Next
ReDim Preserve TileRtang(Rx * Ry)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Draw_Grid()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Z > -10 Then
PaintPad.Size = New Size(PaintPad.Width - W, PaintPad.Height - H)
H = PaintPad.Width / Rx
W = PaintPad.Height / Ry
Button1.Text = H
Button2.Text = W
End If
If Z < -10 Then Z = -10
Z -= 1
Me.Invalidate()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Z < 10 Then
PaintPad.Size = New Size(PaintPad.Width + W, PaintPad.Height + H)
H = PaintPad.Width / Rx
W = PaintPad.Height / Ry
End If
If Z > 10 Then Z = 10
Z += 1
Me.Invalidate()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'If PaintPad.Location.Y > 0 Then
' PaintPad.Location = New Point(PaintPad.Location.X, 0)
'Else
PaintPad.Location = New Point(PaintPad.Location.X, PaintPad.Location.Y + W)
'End If
Me.Invalidate()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'If PaintPad.Location.Y <= Me.Height - PaintPad.Width Then
' PaintPad.Location = New Point(PaintPad.Location.X, -5000 + (Me.Height - 100))
'Else
PaintPad.Location = New Point(PaintPad.Location.X, PaintPad.Location.Y - W)
'End If
Me.Invalidate()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'If PaintPad.Location.X > 0 Then
' PaintPad.Location = New Point(0, PaintPad.Location.Y)
'Else
PaintPad.Location = New Point(PaintPad.Location.X + H, PaintPad.Location.Y)
'End If
Me.Invalidate()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'If PaintPad.Location.X <= Me.Width - PaintPad.Width Then
' PaintPad.Location = New Point(-5000 + (Me.Width - 100), PaintPad.Location.Y)
'Else
PaintPad.Location = New Point(PaintPad.Location.X - H, PaintPad.Location.Y)
'End If
Me.Invalidate()
End Sub
End Class
Im Not sure whats Causing My Problem But Im Sure Button1 click or Button2 click is a good place to start. PLs Help
Re: Creating A Grid For 2d Graphics
You posted this in the wrong forum, this is for VB6/classic, .Net has its own. I'll inform adminstrators so they can move it for you.
Re: Creating A Grid For 2d Graphics
That's too much code and too vague a description I'm afraid. Please provide a more detailed description of what you're trying to achieve, exactly what you do when things go wrong and exactly what goes wrong.