27
May 11, 1998 CS102-02 Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

More Graphics in Java

CS 102-02

Lecture 7-1

A picture's worth a thousand words

Page 2: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Agenda

• Fonts

• Lines

• Rectangles

• Ovals

• Arcs

Page 3: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Logical Fonts

• Different systems have different fonts

• Java uses logical fonts– Maps logical fonts into system-specific

fonts

Page 4: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Specifying Fonts• Font name

– Serif, Sans Serif, Dialog, DialogInput and MonospacedSans SerifWith Serif

• Font style– Bold, italic and plain

• Font size– Point size (1pt = 1/72nd of an inch)

Page 5: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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

Page 6: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Getting the List

• Use the Toolkit

• Toolkit is the link between Java and the specific system

String fonts[] = Toolkit.getDefaultToolkit().getFontList();

Page 7: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Constant Styles

• Font class includes constants for setting style– Font.BOLD, Font.ITALIC, Font.PLAIN– Combine them with +, as in

• Font.BOLD+Font.ITALIC gives a bold, italic font

Page 8: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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

Page 9: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Font Metrics

From the Java Tutorial

Page 10: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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.

Page 11: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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.

Page 12: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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.

Page 13: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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.

Page 14: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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.

Page 15: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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

Page 16: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Drawing Lines• Use the drawLine() method of the

Graphics classdrawLine(int x1, int y1, int x2, int y2)

• Specify four coordinatesx1, y1

x2, y2

Page 17: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Drawing Rectangles• Two dimensions gives more options

– Filled and unfilled– Fill color is the current color -- not an

argument to the method

• Just a rectangledrawRect(int x, // top-leftint y, // coordinateint width,int height)

Page 18: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Filled & Unfilled

x, y x, y

width

height

Page 19: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Rectangle Flavors

• Outline rectangledrawRect()

• Filled (in current foreground color) rectanglefillRect()

• Filled (in background color) rectangleclearRect()

Page 20: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Rounded Rectangles

• To draw a rounded rectangledrawRoundRect(x, y, width, height, arcWidth, arcHeight)

• Rounded rectangles can also be filledfillRoundRect()

arcWidth

arcHeight

width

height

x, y

Page 21: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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 cornerdraw3DRect(int x,int y,int width,int height,boolean raised)

Page 22: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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 withfill3DRect()

Page 23: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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-leftint y, // coordinateint width,int height)

Page 24: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

A Filled Oval and Its Box

x, y

width

height

Page 25: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

Drawing Arcs

• Draws the outline of a circular or elliptical arc covering the specified rectangle (filled arcs too)

startAngle

arcAngle + startAngle

Page 26: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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

Page 27: May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words

May 11, 1998 CS102-02 Lecture 7-1

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