Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/Canvas.cs @ 1641

Last change on this file since 1641 was 1606, checked in by bspisic, 16 years ago

Exception handling for invalid coordinates (#589)

File size: 2.1 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Drawing;
4using System.Drawing.Drawing2D;
5
6namespace HeuristicLab.Visualization {
7  public class Canvas : IShape {
8    private readonly WorldShape worldShape;
9
10    private Rectangle viewport;
11
12    public Canvas() {
13      worldShape = new WorldShape();
14      worldShape.Parent = this;
15    }
16
17    public void AddShape(IShape shape) {
18      worldShape.AddShape(shape);
19    }
20
21    public void RemoveShape(IShape shape) {
22      worldShape.RemoveShape(shape);
23    }
24
25    public void ClearShapes() {
26      worldShape.ClearShapes();
27    }
28
29    public Rectangle Viewport {
30      get { return viewport; }
31      set {
32        viewport = value;
33        worldShape.ClippingArea = new RectangleD(0, 0, viewport.Width, viewport.Height);
34        worldShape.BoundingBox = worldShape.ClippingArea;
35      }
36    }
37
38    public RectangleD ClippingArea {
39      get { return worldShape.ClippingArea; }
40    }
41
42    public RectangleD BoundingBox {
43      get { throw new InvalidOperationException(); }
44    }
45
46    public IShape Parent {
47      get { throw new InvalidOperationException(); }
48      set { throw new InvalidOperationException(); }
49    }
50
51    public void Draw(Graphics graphics) {
52      try {
53        Stopwatch sw = new Stopwatch();
54        sw.Start();
55
56        graphics.SmoothingMode = SmoothingMode.AntiAlias;
57        graphics.FillRectangle(Brushes.White, Viewport);
58
59        worldShape.Draw(graphics);
60
61        graphics.DrawRectangle(Pens.Black, 0, 0, Viewport.Width - 1, Viewport.Height - 1);
62
63        sw.Stop();
64        Trace.WriteLine(string.Format("Drawing time: {0:0.0}ms", sw.Elapsed.TotalMilliseconds));
65      } catch (OverflowException e) {
66        Trace.WriteLine(e);
67
68        graphics.FillRectangle(Brushes.White, graphics.ClipBounds);
69
70        using (Font font = new Font("Arial", 14)) {
71          const string message = "Zoom level is too high!";
72          graphics.DrawString(message, font, Brushes.Red, graphics.ClipBounds.X + 10, graphics.ClipBounds.Y + 10);
73        }
74      }
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.