may 11, 1998cs102-02lecture 7-1 more graphics in java cs 102-02 lecture 7-1 a picture's worth a...
Post on 20-Dec-2015
213 views
TRANSCRIPT
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, 1998 CS102-02 Lecture 7-1
Agenda
• Fonts
• Lines
• Rectangles
• Ovals
• Arcs
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
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)
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
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();
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
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
May 11, 1998 CS102-02 Lecture 7-1
Font Metrics
From the Java Tutorial
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.
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.
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.
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.
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.
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
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
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)
May 11, 1998 CS102-02 Lecture 7-1
Filled & Unfilled
x, y x, y
width
height
May 11, 1998 CS102-02 Lecture 7-1
Rectangle Flavors
• Outline rectangledrawRect()
• Filled (in current foreground color) rectanglefillRect()
• Filled (in background color) rectangleclearRect()
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
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)
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()
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)
May 11, 1998 CS102-02 Lecture 7-1
A Filled Oval and Its Box
x, y
width
height
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
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
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