Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/LineShape.cs @ 1240

Last change on this file since 1240 was 1240, checked in by mstoeger, 15 years ago

Transformations on shapes are possible outside of the Draw method. (#424)

File size: 3.3 KB
Line 
1using System.Drawing;
2using System.Drawing.Drawing2D;
3
4namespace HeuristicLab.Visualization {
5  public class LineShape : IShape {
6    private IShape parent;
7    private RectangleD boundingBox;
8
9    private Color color;
10    private int thickness;
11    private DrawingStyle drawingStyle;
12
13    private Pen pen;
14
15    /// <summary>
16    /// Initializes the LineShape.
17    /// </summary>
18    /// <param name="x1">x coordinate of left lineEndPoind</param>
19    /// <param name="y1">y coordinate of left lineEndPoind</param>
20    /// <param name="x2">x coordinate of right lineEndPoind</param>
21    /// <param name="y2">y coordinate of right lineEndPoind</param>
22    /// <param name="color">color for the LineShape</param>
23    /// <param name="thickness">tickness of the line in pixels</param>
24    /// <param name="drawingStyle">drawing style of the line (solid, dashed, dotted,...)</param>
25    public LineShape(double x1, double y1, double x2, double y2, Color color, int thickness, DrawingStyle drawingStyle) {
26      this.boundingBox = new RectangleD(x1, y1, x2, y2);
27      this.LSColor = color;
28      this.LSThickness = thickness;
29      this.LSDrawingStyle = drawingStyle;
30    }
31
32    public RectangleD BoundingBox {
33      get { return boundingBox; }
34    }
35
36    public RectangleD ClippingArea {
37      get { return Parent.ClippingArea; }
38    }
39
40    public Rectangle Viewport {
41      get { return Parent.Viewport; }
42    }
43
44    public IShape Parent {
45      get { return parent; }
46      set { parent = value; }
47    }
48
49    public double Y1 {
50      get { return boundingBox.Y1; }
51      set { boundingBox.Y1 = value; }
52    }
53
54    public double Y2 {
55      get { return boundingBox.Y2; }
56      set { boundingBox.Y2 = value; }
57    }
58
59    public double X1 {
60      get { return boundingBox.X1; }
61      set { boundingBox.X1 = value; }
62    }
63
64    public double X2 {
65      get { return boundingBox.X2; }
66      set { boundingBox.X2 = value; }
67    }
68
69    /// <summary>
70    /// Draws the LineShape.
71    /// </summary>
72    /// <param name="graphics">graphics handle to draw to</param>
73    public void Draw(Graphics graphics) {
74      Rectangle screenRect = Transform.ToScreen(boundingBox, Parent.Viewport, Parent.ClippingArea);
75
76      graphics.DrawLine(GetPen(), screenRect.Left, screenRect.Bottom, screenRect.Right, screenRect.Top);
77    }
78
79    private Pen GetPen() {
80      if (pen == null) {
81        pen = new Pen(LSColor, LSThickness);
82
83        switch (LSDrawingStyle) {
84          case DrawingStyle.Dashed:
85            pen.DashStyle = DashStyle.Dash;
86            break;
87          default:
88            pen.DashStyle = DashStyle.Solid;
89            break;
90        }
91      }
92
93      return pen;
94    }
95
96    private void DisposePen() {
97      if (pen != null) {
98        pen.Dispose();
99        pen = null;
100      }
101    }
102
103    public Color LSColor {
104      get { return color; }
105      set {
106        color = value;
107        DisposePen();
108      }
109    }
110
111    public int LSThickness {
112      get { return thickness; }
113      set {
114        thickness = value;
115        DisposePen();
116      }
117    }
118
119    public DrawingStyle LSDrawingStyle {
120      get { return drawingStyle; }
121      set {
122        drawingStyle = value;
123        DisposePen();
124      }
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.