Using Graphics object in another class

ThePentiumGuy

Well-known member
Joined
May 21, 2003
Messages
1,113
Location
Boston, Massachusetts
Hey,

Is there a way to use ur Graphics object in another class? For example, Ive done something like

public class Gameclass
Public shared gfx as Graphics

Public Sub Paint(e as painteventargs)
gfx = e.graphics
gfx.drawimage("etc...") <-- this line works ... but
end sub

end class

Public class clsSomething

Public sub Paint()
gameclass.gfx.drawimage("etc...") <-- this one doesnt.
End Sub
End class

Oh btw, im getting the graphics by going to form1_paint:
myGameClass.Paint(e)

Im getting an argument error (In clsSomething.Paint), although Im using the exact same arguments for DrawImage in GameClass as well as clsSomething.

Whats going on?
I just want to be able to draw from other classes (preferably from the graphics object in GameClass). Or should I take another route and use AddHandler in GameClass to do the same, if so - can you please remind me how to use it? Heh I need to brush up my AddHandler skills.

-The Pentium Guy
-The Pentium Guy
 
Well, when the Paint event is raised, that could be a different Graphics object returned... so, you may want to do:

gfx = Me.CreateGraphics()
in Form_Load.

In the Paint event, draw with gfx. Then, you should surely be able to draw with gfx in another class.
Do you have the image available in the clsSomething class?
What does the error say?
 
when the Paint event is raised, that could be a different Graphics object returned
Oh. Good point ;).

"gfx = Me.CreateGraphics()"

Ill give that a try. Thanks.

[Do you have the image available in the clsSomething class?]
Yep. Right there in my bin folder.

The error was some Invalid Argument error. Ill give what you said a try (computer with .net doesnt have internet).

-The Pentium Guy

Edit: Wow. WYS is NOT WYG. I typed the post in normal characters and somehow it got to italics.
 
Are you doing something that may cause the form to be redrawn?
Do you have a Timer or something on your form? Are you changing some of the forms properties regularly?
 
On keydown, my form is redrawn.
No timer.
No changing properties, I maximize in the load procedure and thats it.

So even if I hold down a key, the text flashes. It doesnt come out clearly (and yes, I have doublebuffering...etc all that stuff turned on).

-The Pentium Guy
 
You could create an empty bitmap, and do all of your drawing onto that.
Then, when its time to display what you have drawn, you just use the Graphics object that draws to your display, and then .DrawImage. :)
 
All this seems redundant just for one small thing :). Ive just thrown all my drawing code into the GameClass.Paint event and it works fine:

Public Sub New(Gameform as form)
AddHandler gameform.Paint, me.Paint
End Sub

Public Sub Paint(sender as object, e as paintevnetargs)
e.graphics.drawimage(etc)

If Not clsSomething.TheBitmapItNeedsToDraw = Nothing Then
e.graphics.drawimage(clsSomething.TheBitmapThatItneedsToDraw)
End If
End Sub

I wish Microsoft offered a simpler solution to this. But oh well, the one Im using right now works.

-The Pentium Guy
 
Back
Top