User Interface Programming in C Graphics Chris North

  • Slides: 28
Download presentation
User Interface Programming in C#: Graphics Chris North CS 3724: HCI

User Interface Programming in C#: Graphics Chris North CS 3724: HCI

GUI Topics ü Components ü GUI layouts ü Events Ø Graphics • Manipulation •

GUI Topics ü Components ü GUI layouts ü Events Ø Graphics • Manipulation • Animation • Databases • MVC

Review • 3 kinds of elements in a component’s API? • Receive Events? •

Review • 3 kinds of elements in a component’s API? • Receive Events? • GUI Layout techniques?

Graphics • • Screen is like a painter’s canvas App must paint its window

Graphics • • Screen is like a painter’s canvas App must paint its window contents • • GUI components paint themselves Anything else: Programmer 1. How to paint? 2. When to paint? Button

1. How to Paint? Examples: Draw static graphics On startup, on click

1. How to Paint? Examples: Draw static graphics On startup, on click

Pixels

Pixels

Coordinate System • Upside-down Cartesian (0, 0) (0, height) • Ywindow = height -

Coordinate System • Upside-down Cartesian (0, 0) (0, height) • Ywindow = height - Ycartesian (width, 0) (width, height)

Component Hierarchy • Each component has its own subwindow • Subwindow = rectangular area

Component Hierarchy • Each component has its own subwindow • Subwindow = rectangular area within parent component • Has own coordinate system • Clipping: • Can’t paint outside its subwindow • Can’t paint over child components? (0, 0) Button Panel (0, 0) Button (wb, hb) (wp, hp)

Painting Components • Can paint any component • Panel is good for custom graphics

Painting Components • Can paint any component • Panel is good for custom graphics area Button Panel

Painting in C# 1. The GDI+ graphics library: using System. Drawing; 2. Get the

Painting in C# 1. The GDI+ graphics library: using System. Drawing; 2. Get the “graphics context” of a component: Graphics g = my. Panel. Create. Graphics( ); 3. Paint in it: g. Draw. Line(pen, x 1, y 1, x 2, y 2);

Graphics Primitives Draw • Line (pt 1, pt 2) • Lines (pt list) •

Graphics Primitives Draw • Line (pt 1, pt 2) • Lines (pt list) • Arc (rect) • Curves, Bezier (pt list) • Ellipse (rect) • Rectangle (rect) • Polygon (pt list) • Image (img, x, y) • String (string, x, y) label Fill

Graphics Attributes • Pen (for lines) • Color, width, dash, end caps, joins, •

Graphics Attributes • Pen (for lines) • Color, width, dash, end caps, joins, • Brush (for filling) • Color, Solid, texture, pattern, gradient • Font, String Format (for strings) • Bitmap/Metafile (for images) • Bmp, gif, jpeg, png, tiff, wmf, …

Color • Combinations of Red, Green, Blue • Alpha value = opacity • Each

Color • Combinations of Red, Green, Blue • Alpha value = opacity • Each in [0, 255] • C#: Color. From. Argb(255, 150, 0)

Color • Combinations of Red, Green, Blue • Alpha value = opacity • Each

Color • Combinations of Red, Green, Blue • Alpha value = opacity • Each in [0, 255] • C#: Color. From. Argb(255, 150, 0) Hokie Orange

2. When to Paint? Examples: Draw graphics On repaint

2. When to Paint? Examples: Draw graphics On repaint

Re-Paint • Screen is like a painter’s canvas • All windows paint on the

Re-Paint • Screen is like a painter’s canvas • All windows paint on the same surface! • Windows don’t “remember” whats under them • Need to re-paint when portions are newly exposed • Receive Repaint events • Open, resize, bring to front • When other windows in front move, resize, close

My. App

My. App

Open Win. Exp, Notepad

Open Win. Exp, Notepad

Close Win. Explorer Repaint event sent to: Desktop, My. App

Close Win. Explorer Repaint event sent to: Desktop, My. App

Desktop gets repaint event

Desktop gets repaint event

My. App gets repaint event My. App Form gets repaint event

My. App gets repaint event My. App Form gets repaint event

My. App gets repaint event My. App Form forwards repaint event to Button

My. App gets repaint event My. App Form forwards repaint event to Button

Repainting Static Graphics • Repaint event: • Erase (fill with background color) - usually

Repainting Static Graphics • Repaint event: • Erase (fill with background color) - usually automatically done by the control • Draw graphics

In C# • Receive “paint” event: (select the event in Vis. Studio) this. Paint

In C# • Receive “paint” event: (select the event in Vis. Studio) this. Paint += new Paint. Event. Handler(this. Form 1_Paint); private void Form 1_Paint(object sender, Paint. Event. Args e){ Graphics g = e. Graphics; g. Draw. Line(new Pen(Color. Red, 10), 10, 300); } • OR: Override the On. Paint method override void On. Paint(Paint. Event. Args e){ base. On. Paint(e); //preserve base class functionality Graphics g = e. Graphics; g. Draw. Line(new Pen(Color. Red, 10), 10, 300); } • Can call Refresh( ) to force a repaint

Typical program structure for Dynamic Graphics • Store data structure of graphics items •

Typical program structure for Dynamic Graphics • Store data structure of graphics items • E. g. user drawn picture in a paint program • Paint event: • Erase window (draw background color) • Draw graphics for items in data structure • Other user events that alter the graphics items: • modify the data structure • send repaint event by calling Refresh( )

Program Structure C# Win. App: Program State -data structures Paint event -display data Interaction

Program Structure C# Win. App: Program State -data structures Paint event -display data Interaction events -modify data Class{ declare data storage; constructor(){ initialize data storage; } cntrl 1_paintevent(e){ draw graphics from data; } cntrl 2_mouse. Event(e){ manipulate data; cntrl 1. refresh(); } …

Data structure for graphics items • 2 approaches: • Store logical contents in a

Data structure for graphics items • 2 approaches: • Store logical contents in a data structure, then draw graphics based on the data » E. g. drawing program: lines, shapes, colors, … » E. g. visualization program: data table • Store visual contents as an off-screen image (bitmap) » E. g. pixels » Then use g. Draw. Image( ) in paint event

What Control’s Subwindows Do • • • Directs mouse input to correct component Determines

What Control’s Subwindows Do • • • Directs mouse input to correct component Determines repaint events Own coordinate system Don’t need repaint when moving Clipping: hides drawing behind a subwindow • Some subwindows remember what’s under them: • Popup menus