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