I have write a c# class, which contains graphics inside. this is how I build the class and draw the rectangle. It works perfectly:
public Graphics shape;
public Rectangle rc;
// constructor
public CLASS_NAME(Graphics formGraphics)
{
this.shape = formGraphics;
}
public void draw(int x, int y, int w, int h)
{
SolidBrush myBrush = new SolidBrush(Color.Red);
this.rc = new Rectangle(x, y, w, h);
this.shape.FillRectangle(myBrush, rc);
myBrush.Dispose();
}
then I wanted to add a new method to the object in order to change the color, but when I call this nothing happens:
public void change_color()
{
SolidBrush myBrush = new SolidBrush(Color.Yellow);
this.shapeFillRectangle(myBrush, rc);
myBrush.Dispose();
}
I also tried: rc.Fill =
but VS doesn't recognize rc.Fill
as a valid method.
- THE ERROR I GET IS: it says that in the
change()
method, this line:this.shapeFillRectangle(myBrush, rc);
has a parameter that isn't valid.
OK, let's start with a 'drawRectangle' class. It has enough data to create a simple
Pen
, holds aRectangle
and also a reference to theControl
it will draw on.I have added a
ToString
override, so we can display it with all its properties in, say aListBox
..version 1
Next we need a list of those rectangles:
Now let's add them in a loop in a button click:
Note how I invalidate the control I want them painted on by
Invalidating
it! (You can use theForm
as well, asForm
inherits fromControl
..)For this to work we need to code the
Paint
event of each control that needs to paint some of the rectangles; I use only aPanel drawPanel1
:Now we can change any of our
DrawRectangles
, maybe in another button click:Update:
The above class was a simple start to show how to encapsulate the things a 'Rectangle' class would need; it was not meant to be perfect!
Here is one flaw it has: It doesn't really care enough about the best way to spread responsibilities. It put the burdon of drawing the rectangles on the controls and if you have more complex drawing code and more controls each of them would have to learn the more complex code. This is not good. Instead the responsibility should stay with the Rectangle class. The control should only tell them to draw themselves..
Here is an updated class that will do just that. As a more complex drawing it will be able to draw filled rectangles as well..:
version 2
It uses a second color to determine the filling. (I didn't add the fill color the the
ToString
method.) It compares the color with the special color valuesColor.Empty
to determine what should and shouldn't be drawn.The loop to create the new rectangles may now include the fill color. If it doesn't, the old constructor will be called, which now sets the fill color to
Color.Empty
.Here is how simple the
Paint
event gets:To fill some rectangle we can now write:
Aside:
A note an color comparison: It is not obvious, but while the color
Color.Empty
really is just 'transparent black' (0,0,0,0), color comparison is special:NamedColors
as well asKnownColors
, includingColor.Empty
always compare false to normal colors. To make a true color comparison one would have to cast to a 'normal'Color
:Therefore the comparison in the
Draw
code is safe.