25
Spring 2010 6.813/6.831 User Interface Design and Implementation 1 Lecture 11: Output

Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Embed Size (px)

Citation preview

Page 1: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Spring 2010 6.813/6.831 User Interface Design and Implementation 1

Lecture 11: Output

Page 2: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

UI Hall of Fame or Shame?

Spring 2008 6.831 User Interface Design and Implementation 2

suggested by Jonathan Goldberg

Page 3: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

6.813/6.831 User Interface Design and Implementation 3

Nanoquiz

• closed book, closed notes• submit before time is up (paper or web)• we’ll show a timer for the last 20 seconds

Spring 2011

Page 4: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

6.813/6.831 User Interface Design and Implementation 4

1. Automatic layout: (choose all that apply)A. is a recursive process over the view hierarchyB. is useful for tolerating changes in font sizeC. is a feature of the MVC pattern D. is limited to enforcing one-dimensional constraints among

objects

2. Which of the following is an uncontroversial statement: (choose all that apply)

A. table elements should be used for layout in HTML and CSS B. CSS layout was originally designed for documents, not GUIsC. it’s harder to prepare for a nanoquiz when the lecture notes are

scantyD. the Document Object Model stores application data

3. Which of the following is not a CSS layout style? (choose one answer)A. display: blockB. display: inlineC. display: divD. display: inline-block

2019181716151413121110 9 8 7 6 5 4 3 2 1 0

Spring 2011

Page 5: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Today’s Topics

• Output approaches• Drawing• Rasterization• Declarative programming

Spring 2010 6.813/6.831 User Interface Design and Implementation 5

Page 6: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Three Output Approaches

• Objects– Graphical objects arranged in a tree with automatic redraw– Example: Label object, Line object– Also called: views, interactors, widgets, controls

• Strokes– High-level drawing primitives: lines, shapes, curves, text– Example: drawText() method, drawLine() method– Also called: vector graphics, structured graphics

• Pixels– 2D array of pixels– Also called: raster, image, bitmap

Spring 2010 6.813/6.831 User Interface Design and Implementation 6

Page 7: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Example: Designing a Graph View

• Object approach– Each node and edge is a component– A node might have two subcomponents: circle and label

• Stroke approach– Graph view draws lines, circles and text

• Pixel approach– Graph view has pixel images of the nodes

Spring 2010 6.813/6.831 User Interface Design and Implementation 7

A

C

B

Window

Node A

Circle Label

Edge A-B

Page 8: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Issues in Choosing Output Approaches

• Layout• Input• Redraw• Drawing order• Heavyweight objects• Device dependence

Spring 2010 6.813/6.831 User Interface Design and Implementation 8

Page 9: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

How Output Approaches Interact

Spring 2010 6.813/6.831 User Interface Design and Implementation 9

Objects

Strokes

Pixels

drawing

rasterization

Page 10: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Drawing a View Tree

• Drawing goes top down– Draw self (using strokes or pixels)– For each child component,

• If child intersects clipping region then– intersect clipping region with child’s bounding box– recursively draw child with clip region set to the intersection

Spring 2010 6.813/6.831 User Interface Design and Implementation 10

A

C

Bclip region

Window

Node A

Circle Label

Node B

Page 11: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Redraw Example

Spring 2010 6.813/6.831 User Interface Design and Implementation 11

Window

Node A

Circle LabelNode B

Skipped (doesn’t intersect clip region)

A

C

B

A

C

B

A

C

B A

C

BA

Edge A-BA

C

BA

1

2

3

5

4 6

7

Page 12: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Z Order

• 2D GUIs are really “2 ½ D”– Drawing order produces layers– Not a true z coordinate for each object, but merely

an ordering in the z dimension

• View tree and redraw algorithm dictate z order– Parents are drawn first, underneath children– Older siblings are drawn under younger ones

• Flex, HTML, most GUI toolkits and drawing programs behave this way

• Java Swing is backwards: last component added (highest index) is drawn first

Spring 2010 6.813/6.831 User Interface Design and Implementation 12

A

C

B A

C

BA

Page 13: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Damage and Automatic Redraw

Spring 2010 6.813/6.831 User Interface Design and Implementation 13

A

C

BA

D

BA

C

B

damaged region Window

Node A Node B Node C/ D

Page 14: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Naïve Redraw Causes Flashing Effects

Spring 2010 6.813/6.831 User Interface Design and Implementation 14

Determine damaged region

Redraw parent(children blink out!)

Redraw children

Object moves

Page 15: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Double-Buffering

• Double-buffering solves the flashing problem

Spring 2010 6.813/6.831 User Interface Design and Implementation 15

Screen

Memory buffer

Page 16: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Going From Objects to Strokes

• Drawing method approach– e.g. Swing paint() method– Drawing method is called directly during redraw; override it

to change how component draws itself

• Retained graphics approach– e.g. Adobe Flex– Stroke calls are recorded and played back at redraw time

• Differences– Retained graphics is less error prone– Drawing method gives more control and performance

Spring 2010 6.813/6.831 User Interface Design and Implementation 16

Page 17: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Stroke Model

• Drawing surface– Also called drawable (X Windows), GDI (MS Win)– Screen, memory buffer, print driver, file, remote screen

• Graphics context– Encapsulates drawing parameters so they don’t have to be

passed with each call to a drawing primitive– Font, color, line width, fill pattern, etc.

• Coordinate system– Origin, scale, rotation

• Clipping region• Drawing primitives

– Line, circle, ellipse, arc, rectangle, text, polyline, shapes

Spring 2010 6.813/6.831 User Interface Design and Implementation 17

Page 18: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

18

HTML5 Canvas in One Slide

HTML element<canvas width=1000

height=1000></canvas>

graphics contextvar ctx =

canvas.getContext(“2d”)

coordinate systemctx.translate()ctx.rotate()ctx.scale()

color, font, line style, etc.ctx.strokeStyle =

“rgb(0.5,0.5,0.5)”ctx.fillStyle = … ctx.font = “bold 12pt sans-

serif”

ctx.lineWidth = 2.5

drawing primitivesctx.beginPath();ctx.moveTo(0,0)ctx.lineTo(500,500)ctx.stroke()

ctx.beginPath()ctx.arc(500,500,100,0,

2*Math.PI,false)ctx.fill()

clippingctx.beginPath()ctx.rect(0, 0, 200, 300)ctx.clip()

Spring 2011 6.813/6.831 User Interface Design and Implementation

Page 19: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Antialiasing and Subpixel Rendering

Spring 2010 6.813/6.831 User Interface Design and Implementation 19

Simple Antialiased Subpixel rendering

Page 20: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Pixel Approach

• Pixel approachis a rectangular array of pixels– Each pixel is a vector (e.g., red, green, blue components), so pixel

array is really 3 dimensional• Bits per pixel (bpp)

– 1 bpp: black/white, or bit mask– 4-8 bpp: each pixel is an index into a color palette– 24 bpp: 8 bits for each color– 32 bpp: 8 bits for each color + alpha channel

• Color components (e.g. RGB) are also called channels or bands• Pixel model can be arranged in many ways

– Packed into words (RGBR GBRG …) or loosely (RGB- RGB- …)– Separate planes (RRR…GGG…BBB…) vs. interleaved (RGB RGB

RGB…)– Scanned from top to bottom vs. bottom to top

Spring 2010 6.813/6.831 User Interface Design and Implementation 20

Page 21: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Transparency

• Alpha is a pixel’s transparency– from 0.0 (transparent) to 1.0 (opaque)– so each pixel has red, green, blue, and alpha

values

• Uses for alpha– Antialiasing– Nonrectangular images– Translucent components– Clipping regions with antialiased edges

Spring 2010 6.813/6.831 User Interface Design and Implementation 21

Page 22: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

BitBlt

• BitBlt (bit block transfer) copies a block of pixels from one image to another– Drawing images on screen– Double-buffering– Scrolling– Clipping with nonrectangular masks

• Compositing rules control how pixels from source and destination are combined– More about this in a later lecture

Spring 2010 6.813/6.831 User Interface Design and Implementation 22

Page 23: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Image File Formats

• GIF– 8 bpp, palette uses 24-bit colors– 1 color in the palette can be transparent (1-bit alpha channel)– lossless compression– suitable for screenshots, stroked graphics, icons

• JPEG– 24 bpp, no alpha– lossy compression: visible artifacts (dusty noise, moire patterns)– suitable for photographs

• PNG– lossless compression– 1, 2, 4, 8 bpp with palette– 24 or 48 bpp with true color– 32 or 64 bpp with true color and alpha channel– suitability same as GIF– better than GIF, but no animation

Spring 2010 6.813/6.831 User Interface Design and Implementation 23

Page 24: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Hints for Debugging Output

• Something you’re drawing isn’t appearing on the screen. Why not?– Wrong place– Wrong size– Wrong color– Wrong z-order

Spring 2010 6.813/6.831 User Interface Design and Implementation 24

Page 25: Spring 20106.813/6.831 User Interface Design and Implementation1 Lecture 11: Output

Summary

• Component, stroke, pixel models• Automatic redraw and double-buffering• Image formats

Spring 2010 6.813/6.831 User Interface Design and Implementation 25