Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.Visualization/3.2/LineShape.cs @ 2716

Last change on this file since 2716 was 1964, checked in by mstoeger, 16 years ago

moved the canvas and the basic types of shapes to their own namespace. #498

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