Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/RectangleShape.cs @ 1245

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

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

File size: 1.9 KB
RevLine 
[635]1using System.Drawing;
2
3namespace HeuristicLab.Visualization {
4  public class RectangleShape : IShape {
[1240]5    private IShape parent;
[984]6    private RectangleD rectangle;
[1233]7
[635]8    private Color color;
[996]9    private int opacity = 255;
[635]10
[1233]11    private Pen pen;
12    private Brush brush;
13
[1038]14    public RectangleShape(double x1, double y1, double x2, double y2, Color color) {
[984]15      rectangle = new RectangleD(x1, y1, x2, y2);
[635]16      this.color = color;
17    }
18
19    public RectangleD BoundingBox {
[984]20      get { return rectangle; }
[635]21    }
22
[1240]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
[1233]36    public RectangleD Rectangle {
37      get { return rectangle; }
38      set { rectangle = value; }
39    }
40
[1240]41    public void Draw(Graphics graphics) {
42      Rectangle screenRect = Transform.ToScreen(rectangle, Parent.Viewport, Parent.ClippingArea);
[984]43
[1233]44      graphics.DrawRectangle(GetPen(), screenRect);
45      graphics.FillRectangle(GetBrush(), screenRect);
46    }
[635]47
[1233]48    private Pen GetPen() {
49      if (pen == null)
50        pen = new Pen(color, 1);
51      return pen;
[635]52    }
53
[1233]54    private Brush GetBrush() {
55      if (brush == null)
56        brush = new SolidBrush(Color.FromArgb(opacity, color));
57      return brush;
58    }
59
[996]60    public int Opacity {
61      get { return opacity; }
[1233]62      set {
63        opacity = value;
64        DisposeDrawingTools();
65      }
[996]66    }
67
[1038]68    public Color Color {
69      get { return color; }
[1233]70      set {
71        color = value;
72        DisposeDrawingTools();
73      }
[1038]74    }
[1233]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    }
[635]87  }
[984]88}
Note: See TracBrowser for help on using the repository browser.