I'm playing around with the System.Drawing namespace and wanted to try making a basic paint program. I'm having trouble with drawing shapes, specifically, how do I show the shape the user is drawing without having to redraw the whole screen?
Currently, when the user draws on the screen, they don't see the shape they're drawing until they release the mouse button. This is annoying and makes it hard to get the shape just right.
I thought using GraphicsState and Graphics.Save()/.Restore() would fix my problem, but whenever I go to draw a new shape, it erases the screen.
I want the user to be able to see the shape as they drag the mouse around the screen.
To store each shape, I'm using an abstract shape class (aShapeData) that all shapes inherit from, and a list of shapes (List<aShapeData>) for undo/redo functionality.
Abstract aShapeData class:
public virtual void Start(Point point, Graphics graphics)
{
startPt = point;
gfxState = graphics.Save();
}
public virtual void Moved(Point point, Graphics graphics)
{
graphics.Restore(gfxState);
}
public virtual void Stop(Point point, Graphics graphics)
{
stopPt = point;
Draw(graphics);
}
LineShape class inherits from aShapeData:
public override void Moved(Point point, Graphics graphics)
{
base.Moved(point, graphics);
graphics.DrawLine(new Pen(Color.Black), startPt, point);
}
public override void Draw(Graphics graphics)
{
graphics.DrawLine(new Pen(Color.Black), startPt, stopPt);
}
The form's OnPaint event handler:
private void MainForm_Paint(object sender, PaintEventArgs e)
{
foreach (aShapeData element in currentState.shapeList)
{
element.Draw(currentState.graphics);
}
}