Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Drawing Library

0.00/5 (No votes)
10 Dec 2007 5  
A library for creating shapes and developing tools.

Screenshot - Painter.JPG

Introduction

This is a generic library for drawing (work in progress). It is composed of some base tools and shapes. It allows you to draw shapes that you want to create, and develop tools to manage your figures.

Using the code

This piece of code describes the implementation of a derived class from Shape and a a derived class from Tool.

public class Ellipse : Shape
{
    public Ellipse()
    {
        Geometric.AddEllipse(new System.Drawing.Rectangle(0, 0, 1, 1));
    }

    public Ellipse(Ellipse ellipse) : base(ellipse)
    {
    }

    public override object Clone()
    {
        return new Ellipse(this);
    }
}
// ....


public class Select : Tool    
{
    .....

    // Override IActions Interface functions

    public override void MouseDown(IDocument document, MouseEventArgs e)
    {
        base.MouseDown(document, e);

        if (SelectShape(document.Shapes, e.Location) == HitPositions.None)
            Select.UnselectAll(document.Shapes);

        if (SelectedShapes != null)
            SelectedShapes(this, Select.GetSelectedShapes(document.Shapes));
    }

    public override void MouseUp(IDocument document, MouseEventArgs e)
    {
        base.MouseUp(document, e);

        document.ActiveCursor = Cursors.Default;
    }
    
    .....
}

It is very important to implement the copy constructor and the function Clone for every new shape, while it is convenient to override the IActions interface during the implementation of a new tool.

Main schemes

In the documentation, there are these schemes:

  • Orange color describes a hot spot class (interface or abstract class usually).
  • Yellow color describes active classes.
  • Blue color describes passive classes (data containers or structs usually).
  • Light grey color describes classes of the framework but not important in the current scheme.
  • Dark grey color describes classes external to the framework.

This scheme represents the actual shapes hierarchy:

Shapes hierarchy

This scheme represents the actual tools hierarchy:

Tools hierarchy

This scheme represents the communication channel between the drawing panel, tools, and shapes:

Hooking Tools-Shapes

The IActions interface contains all the panel actions used by the tools; for example, mouse down and mouse move, and also the paint function. IDocument is an interface, but it is blue because it is only a connection point between tools and shapes, and so it is a passive class.

The following scheme represents the relation between transformers and shapes. Transformers are used to handle shapes moving, so if you want to add any new move, you must only derive two classes, Transformer and CompositeTransformer, and not all the classes in the shapes hierarchy.

Hooking Transformers-Shapes

This scheme represents the actual appearances hierarchy:

Appearances hierarchy

Other schemes are available in the documentation.

Points of interest

To rotate a shape, choose the rotate tool and select the shape. Do a mouse click down and move. To rotate relative to a non-center point, select a shape, click the Ctrl key, do mouse click down, and move.

Rotate example

To deform a shape, change the property Marked to true, and choose the deform tool, do a mouse click down on a marker, and move. Try to group shapes and do the same operations above.

Deform example

Serialization

The serialization operations are made by XmlSerializer. The performance is not very great.

Updates

  • Zoom functionality.
  • Grid on panel.
  • DrawFreeLine tool with possibility to decide the minimum offset between two consecutive points.

Grid example and DrawFreeLine tool

See the Layout options for the Bring To Front and Send To Back functionalities.

Last updates

Added possibility to move shapes with arrows and Control key.

Bug fixes

Modified setter methods of the Text shape to maintain the text state after rotation.

Known bugs

  • Deform tool is not precise on shape border markers.
  • A problem with resize tool and grid enabled.
  • The Undo-Redo mechanism is not precise.

Future developments

  • Improvement of tools (for example, resize).
  • Improvement of performance.
  • Development of other tools.

References

For documentation:

For object-oriented and color theory and other software development information:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here