2012-10-11 8 views
6

Sono uno studente e sono nuovo qui intorno. Ho un progetto di corso per realizzare un programma simile a Paint. Ho una classe base Forma con DrawSelf, Contiene ect. metodi e classi per Rectangle, Ellipse e Triangle per ora. Inoltre ho altri due classificati DisplayProccesor che è la classe per il disegno e DialogProcessor, che controlla la finestra di dialogo con l'utente. Questi sono i requisiti per il progetto.Quando si ruota la forma, rimane insieme a quella ruotata

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

E Qui `s l'altro:

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

Quindi, voglio far ruotare una forma, che viene selezionata dall'utente. Ci provo così. E 'la ruota, ma ottengo questo: 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(); 
    } 
} 

risposta

1

L'ho risolto! Il problema era che disegnavo la selezione per ogni forma e quando ruotavo la forma, la selezione rimaneva non ruotata. Ho fatto la stessa trasformazione del metodo DrawSelf alla selezione e tutto andava bene! Saluti!

2

Sto avendo difficoltà ad interpretare la tua domanda. La mia ipotesi è che quando si ruota una prima forma e si disegna. Quindi disegna un'altra forma, la seconda forma viene anche ruotata.

Questo perché, tutti i metodi DrawSelf funzionano con lo stesso riferimento grafico e pertanto qualsiasi trasformazione utilizzata su un metodo influirà su tutte le chiamate successive nello stesso contesto.

È possibile risolvere questo problema semplicemente chiamando il metodo Graphics.ResetTransform alla fine di ogni 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

Ho modificato la mia domanda. Spero sia più chiaro ora. – vortex

Problemi correlati