Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey,

 

Is there a way to use ur Graphics object in another class? For example, I've 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 doesn't.

End Sub

End class

 

Oh btw, i'm getting the graphics by going to form1_paint:

myGameClass.Paint(e)

 

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

 

What's 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

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • Leaders
Posted

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?

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

when the Paint event is raised, that could be a different Graphics object returned

Oh. Good point ;).

 

"gfx = Me.CreateGraphics()"

 

I'll 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. I'll give what you said a try (computer with .net doesn't have internet).

 

-The Pentium Guy

 

Edit: Wow. WYS is NOT WYG. I typed the post in normal characters and somehow it got to italics.

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • Leaders
Posted

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 form's properties regularly?

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

On keydown, my form is redrawn.

No timer.

No changing properties, I maximize in the load procedure and that's it.

 

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

 

-The Pentium Guy

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • Leaders
Posted
So, what are you drawing in the KeyDown event?

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

Unless I am mistaken you can use the technique I use for GDI and DirectX objects. I don�t like to have my paint function know what needs to be done to draw and I always want to be able to draw everywhere. Multiple forms, Bitmaps or basicly everything that has a graphics object.

 

So how do I do this?

 

I create a class that hold all parameters for the particular shape Let�s say an regular N-Gon

 

Properties Diameter of the containing circle, Sides, Pen, Edge color and fillcolor , . This of course can be exended to the inner circle and so on�� but we keep it simple

 

Then I create a sub named draw that take the graphics object as parameter.

Here I explain how the shape should be drawn.

 

I also have a container Class that has the ability to contain the above class.

 

Yes it is basically an araylist that also has the Draw sub and also takes the graphics bject as a parameter.

 

In the draw sub I just call every shape that�s in my arraylist and call their draw method.

 

This has the advantage that you can even place containers into other containers and that you can call the root container or any of it�s children if you only want to draw a partial image.

 

Having a visible property is always useful.

 

I would give you an example but I have only a very complex vector world map drawing system here with me that�s far from completed so you understand this would make a lousy example.

  • Leaders
Posted

You could create an empty bitmap, and do all of your drawing onto that.

Then, when it's time to display what you have drawn, you just use the Graphics object that draws to your 'display', and then .DrawImage. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

All this seems redundant just for one small thing :). I've 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 I'm using right now works.

 

-The Pentium Guy

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...