More%20Graphics%20in%20Java - PowerPoint PPT Presentation

About This Presentation
Title:

More%20Graphics%20in%20Java

Description:

Number of pixels between the baseline and the descender line ... Returns the suggested distance (in pixels) between one line of text and the next ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 28
Provided by: kurtdfens
Category:

less

Transcript and Presenter's Notes

Title: More%20Graphics%20in%20Java


1
More Graphics in Java
A picture's worth a thousand words
  • CS 102-02
  • Lecture 7-1

2
Agenda
  • Fonts
  • Lines
  • Rectangles
  • Ovals
  • Arcs

3
Logical Fonts
  • Different systems have different fonts
  • Java uses logical fonts
  • Maps logical fonts into system-specific fonts

4
Specifying Fonts
  • Font name
  • Serif, Sans Serif, Dialog, DialogInput and
    Monospaced
  • Sans Serif
  • With Serif
  • Font style
  • Bold, italic and plain
  • Font size
  • Point size (1pt 1/72nd of an inch)

5
Font Names
  • Listed names are guaranteed, but there might be
    more
  • From my Win NT 4.0 machine
  • Dialog, SansSerif, Serif, Monospaced, Helvetica,
    TimesRoman, Courier, DialogInput, ZapfDingbats

6
Getting the List
  • Use the Toolkit
  • Toolkit is the link between Java and the specific
    system
  • String fonts Toolkit.getDefaultToolkit().getFo
    ntList()

7
Constant Styles
  • Font class includes constants for setting style
  • Font.BOLD, Font.ITALIC, Font.PLAIN
  • Combine them with , as in
  • Font.BOLDFont.ITALIC gives a bold, italic font

8
What Does Size Mean?
  • Every font has many sizes associated with it
  • Different sizes are the font's metrics
  • Font's size (in points) is a rough gauge of the
    overall size

9
Font Metrics
From the Java Tutorial
10
How High?
  • getAscent(), getMaxAscent()
  • Number of pixels between the ascender line and
    the baseline
  • Ascender line represents the typical height of
    capital letters (chosen by the font's designer to
    represent the correct text "color")
  • Ascent typically provides enough room for almost
    all of the characters in the font, except perhaps
    for accents on capital letters
  • getMaxAscent() method accounts for these
    exceptionally tall characters.

11
How Low Can You Go?
  • getDescent(), getMaxDescent()
  • Number of pixels between the baseline and the
    descender line
  • In most fonts, all characters fall within the
    descender line at their lowest point
  • Use the getMaxDescent() method to get a distance
    guaranteed to encompass all characters.

12
Font Height
  • getHeight()
  • Returns the number of pixels normally found
    between the baseline of one line of text and the
    baseline of the next line of text
  • Includes an allowance for leading.

13
Leading
  • getLeading()
  • Returns the suggested distance (in pixels)
    between one line of text and the next
  • Leading is the distance between the descender
    line of one line of text and the ascender line of
    the next line of text.

14
Will the Real Size Please Stand Up?
  • Font size (returned by the Font class getSize()
    method) is an abstract measurement
  • In theory corresponds to the ascent plus the
    descent
  • Reality font designer decides exactly how tall a
    "12 point" font (for example) is
  • 12-point Times is often slightly shorter than
    12-point Helvetica.

15
Font Measurements
  • Use a FontMetrics object
  • Call methods such as getAscent() and getDescent()
    from a FontMetrics object
  • int ascent g.getFontMetrics().getAscent()
  • When text is drawn at (x,y), the specified point
    is used as the reference point

Baseline
16
Drawing Lines
  • Use the drawLine() method of the Graphics class
  • drawLine(int x1, int y1, int x2, int y2)
  • Specify four coordinates

x1, y1
x2, y2
17
Drawing Rectangles
  • Two dimensions gives more options
  • Filled and unfilled
  • Fill color is the current color -- not an
    argument to the method
  • Just a rectangle
  • drawRect(
  • int x, // top-left
  • int y, // coordinate
  • int width,
  • int height)

18
Filled Unfilled
x, y
x, y
height
width
19
Rectangle Flavors
  • Outline rectangle
  • drawRect()
  • Filled (in current foreground color) rectangle
  • fillRect()
  • Filled (in background color) rectangle
  • clearRect()

20
Rounded Rectangles
x, y
height
arcWidth
arcHeight
width
  • To draw a rounded rectangle
  • drawRoundRect(x, y, width, height, arcWidth,
    arcHeight)
  • Rounded rectangles can also be filled
  • fillRoundRect()

21
Drawing 3-D Rectangles
  • Draw a 3-D highlighted outline of the specified
    rectangle
  • Edges of the rectangle are highlighted so that
    they appear to be beveled
  • Lit from the upper left corner
  • draw3DRect(int x,
  • int y,
  • int width,
  • int height,
  • boolean raised)

22
3-D Highlighting
  • Colors used for the highlighting effect are based
    on the current color
  • Resulting rectangle covers an area that is width
    1 pixels wide by height 1 pixels tall
  • Filled 3D rectangles with
  • fill3DRect()

23
Drawing Ovals
  • Drawing ovals is similar to drawing rectangles
    because you specify the bounding box for the oval
  • For an unfilled oval
  • drawOval(
  • int x, // top-left
  • int y, // coordinate
  • int width,
  • int height)

24
A Filled Oval and Its Box
x, y
height
width
25
Drawing Arcs
arcAngle startAngle
startAngle
  • Draws the outline of a circular or elliptical arc
    covering the specified rectangle (filled arcs too)

26
Arc Parameters
  • Resulting arc begins at startAngle and extends
    for arcAngle degrees, using the current color
  • Angles are interpreted such that 0 degrees is at
    the 3 o'clock position
  • Positive value indicates a counter-clockwise
    rotation while a negative value indicates a
    clockwise rotation

27
More Arc Parameters
  • Center of the arc is the center of the rectangle
    whose origin is (x, y)
  • Size is specified by the width and height
    arguments
  • Resulting arc covers an area width 1 pixels
    wide by height 1 pixels tall
Write a Comment
User Comments (0)
About PowerShow.com