2011-11-16 167 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方法。