Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS 102-02 Lecture 7-1 A picture's worth a thousand words."— Presentation transcript:

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

2 May 11, 1998CS102-02Lecture 7-1 Agenda Fonts Lines Rectangles Ovals Arcs

3 May 11, 1998CS102-02Lecture 7-1 Logical Fonts Different systems have different fonts Java uses logical fonts –Maps logical fonts into system-specific fonts

4 May 11, 1998CS102-02Lecture 7-1 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 May 11, 1998CS102-02Lecture 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

6 May 11, 1998CS102-02Lecture 7-1 Getting the List Use the Toolkit Toolkit is the link between Java and the specific system String fonts[] = Toolkit.getDefaultToolkit().getFontList();

7 May 11, 1998CS102-02Lecture 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

8 May 11, 1998CS102-02Lecture 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

9 May 11, 1998CS102-02Lecture 7-1 Font Metrics From the Java Tutorial

10 May 11, 1998CS102-02Lecture 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.

11 May 11, 1998CS102-02Lecture 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.

12 May 11, 1998CS102-02Lecture 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.

13 May 11, 1998CS102-02Lecture 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.

14 May 11, 1998CS102-02Lecture 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.

15 May 11, 1998CS102-02Lecture 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

16 May 11, 1998CS102-02Lecture 7-1 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 May 11, 1998CS102-02Lecture 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 rectangle drawRect( int x,// top-left int y,// coordinate int width, int height)

18 May 11, 1998CS102-02Lecture 7-1 Filled & Unfilled x, y width heightheight

19 May 11, 1998CS102-02Lecture 7-1 Rectangle Flavors Outline rectangle drawRect() Filled (in current foreground color) rectangle fillRect() Filled (in background color) rectangle clearRect()

20 May 11, 1998CS102-02Lecture 7-1 Rounded Rectangles To draw a rounded rectangle drawRoundRect(x, y, width, height, arcWidth, arcHeight) Rounded rectangles can also be filled fillRoundRect() arcWidth arcHeight width heightheight x, y

21 May 11, 1998CS102-02Lecture 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 corner draw3DRect(int x, int y, int width, int height, boolean raised)

22 May 11, 1998CS102-02Lecture 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 with fill3DRect()

23 May 11, 1998CS102-02Lecture 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-left int y,// coordinate int width, int height)

24 May 11, 1998CS102-02Lecture 7-1 A Filled Oval and Its Box x, y width heightheight

25 May 11, 1998CS102-02Lecture 7-1 Drawing Arcs Draws the outline of a circular or elliptical arc covering the specified rectangle (filled arcs too) startAngle arcAngle + startAngle

26 May 11, 1998CS102-02Lecture 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

27 May 11, 1998CS102-02Lecture 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


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

Similar presentations


Ads by Google