旋转绘制的对象



所以我画了一些对象,圆,正方形或甚至线。这是我用来绘制图像的代码:

Graphics surface = this.splitContainer1.Panel2.CreateGraphics();
Pen pen1 = new Pen(ColorR.BackColor, float.Parse(boxWidth.Text));
switch (currentObject)
{
case "line":
    if (step == 1)
    {
        splitContainer1.Panel2.Focus();
        one.X = e.X;
        one.Y = e.Y;
        boxX.Text = one.X.ToString();
        boxY.Text = one.Y.ToString();
        step = 2;
    }
    else
    {
        two.X = e.X;
        two.Y = e.Y;
        boxX2.Text = two.X.ToString();
        boxY2.Text = two.Y.ToString();
        surface.DrawLine(pen1, one, two);
        step = 1;
    }
    break;
case "circle":
    if (step == 1)
    {
        boxX.Text = e.X.ToString();
        boxY.Text = e.Y.ToString();
        step = 2;
    }
    else
    {
        int tempX = int.Parse(boxX.Text);
        int tempY = int.Parse(boxY.Text);
        int tempX2 = e.X;
        int tempY2 = e.Y;
        int sideX, sideY;
        if (tempX > tempX2)
        {
            sideX = tempX - tempX2;
        }
        else
        {
            sideX = tempX2 - tempX;
        }
        if (tempY > tempY2)
        {
            sideY = tempY - tempY2;
        }
        else
        {
            sideY = tempY2 - tempY;
        }
        double tempRadius;
        tempRadius = Math.Sqrt(sideX * sideX + sideY * sideY);
        tempRadius *= 2;
        bWidth.Text = bHeight.Text = Convert.ToInt32(tempRadius).ToString();
        surface.DrawEllipse(
           pen1,
           int.Parse(boxX.Text) - int.Parse(bWidth.Text) / 2, 
           int.Parse(boxY.Text) - int.Parse(bHeight.Text) / 2,
           float.Parse(bWidth.Text), float.Parse(bHeight.Text));
        step = 1;
    }
    break;
case "square":
    if (step == 1)
    {
        boxX.Text = e.X.ToString();
        boxY.Text = e.Y.ToString();
        step = 2;
    }
    else if (step == 2)
    {
        int tempX = e.X;
        if (tempX > int.Parse(boxX.Text))
        {
            bWidth.Text = (tempX - int.Parse(boxX.Text)).ToString();
        }
        else
        {
            bWidth.Text = (int.Parse(boxX.Text) - tempX).ToString();
        }
        step = 3;
    }
    else
    {
        int tempY = e.Y;
        if (tempY > int.Parse(boxY.Text))
        {
            bHeight.Text = (tempY - int.Parse(boxY.Text)).ToString();
        }
        else
        {
            bHeight.Text = (int.Parse(boxY.Text) - tempY).ToString();
        }
        surface.DrawRectangle(
            pen1,
            int.Parse(boxX.Text),
            int.Parse(boxY.Text),
            int.Parse(bWidth.Text),
            int.Parse(bHeight.Text));
        step = 1;
    }
    break;
}

因此,在我绘制图像之后,我希望能够选择一个图形,例如,改变颜色或旋转它。但我似乎不知道该怎么做。

我建议定义一个基本的抽象形状类,它具有所有形状都应该提供的方法,例如在图形对象上绘制自身的方法,表示点是否在其中/应该可以选择它的方法,以给定数量旋转它的方法和改变颜色的方法。

一旦你有了你的形状类,那么你必须弄清楚如何填充每个派生形状的方法。对于绘图,你已经有了代码。选择它时,这取决于形状。对于像圆这样的东西,这很简单,只需计算圆心和点击点之间的距离,对于像线这样的东西,这就比较难了,因为你不想让用户精确地点击它。

剩下的就是旋转和改变颜色。改变颜色很容易,只要在Shape类上有一个Color属性,然后当你绘制形状时,使用该颜色来创建画笔或钢笔。

关于旋转,请看Graphics.RotateTransform


public abstract class Shape
{
    public Color Color { get; set; }
    public float Rotation { get; set; }
    public Point Position { get; set; }
    public Shape(Color color, float rotation, Point position)
    {
        Color = color;
        Rotation = rotation;
        Position = position;
    }
    public void ChangeRotation(float amount)
    {
        Rotation += amount;
    }
    public abstract void Draw(Graphics graphics);
    public abstract bool WithinBounds(Point point);
}
public class Circle : Shape
{
    public float Radius { get; set; }
    public Circle(Color color, float rotation, Point position)
        :base(color, rotation, position)
    {
    }
    public override void Draw(Graphics graphics)
    {
    }
    public override bool WithinBounds(Point point)
    {
        if (Math.Sqrt(Math.Pow(point.X - Position.X, 2) + Math.Pow(point.Y - Position.Y, 2)) <= Radius)
            return true;
        else
            return false;
        // Note, if statement could be removed to become the below:
        //return Math.Sqrt(Math.Pow(point.X - Position.X, 2) + Math.Pow(point.Y - Position.Y, 2)) <= Radius;
    }
}

看一下图形对象的RotateTransform方法。还有一个TranslateTransform方法

最新更新