Docsity

CS 338: Graphical User Interfaces Lecture 6 - Graphics Basics - Prof. David E. Breen, Study notes of Computer Science

Toggle sidebar
bg1
Page 1
1 CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University.
Lecture 6:
Graphics Basics
CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 2
Onwards and downwards…
• What goes on underneath the GUI?
• How does the toolkit draw / maintain the
interface?
• This class: Intro to basic graphics
– image models
– coordinate systems
– canvas
– drawing
– clipping
Thanks to James Landay
@ UC Berkeley for the
foundation of these slides!
CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 3
Models for images
• How can we
represent / store
these images?
CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 4
• Image = set of strokes
Stroke = { start, end, thickness, color }
• Line ((10,4), (17,4), thick 2, red)
• Circle ((19,13), radius 3, thick 3, black)
• Easy to display, print (?), plot
• Commonly used in GUI toolkits, in variations
– arcs, ellipses, rounded rectangles, etc.
Stroke model
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 338: Graphical User Interfaces Lecture 6 - Graphics Basics - Prof. David E. Breen and more Study notes Computer Science in PDF only on Docsity!

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 1

Lecture 6:

Graphics Basics

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 2

Onwards and downwards…

  • What goes on underneath the GUI?
  • How does the toolkit draw / maintain the

interface?

  • This class: Intro to basic graphics
    • image models
    • coordinate systems
    • canvas
    • drawing
    • clipping Thanks to James Landay @ UC Berkeley for the foundation of these slides! CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 3

Models for images

  • How can we

represent / store

these images?

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 4

  • Image = set of strokes

Stroke = { start, end, thickness, color }

  • Line ((10,4), (17,4), thick 2, red)
  • Circle ((19,13), radius 3, thick 3, black)
  • Easy to display, print (?), plot
  • Commonly used in GUI toolkits, in variations
  • arcs, ellipses, rounded rectangles, etc.

Stroke model

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 5

Stroke model

  • Can we represent this picture as strokes? CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 6

Pixel model

  • “Pixel” = picture element
    • discrete 2D unit of a picture grid
  • Pixel image components
    • spatial resolution = { # rows, # columns }
      • e.g., your favorite monitor or printer resolution
    • image depth = number of bits per pixel
      • why do we need more than one bit per pixel? CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 7

Pixel model: Image depth

  • Black & white: 1 bit / pixel
  • Grayscale: ~8 bits / pixel
    • gray from 0 (black) to 255 (white)
  • Full color: ~24 bits / pixel
    • primary colors (red, green, blue) from 0-
    • but… lots of memory needed!
  • Color-mapped: ~8 bits / pixel
    • pixel = index into color table
    • color table elsewhere for lookup
    • some extra work, but lots less space CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 8

Pixel model: Aliasing

  • Smooth objects are mapped to closest pixels,

which can result in jagged edges

  • Antialiasing attempts to reconcile with

intermediate colors

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 13

Canvas

  • What functionality does the canvas provide?
    • drawing of lines & shapes
      • with stroke & region models
    • drawing of text
      • subset of lines & shapes, but highly specialized
    • “clipping”
      • drawing only what you need and/or what’s visible
    • controlling color & texture CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 14 Note: Pseudocode!

Drawing

  • Q: How do you draw a rectangle?
    • void Canvas::Rectangle (x1, y1, x2, y2, lineWidth, lineColor, fillColor)
  • Do I have to do all that every time?!?
    • shapes have properties in common
      • geometry, line/border width, line/fill color, pattern
    • we can make use of this!
  • Maintain settings within system that

generally stay the same within logical blocks

  • void Canvas::SetLineWidth (lw)
  • void Canvas::SetFillColor (color)
  • void Canvas::Rectangle (x1, y1, x2, y2) CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 15

Paths

  • 1D objects drawn on the 2D canvas
    • no inside or outside
    • infinitely thin
    • can have associated “stroke”, “fill”
  • Examples
    • lines (end points)
    • circles (center, radius)
    • ellipses (center, width, height)
    • arcs (circle plus restrictions)
    • splines (parametric cubic equations) CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 16

Regions = Closed shapes

  • Closed path + fill color/pattern
  • If path is not closed, system can “close” it

for you

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 17

Text

  • Back in the old days…
    • easy storage: 1 byte / character
    • map character --> display representation
  • The WYSIWYG revolution
    • “what you see is what you get”
    • suddenly, text was drawn like everything else
    • in hindsight, makes sense
    • but it takes work…

T e x t w a s d r a

w i t h i n b l o c k

w n

s.

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 18

Text

  • Commodore 64 screen shot CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 19

Text

  • Font family
    • general shape of characters
    • e.g., Times, Palatino, Helvetica, Courier
    • serif vs. sans-serif (without serif)
    • fixed-width vs. proportionally-spaced Helvetica A Courier every letter has the same width Times every letter has a different width Palatino A CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 20

Text

  • Font style
    • normal, bold, italic, bold italic
  • Font size
    • measured in points, 1 point = 1/72 inch
    • generally accurate for printed output
    • on displays, things can vary a great deal
  • Font information
    • height (for multiple lines of text)
    • ascenders & descenders (e.g., f & g)
    • leading (space between lines)
    • kerning (space between letters) (Ha vs. Ta)

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 25

Color models

  • RGB model
    • specify color by red, green, & blue components
    • additive model
    • 8 bits/component = 24 bits total per pixel
    • in the end, all others reduce to RGB for display CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 26

Color models

  • HSV model -

hue, saturation, & value

  • easier for people to think about
  • hue = primary wavelength (i.e., basic color)
  • saturation = measure of how pure light is - high is pure, low means it is mixed w/ white/gray
  • value = intensity or brightness (dark vs. light)
  • direct conversion to RGB CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 27
  • CMY model
  • represent in terms of mixtures of pigments
  • gets color from light it absorbs & doesn’t reflect
  • mix Cyan, Magenta, Yellow
  • “subtractive primaries”
  • cyan = no red, magenta = no green, yellow = no blue
  • used by printers and artists
  • Color “matching” is quite

difficult for printed output

**sometimes:

  • K = Black (needed for truer printing for shades of black)**

Color models

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 28

Research Question of the Day

  • Users are human — how does this affect how

we can/should do graphics?

  • Let’s ask two questions...
  • Question #1:
    • Often, graphics today involves animation.
    • Animation involves re-drawing frames many times per second to “create” motion.
    • How quickly do we need to draw new frames to get smooth motion?
    • << Jumping Bug >>

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 29

Research Question of the Day

  • Answer to Question #1: No easy answer.
  • Some common rates:
    • movies: 24 fps
    • television: 30 fps
      • actually, 1/2 frame @ 60 Hz (interlaced)
    • CRT (cathode-ray tube) monitor: ~60-100 fps
  • Generally agreed-to rates:
    • minimal rate for smooth motion: ~20-30 fps
    • limits of the eye: probably ~200 fps (??)
    • medium rates may not be noticeable, but may cause eye strain CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 30

Research Question of the Day

  • In the context of GUIs, though, lower redraw

rates may be OK... sometimes

  • dragging a window?
  • displaying typed text?
  • finding search text?
  • virtual reality simulation? CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 31 {100,0,0} {0,100,0} {0,70,0} {0,72,0}

Research Question of the Day

  • Question #2:
    • Today’s computers can display millions of colors.
    • How many colors can people distinguish? How many colors are “enough”? CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 32

Research Question of the Day

  • Answer to Question #2: No easy answer.
  • Total colors = 256^3 = 16,777,
  • Discriminable hues probably > 10,
    • could theoretically be millions by some definition
    • for practical purposes, though, << 16 million
  • This is useful to know!
    • e.g., some “lossy” image compression algorithms (e.g., JPEG) take advantage of our inability to discriminate

CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. 37

Graphics in Swing

  • Drawing text
    • pretty straightforward...
    • useful method
      • returns the total advance width for showing the specified String in current Font
      • Used for positioning String g.drawString("Hello World!", x, y); int stringWidth (String);