Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/RectangleShape.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: 1.9 KB
Line 
1using System.Drawing;
2
3namespace HeuristicLab.Visualization {
4  public class RectangleShape : IShape {
5    private IShape parent;
6    private RectangleD rectangle;
7
8    private Color color;
9    private int opacity = 255;
10
11    private Pen pen;
12    private Brush brush;
13
14    public RectangleShape(double x1, double y1, double x2, double y2, Color color) {
15      rectangle = new RectangleD(x1, y1, x2, y2);
16      this.color = color;
17    }
18
19    public RectangleD BoundingBox {
20      get { return rectangle; }
21    }
22
23    public RectangleD ClippingArea {
24      get { return Parent.ClippingArea; }
25    }
26
27    public Rectangle Viewport {
28      get { return Parent.Viewport; }
29    }
30
31    public IShape Parent {
32      get { return parent; }
33      set { parent = value; }
34    }
35
36    public RectangleD Rectangle {
37      get { return rectangle; }
38      set { rectangle = value; }
39    }
40
41    public void Draw(Graphics graphics) {
42      Rectangle screenRect = Transform.ToScreen(rectangle, Parent.Viewport, Parent.ClippingArea);
43
44      graphics.DrawRectangle(GetPen(), screenRect);
45      graphics.FillRectangle(GetBrush(), screenRect);
46    }
47
48    private Pen GetPen() {
49      if (pen == null)
50        pen = new Pen(color, 1);
51      return pen;
52    }
53
54    private Brush GetBrush() {
55      if (brush == null)
56        brush = new SolidBrush(Color.FromArgb(opacity, color));
57      return brush;
58    }
59
60    public int Opacity {
61      get { return opacity; }
62      set {
63        opacity = value;
64        DisposeDrawingTools();
65      }
66    }
67
68    public Color Color {
69      get { return color; }
70      set {
71        color = value;
72        DisposeDrawingTools();
73      }
74    }
75
76    private void DisposeDrawingTools() {
77      if (pen != null) {
78        pen.Dispose();
79        pen = null;
80      }
81
82      if (brush != null) {
83        brush.Dispose();
84        brush = null;
85      }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.