using System.Drawing;
using System.Drawing.Drawing2D;
namespace HeuristicLab.Visualization {
public class LineShape : IShape {
private IShape parent;
private RectangleD boundingBox;
private Color color;
private int thickness;
private DrawingStyle drawingStyle;
private Pen pen;
///
/// Initializes the LineShape.
///
/// x coordinate of left lineEndPoind
/// y coordinate of left lineEndPoind
/// x coordinate of right lineEndPoind
/// y coordinate of right lineEndPoind
/// color for the LineShape
/// tickness of the line in pixels
/// drawing style of the line (solid, dashed, dotted,...)
public LineShape(double x1, double y1, double x2, double y2, Color color, int thickness, DrawingStyle drawingStyle) {
this.boundingBox = new RectangleD(x1, y1, x2, y2);
this.LSColor = color;
this.LSThickness = thickness;
this.LSDrawingStyle = drawingStyle;
}
public RectangleD BoundingBox {
get { return boundingBox; }
set { boundingBox = value; }
}
public RectangleD ClippingArea {
get { return Parent.ClippingArea; }
}
public Rectangle Viewport {
get { return Parent.Viewport; }
}
public IShape Parent {
get { return parent; }
set { parent = value; }
}
public double Y1 {
get { return boundingBox.Y1; }
set { boundingBox.Y1 = value; }
}
public double Y2 {
get { return boundingBox.Y2; }
set { boundingBox.Y2 = value; }
}
public double X1 {
get { return boundingBox.X1; }
set { boundingBox.X1 = value; }
}
public double X2 {
get { return boundingBox.X2; }
set { boundingBox.X2 = value; }
}
///
/// Draws the LineShape.
///
/// graphics handle to draw to
public virtual void Draw(Graphics graphics) {
Rectangle screenRect = Transform.ToScreen(boundingBox, Parent.Viewport, Parent.ClippingArea);
graphics.DrawLine(GetPen(), screenRect.Left, screenRect.Bottom, screenRect.Right, screenRect.Top);
}
private Pen GetPen() {
if (pen == null) {
pen = new Pen(LSColor, LSThickness);
switch (LSDrawingStyle) {
case DrawingStyle.Dashed:
pen.DashStyle = DashStyle.Dash;
break;
default:
pen.DashStyle = DashStyle.Solid;
break;
}
}
return pen;
}
private void DisposePen() {
if (pen != null) {
pen.Dispose();
pen = null;
}
}
public Color LSColor {
get { return color; }
set {
color = value;
DisposePen();
}
}
public int LSThickness {
get { return thickness; }
set {
thickness = value;
DisposePen();
}
}
public DrawingStyle LSDrawingStyle {
get { return drawingStyle; }
set {
drawingStyle = value;
DisposePen();
}
}
}
}