Click to See Complete Forum and Search --> : overriding OnPaint method


HeatherLBA
12-04-2004, 06:19 PM
Hello,

I'm trying to write the code for a game similar to LoLo in VB.NET. I have an array of Squares that show on the screen, but I can't make Lolo (here she's called Player and she is a subclass of Square) show up.

I know I'm close but missing something simple. I don't know if it's the Invalidate() method needs to be called or what.

If someone had a sec to look at it, I would greatly appreciate it. The onpaint methods are driving me crazy.

The code is below, and is also attached as a text file.

Kind regards,
Heather

*****************************
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace Game

Public Class MainForm
Inherits System.Windows.Forms.Form
Public Const BoardWidth As long = 20
Public Const BoardHeight As long = 15
Public Dim Shared Board(BoardWidth,BoardHeight) As Square
Protected score as long = 0
Private PlayerX as long = 10
Private PlayerY As Long = 10
Private Player As Player = New Player()


Public Shared Sub Main
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub

Public Sub New()
MyBase.New
'
' The Me.InitializeComponent call is required for Windows Forms designer support.
'
Me.InitializeComponent
'
' TODO : Add constructor code after InitializeComponents
'
For i As Integer = 0 To BoardWidth - 1
For j As Integer = 0 To BoardHeight - 1
board(i,j)= New Square()
Next j
Next i
board(PlayerX,PlayerY) = Player
End Sub

#Region " Windows Forms Designer generated code "
' This method is required for Windows Forms designer support.
' Do not change the method contents inside the source code editor. The Forms designer might
' not be able to load this method if it was changed manually.
Private Sub InitializeComponent()
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(400,400)
Me.Name = "MainForm"
Me.Text = "Make Your Own Game"
AddHandler Me.KeyPress, AddressOf keypressed
End Sub
#End Region
Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
' The keypressed method uses the KeyChar property to check
' whether the ENTER key is pressed.

' If the ENTER key is pressed, the Handled property is set to true,
' to indicate the event is handled.

If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
e.Handled = True
' do game specific actions
End If
' or you can use switch
End Sub
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pevent)
For i As Integer = 0 To BoardWidth-1
For j As Integer = 0 To BoardHeight-1
board(i,j).paint(pevent.Graphics,10+ Square.SquareWidth * i,30 + Square.SquareHeight * j)
Next j
Next i
End Sub
End Class

Public Class Square
Public Const SquareWidth As Integer = 10
Public Const SquareHeight As Integer = 10

Public Sub New()

End Sub

Public Sub paint(ByRef g As System.Drawing.Graphics, ByVal x As Integer,ByVal y As Integer)
Dim whiteBrush As New SolidBrush(Color.White)
g.FillRectangle(whiteBrush,x, y, SquareWidth, SquareHeight)
End Sub
End Class

Public Class Player
Inherits Square
Public shadows Sub paint(ByRef g As System.Drawing.Graphics, ByVal x As Integer,ByVal y As Integer)
Dim grayBrush As New SolidBrush(Color.Gray)
g.FillRectangle(grayBrush,x, y, SquareWidth, SquareHeight)
End Sub
End Class
End Namespace

Supercharged
12-08-2004, 05:55 PM
Hi, had a quick look, but it’s difficult not really knowing what LoLo is!
Basically the paint sub for Player is never called, For test purposes, I added a call to the sub with 0,0 just to make her appear

Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pevent)
For i As Integer = 0 To BoardWidth - 1
For j As Integer = 0 To BoardHeight - 1
Board(i, j).paint(pevent.Graphics, 10 + Square.SquareWidth * i, 30 + Square.SquareHeight * j)
Next j
Next i
Board(PlayerX, PlayerY).paint(pevent.Graphics, 0, 0)
End Sub

I would recommend a read of the article below for a better understanding of GDI+

http://www.bobpowell.net/manipulate_graphics.htm

Good Luck!