Dynamic Pens Brushes

In my application, I need to change pen (and brush) properties depending on the view I’m painting to.



Since it it is not allowed to change pen/brush properties once assigned, I now create a new Pen (and Brush) in Paint(), for example:



public override void Paint(Graphics g, GoView view)

{

Pen pen = new Pen(GetColor(view), GetWidth(view));

pen.DashStyle = GetDashStyle(view);

Pen = pen;

base.Paint(g, view);

}



I noticed that the Pen interning does re-use pens with the same properties, but it still seems a little wasteful to be creating new pens in each Paint().



Any ideas on how to best go about this ?



It would seem, at least for the code you provide… you could allocate a new Pen when whatever is causing GetColor() or GetWidth() to change… I would presume that neither of those change very frequently.



in general, setting this.Pen in the Paint method seems like a really bad idea to me.



It looks like you have the View being responsible for a Document / GoObject property. What happens if you have 2 views onto the same Document, and one view wants Pen to be Red and the other wants it to be Blue?

One view could be for a regular color display, and another could be for a monochrome printer.



Each view requires painting with different pens; in this case different colors and possibly widths.



Sofar, I have not seen simultaneous paint operations collide, but I am not 100% sure they cannot occur.

I guess I have to add a lock to GoView.PaintObjects to prevent that from happening.



Do you see another solution to this ?

If printing is the main use, you could certainly clone the entire document, set the Pens/Brushes in that new doc, then print it and discard it.

I’m afraid cloning documents is not trivial in our application.

We typically have a couple thousand of documents, with a lot of references between them.



I guess there is no other way than to assign new pens for the relatively rare printing views, and clean up when the printing is done.

While the pens are set up for printing, no other painting of the affected document can take place.



The regular screen views won’t have to assign pens on every paint this way.



Maybe the GO framework could support different rendering modes for all objects in the future ?

Perhaps you could use the static GoShape methods for drawing those shapes that you want to draw differently.

That does mean overriding Paint on those shapes…

I want the same painting, just with a different pen and/or brush.



I guess another way to avoid creating (and assigning) new pens on each Paint() would be to override the get_Pen property for all my GoShape subclasses.



I would have to manage the pens myself, then.

Do you see any potential issues with that ?