2011-11-16 71 views
0

所以我画了几个物体,圆形正方形甚至是线条。这是我用来绘制图像的代码:旋转画出的物体

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; 
} 

所以之后我画的图像,我希望能够选择一个数字, - 例如 - 改变颜色或旋转。但我似乎无法弄清楚如何去做。

+0

这无关你的问题,但如果你有兴趣在提高你的代码,看看这里:[http://stackoverflow.com/questions/126409/ways-to -eliminate开关功能于代码(http://stackoverflow.com/questions/126409/ways-to-eliminate-switch-in-code) –

回答

1

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

一旦你有了你的形状类,那么你必须弄清楚如何填充每个派生形状的方法。对于绘图,你已经有了代码。为了选择它,这将取决于形状。对于像圆圈这样的事情来说,这相当简单,只需计算圆心和点击点之间的距离即可,因为您不希望用户必须完全点击它,因为它更难。

留下旋转和改变颜色。更改颜色很简单,只需在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; 
    } 
} 
0

查看Graphics对象的RotateTransform方法。还有一个TranslateTransform方法。