Design an app like MS Paint

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-05-09

Problem Statement

Designing a drawing application similar to MS Paint that allows users to create, edit, and save drawings. Focus is on object-oriented design, modular architecture, undo/redo functionality, and tool management.


✅ Functional Requirements


1.Canvas Management
•Create, resize, and clear canvas.
•Support multiple layers (optional).
•Maintain drawing history for undo/redo functionality.
2.Drawing Tools
•Brush/Pencil: free-hand drawing.
•Eraser: remove parts of drawing.
•Shapes: rectangle, circle, line, polygon.
•Text Tool: insert text.
•Fill Tool: fill color in an area.
3.Color & Style
•Select colors for stroke and fill.
•Adjust line thickness.
•Support basic styles like dashed or solid lines.
4.Undo/Redo
•Maintain a history stack for undo/redo operations.
•Each action (draw, erase, fill) is recorded as a command.
5.File Management
•Save drawing to file (e.g., PNG, JPEG).
•Load drawing from file.
6.Optional Features
•Zoom in/out.
•Multiple selection and move objects.
•Layer visibility toggle.


🔐 Constraints & Assumptions


•Focus on LLD, class design, and command handling, not GUI implementation.
•Assume a basic 2D pixel-based canvas.
•Support for concurrency (multi-user editing) is not required.


🧱 Key Components & Classes


1.Canvas Class
1 2class Canvas { 3private: 4 int width; 5 int height; 6 Pixel** pixels; // 2D array of pixels 7 stack<Command*> undoStack; 8 stack<Command*> redoStack; 9public: 10 void drawShape(Shape* shape); 11 void undo(); 12 void redo(); 13 void clear(); 14};
1.Pixel Class
1 2class Pixel { 3private: 4 int x, y; 5 Color color; 6public: 7 void setColor(Color color); 8};
1.Shape Class (Abstract)
1 2class Shape { 3protected: 4 Color strokeColor; 5 Color fillColor; 6 int strokeWidth; 7public: 8 virtual void draw(Canvas* canvas) = 0; 9};
•Derived Shapes: Line, Rectangle, Circle, Polygon, Text.
1.Tool Class (Abstract)
1 2class Tool { 3public: 4 virtual void apply(Canvas* canvas, int x, int y) = 0; 5};
•Derived Tools: Brush, Eraser, FillTool, TextTool.
1.Command Interface (For Undo/Redo)
1 2class Command { 3public: 4 virtual void execute() = 0; 5 virtual void unexecute() = 0; 6};
•Each drawing action implements Command.
1.Color Class
1 2class Color { 3private: 4 int r, g, b; 5public: 6 int getR(), getG(), getB(); 7};


🔄 Example Workflow


1.User selects Brush tool and draws a line → Brush.apply() creates a DrawLineCommand.
2.Command executed → updates Canvas and pushed onto undoStack.
3.User performs Undo → undoStack.top().unexecute() restores previous canvas state.
4.User saves canvas → serializes pixels or layers into a file format.


🧪 Expected Deliverables


•Class diagram showing Canvas, Pixel, Shape, Tool, Command, and Color.
•Implementation of drawing tools, shapes, undo/redo, and basic canvas operations.
•Test cases:
•Draw multiple shapes and undo/redo
•Erase parts of drawing
•Fill areas with color
•Save and load canvas


💡 Discussion Points


•Undo/Redo: command pattern for maintaining history.
•Extensibility: adding new tools and shapes easily.
•Performance: efficient pixel manipulation for large canvases.
•Optional: layer support and selection tools.


Solution

Comments