v 1.0 programming iii. recap drawing / geometry classes

10
V 1.0 Programming III. Recap Drawing / Geometry classes

Upload: barnard-fox

Post on 18-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0

Programming III.

RecapDrawing / Geometry classes

Page 2: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Possibilities in WPF• Shapes (System.Windows.Shapes.Shape descendants)

– Simple, pre-defined shapes– Can be (mostly) accessed from the Toolboxban– FrameworkElement descendants: input, focus, events…– Max. 10-20(-100) objects

• Drawing objects (System.Windows.Media.Drawing namespace)– No built-in support for event management– No built-in support for display, requires a hosting object– Usually driven from the XAML, using converters/geometry

instances– Faster (max. 100-1000 objects)

• Visual descendants (System.Windows.Media.Visual)– Most complicated, fastest (10000-20000 objects)– We always use C# code

2

Page 3: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Drawings

• Descendants os System.Windows.Media.Drawing• Can represents more complex shapes

• No built-in support for display/events, they require a hosting object (typically: Image)

3

GeometryDrawing Simple 2D shapes and their groups

GlyphRunDrawing Textual data, special glyphs

ImageDrawing Display image

VideoDrawing Display video (instead, use: MediaPlayer)

DrawingGroup Incorporate multiple drawings into one

Page 4: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Hosting objects

• DrawingImage– To be used in an Image control

• DrawingBrush– To be used in a Brush

• DrawingVisual– To be used in a Visual descendant (later)

4

Page 5: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Geometry classes

• Descendants of System.Windows.Media.Geometry– They define 2D structures, shapes, simple drawings/lines– No display – they only represent the structure!

5

RectangleGeometry Rectangle

EllipseGeometry Ellipse

LineGeometry Line (no area!)

PathGeometry Multiple line segments

GeometryGroup Multiple geometry instances

Page 6: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Example of displaying Geometry in an Image• XAML code: <Image Stretch="None"> <Image.Source> <DrawingImage> <DrawingImage.Drawing> <GeometryDrawing Brush="Black"> <GeometryDrawing.Geometry> <EllipseGeometry Center="10,10” RadiusX="10" RadiusY="10"> </EllipseGeometry> </GeometryDrawing.Geometry> </GeometryDrawing> </DrawingImage.Drawing> </DrawingImage> </Image.Source> </Image>

6

• Can be specified in the GeometryDrawing: Geometry, Brush, Pen

Page 7: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Display GeometryDrawing in a DrawingImage, C#• C# code:DrawingImage drawingImage = new DrawingImage();GeometryDrawing geometryDrawing = new GeometryDrawing();geometryDrawing.Brush = Brushes.Black;geometryDrawing.Geometry = new EllipseGeometry(new Point(10, 10), 10, 10);drawingImage.Drawing = geometryDrawing;image.Source = drawingImage;

7

Page 8: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Example – displaying Text

EllipseGeometry myEllipse = new EllipseGeometry(new Point(10, 10), 10, 10);

FormattedText text = new FormattedText("This is MINE.",CultureInfo.CurrentCulture,

FlowDirection.LeftToRight,new Typeface("Tahoma"), 16, Brushes.Black);

Geometry textGeometry =

text.BuildGeometry(new Point(10, 20));GeometryGroup group = new GeometryGroup();group.Children.Add(ellipse);group.Children.Add(textGeometry);geometryDrawing.Geometry = group; 8

• Display text as Geometry– Using the BuildGeometry method of the FormattedText class

• Then we can merge multiple Geometry instances into one GeometryGroup

Page 9: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Exercise

9

Page 10: V 1.0 Programming III. Recap Drawing / Geometry classes

V 1.0 ÓE-NIK, 2014

Exercise

• If the player finishes a level, a new one should be automatically loaded

• We should measure the time for every level (System.Diagnostics.Stopwatch class)

• In a new window, display the level times using a Garph drawn using a GeometryDrawing

10