2012-10-11 18 views
6

Tôi là sinh viên và tôi mới ở đây. Tôi có một dự án khóa học để tạo ra một chương trình giống như Paint. Tôi có một lớp cơ sở Hình dạng với DrawSelf, Chứa ect. phương pháp và lớp học cho hình chữ nhật, Ellipse và Triangle cho bây giờ. Ngoài ra tôi còn có hai lớp khác DisplayProccesor là lớp để vẽ và Trình xử lý đối thoại, điều khiển hộp thoại với người dùng. Đây là những yêu cầu cho dự án.Khi xoay hình dạng, nó vẫn giữ nguyên với hình xoay

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

Và here`s kia một:

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

Vì vậy, tôi muốn xoay một hình dạng, được chọn bởi người sử dụng. Tôi cố gắng như thế này. Nó xoay nó, nhưng tôi nhận được điều này: 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(); 
    } 
} 

Trả lời

1

Tôi đã an ủi nó! Vấn đề là tôi vẽ lựa chọn cho mỗi hình dạng và khi tôi xoay hình dạng, lựa chọn không được bảo vệ. Tôi đã thực hiện chuyển đổi tương tự như trong phương thức DrawSelf cho lựa chọn và mọi thứ đều ổn! Chúc mừng!

2

Tôi đang gặp khó khăn khi giải thích câu hỏi của bạn. Tôi đoán là khi bạn xoay một hình đầu tiên và vẽ nó. Sau đó vẽ một hình dạng khác, hình dạng thứ hai cũng được xoay.

Điều này là do, tất cả phương thức DrawSelf đều làm việc với cùng tham chiếu đồ họa và bất kỳ phép chuyển đổi nào được sử dụng trên một phương pháp sẽ ảnh hưởng đến tất cả các cuộc gọi liên tiếp trên cùng một ngữ cảnh.

Bạn có thể giải quyết vấn đề này bằng cách chỉ cần gọi phương thức Graphics.ResetTransform ở cuối mỗi methid DrawSelf.

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(); 
    } 
+0

Tôi đã chỉnh sửa câu hỏi của mình. Tôi hy vọng là rõ ràng hơn bây giờ. – vortex