270 likes | 284 Views
More Graphics in Java. A picture's worth a thousand words. CS 102-02 Lecture 7-1. Agenda. Fonts Lines Rectangles Ovals Arcs. Logical Fonts. Different systems have different fonts Java uses logical fonts Maps logical fonts into system-specific fonts. Specifying Fonts. Font name
E N D
More Graphics in Java A picture's worth a thousand words CS 102-02 Lecture 7-1
Agenda • Fonts • Lines • Rectangles • Ovals • Arcs
Logical Fonts • Different systems have different fonts • Java uses logical fonts • Maps logical fonts into system-specific fonts
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)
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
Getting the List • Use the Toolkit • Toolkit is the link between Java and the specific system String fonts[] = Toolkit.getDefaultToolkit().getFontList();
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
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
Font Metrics From the Java Tutorial
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.
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.
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.
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.
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.
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
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
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)
Filled & Unfilled x, y x, y height width
Rectangle Flavors • Outline rectangle drawRect() • Filled (in current foreground color) rectangle fillRect() • Filled (in background color) rectangle clearRect()
Rounded Rectangles x, y height • To draw a rounded rectangle drawRoundRect(x, y, width, height, arcWidth, arcHeight) • Rounded rectangles can also be filled fillRoundRect() arcWidth arcHeight width
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)
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()
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)
A Filled Oval and Its Box x, y height width
Drawing Arcs arcAngle + startAngle • Draws the outline of a circular or elliptical arc covering the specified rectangle (filled arcs too) startAngle
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
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