2012-10-11 52 views
6

Soy estudiante y soy nuevo por aquí. Tengo un proyecto de curso para hacer un programa similar a Paint. Tengo una clase base Forma con DrawSelf, contiene ect. métodos y clases para Rectangle, Ellipse y Triangle por ahora. También tengo otros dos clasificados DisplayProccesor que es clase para el dibujo, y DialogProcessor, que controla el diálogo con el usuario. Estos son requisitos para el proyecto.Al girar la forma, se mantiene junto con la rotada

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

Y Aquí `el otro:

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

lo tanto, quiero hacer girar una forma, que es seleccionado por el usuario. Intento así. Se gira, pero me sale esto: 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(); 
    } 
} 

Respuesta

1

¡Lo he guardado! El problema es que dibujo la selección para cada forma y cuando giro la forma, la selección permanecía sin rotar. He hecho la misma transformación que en DrawSelf método para la selección y todo estaba bien! ¡Aclamaciones!

2

estoy teniendo dificultades para interpretar su pregunta. Supongo que cuando giras una primera forma y la dibujas. Luego dibuja otra forma, la segunda forma también se gira.

Esto se debe a que, todo el método DrawSelf está trabajando con la misma referencia de gráficos, y por lo tanto, cualquier transformación utilizada en un método afectará a todas las llamadas sucesivas en el mismo contexto.

Puede resolver esto simplemente llamando al método Graphics.ResetTransform al final de cada DrawMe Metid.

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

He editado mi pregunta. Espero que ahora esté más claro. – vortex

Cuestiones relacionadas