Question How to draw an image from a collection of shapes?

vbDave

New member
Joined
Jan 23, 2010
Messages
2
Location
Illinois
Programming Experience
10+
I am fairly new to .NET but have experience with VB6 programming (non-graphics). I plan to write a graphics program to draw a graph and suspect that I am already headed down the wrong path.

My question has to do with the best method to draw varying graphics content. I phrase it that way to contrast the situation with one in which you know in advance what the final image will look like. For example, I will be adding and removing different type of basic shapes (points, lines, text) dynamically. The way I envision doing this is to create a general drawing object type whose definition I can overload.

Class GraphicsTask (taskType, point1, point2, color, font, string)
[point1 and point2 might be replaced by rect1]

The taskType would be an enum with members “point”, “line” or “string”. Only a subset of the other parameters would be necessary, depending on what is being drawn. For example, if I am drawing a point, I would set taskType = “point” and would pass only point1 and color. The constructors for GraphicsTask would look like this:

For a point:
Public Sub New(ByVal tsk As Integer, ByVal rect As Rectangle, ByVal c As Color)
[rect is used since I want to be able to control the size of the point draw]

For a line:
Public Sub New(ByVal tsk As Integer, ByVal pt1 As Point, ByVal pt2 As Point, ByVal c As Color, ByVal w As Integer)

For a string:
Public Sub New(ByVal tsk As Integer, ByVal pt1 As PointF, ByVal c As Color, ByVal fnt As Font, ByVal s As String)

As I build up the drawing, I would draw a shape, then add a corresponding GraphicsTask object to a collection. When the form’s (or control’s) paint event is fired, I would run a method called “ProcessGraphicsTasks” which would iterate through the collection.

While I think this approach will work, I wonder is there isn’t an easier way to accomplish this? It seems like this would be a pretty common requirement and perhaps a simpler mechanism exists. Again, I do want to be able to build my image from a collection of shapes and have the ability to selectively and dynamically add shapes to and remove shapes from the collection.
 
It sounds like a pretty good design to me. If you want to edit the shapes selectively, have don't much option but to keep some kind of collection. But generic collections such as Lists and Stacks are pretty easy to manage in VB.Net.

An alternative might be to store graphic "objects" instead of "tasks". I mean, you could draw each shape into a separate bitmap and iterate through the stored bitmaps in the Paint event. As long as you put each bitmap into the collection at its final size, you can take advantage of unscaled drawing (Graphics.DrawImageUnscaled) which is more efficient than scaling (e.g. with DrawImage). It would obviously need more memory, but it could be a bit less complicated than interpreting a list of tasks.

Later you would be able to apply transformations like scaling, rotating, fading etc. to each bitmap separately. In effect it would be the basis of a layered drawing system.

bye, Vic
 
Back
Top