I've been working on a simple graphics project where I can draw shapes using button and edit them (Resize and other stuffs). I've been able to draw different shapes, but my problem is that I cannot draw the same shape again. It only updates the parameter of the previous same shape.
For example, I tried to draw a rectangle and then I want to draw again in a different size, the rectangle changes it size rather than creating a new one.
Here's my code:
Shape Class
class ShapeClass
{
public int x { get; set; }
public int y { get; set; }
public int width { get; set; }
public int height { get; set; }
public Pen color { get; set; }
public string Shape { get; set; }
}
Draw Class
class Draw:ShapeClass
{
public void DrawRectangle(PaintEventArgs e)
{
e.Graphics.DrawRectangle(color, new Rectangle(x, y, width, height));
}
public void DrawSquare(PaintEventArgs e)
{
e.Graphics.DrawRectangle(color, new Rectangle(x, y, width, height));
}
public void DrawCircle(PaintEventArgs e)
{
e.Graphics.DrawEllipse(color, new Rectangle(x,y,width,height));
}
public void DrawEllipse(PaintEventArgs e)
{
e.Graphics.DrawEllipse(color, new Rectangle(x, y, width, height));
}
Paint Events
public void PaintRect(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = rX;
d.y = rY;
d.width = rW;
d.height = rH;
rC = new Pen(Color.Red, 2);
d.color = rC;
d.DrawRectangle(e);
}
public void PaintSquare(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = sX;
d.y = sY;
d.width = sW;
d.height = sH;
d.color = new Pen(Color.Red, 2);
//d._Rectangle.Add(new Rectangle(sX,sY,sW,sH));
d.DrawSquare(e);
}
public void PaintCircle(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = cX;
d.y = cY;
d.width = cW;
d.height = cH;
d.color = new Pen(Color.Red, 2);
d.DrawCircle(e);
}
public void PaintEllipse(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = eX;
d.y = eY;
d.width = eW;
d.height = eH;
d.color = new Pen(Color.Red, 2);
d.DrawEllipse(e);
}
ShapeClassto see how you define the various location and size parameters. - CoreycX,cY,cWandcH? - CoreyPaintevent is raised, everything you have previously drawn is erased. If you want multiple shapes drawn then you have to store those shapes in a list at class level and then draw everything in that list on eachPaintevent. You should have onePaintevent handler that draws every item in the list and then adding a new shape would be a case of adding an item to that list and then forcing a repaint by callingRefresh. - jmcilhinney