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.
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.