Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When rotating shape, it stays together with the rotated one

I'm a student and i'm new around here. I have a course project to make a Paint-like program. I have a base class Shape with DrawSelf, Contains ect. methods and classes for Rectangle, Ellipse and Triangle for now. Also i have two other classed DisplayProccesor which is class for drawing, and DialogProcessor, which controls the dialog with the user. Theese are requirements for the project.

public class DisplayProcessor
{

    public DisplayProcessor()
    {
    }

    /// <summary>
    /// List of shapes
    /// </summary>
    private List<Shape> shapeList = new List<Shape>();
    public List<Shape> ShapeList
    {
        get { return shapeList; }
        set { shapeList = value; }
    }

    /// <summary>
    /// Redraws all shapes in shapeList
    /// </summary>
    public void ReDraw(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        Draw(e.Graphics);
    }

    public virtual void Draw(Graphics grfx)
    {
        int n = shapeList.Count;
        Shape shape;

        for (int i = 0; i <= n - 1; i++)
        {
            shape = shapeList[i];
            DrawShape(grfx, shape);
        }
    }

    public virtual void DrawShape(Graphics grfx, Shape item)
    {
        item.DrawSelf(grfx);
    }
}

And here`s the other one:

public class DialogProcessor : DisplayProcessor
{
    public DialogProcessor()
    {
    }

    private Shape selection;
    public Shape Selection
    {
        get { return selection; }
        set { selection = value; }
    }

    private bool isDragging;
    public bool IsDragging
    {
        get { return isDragging; }
        set { isDragging = value; }
    }

    private PointF lastLocation;
    public PointF LastLocation
    {
        get { return lastLocation; }
        set { lastLocation = value; }
    }

   public void AddRandomRectangle()
    {
        Random rnd = new Random();
        int x = rnd.Next(100, 1000);
        int y = rnd.Next(100, 600);

        RectangleShape rect = new RectangleShape(new Rectangle(x, y, 100, 200));
        rect.FillColor = Color.White;

        ShapeList.Add(rect);
    }
}

So, i want to rotate one shape, which is selected by the user. I try like this. It rotates it, but i get this: http://www.freeimagehosting.net/qj3zp

public class RectangleShape : Shape
{

    public override void DrawSelf(Graphics grfx)
    {
        grfx.TranslateTransform(Rectangle.X + Rectangle.Width / 2, Rectangle.Y + Rectangle.Height / 2);
        grfx.RotateTransform(base.RotationAngle);
        grfx.TranslateTransform( - (Rectangle.X + Rectangle.Width / 2), -( Rectangle.Y + Rectangle.Height / 2));
        grfx.FillRectangle(new SolidBrush(FillColor), Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
        grfx.DrawRectangle(Pens.Black, Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
        grfx.ResetTransform();
    }
}
like image 400
vortex Avatar asked Oct 11 '12 17:10

vortex


1 Answers

I'm having difficulty interpreting your question. My guess is that when you rotate a first shape and draw it. Then draw another shape, the second shape is also rotated.

This is because, all the DrawSelf method are working with the same graphics reference, and therefor any transformation used on one method will affect all the successive calls on the same context.

You can solve this by simply calling the Graphics.ResetTransform method at the end of each DrawSelf methid.

public override void DrawSelf(Graphics grfx)
    {
        base.DrawSelf(grfx);

        //grfx.FillRectangle(new SolidBrush(FillColor), Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
        //grfx.DrawRectangle(Pens.Black, Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);

        grfx.TranslateTransform(Rectangle.X + Rectangle.Width / 2, Rectangle.Y + Rectangle.Height/2);
        grfx.RotateTransform(base.RotationAngle);
        grfx.TranslateTransform(-(Rectangle.X + Rectangle.Width / 2), -(Rectangle.Y + Rectangle.Height/2));
        grfx.FillRectangle(new SolidBrush(FillColor), Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
        grfx.DrawRectangle(Pens.Black, Rectangle.X,Rectangle.Y, Rectangle.Width, Rectangle.Height);
        grfx.ResetTransform();
    }
like image 57
Samy Arous Avatar answered Oct 29 '22 03:10

Samy Arous