Draw a string on a picture at the top of the picture and centered horizontally. You will need to use the FontMetrics class to get the height of the string in pixels in order to determine where the baseline should be so that the string is visible. You should also subtract the descent from the height.

What will be an ideal response?

```
public void drawTopCenteredString ( String text )
{
// g e t the g r a p h i c s o b j e c t
Graphics g = this . ge tGraphi c s ( ) ;
// c r e a t e the f ont o b j e c t
Font f ont = new Font ( "Ar i a l " , Font .BOLD, 2 4 ) ;
// s e t the c o l o r
g . s e tCo l o r ( Color . black ) ;
// s e t the f ont
g . setFont ( f ont ) ;
// g e t the f ont me t r i c s
FontMet r ics f ontMe t r i c s = g . ge tFontMe t r i c s ( ) ;
// g e t the width of the s t r i n g
int strWidth = f ontMe t r i c s . s t r ingWidth ( t ext ) ;
// c a l c u l a t e the c ent e r of the p i c t u r e
int c ent e r = ( int ) ( this . getWidth ( ) * 0 . 5 ) ;
// c a l c u l a t e the b a s e l i n e Y of the f ont
int y = f ontMe t r i c s . ge tHe ight ( ) -
f ontMe t r i c s . ge tDe s c ent ( ) ;
// draw the s t r i n g c ent e r ed in x
g . drawSt r ing ( text ,
c ent e r - ( int ) ( strWidth * 0 . 5 ) ,
y ) ;
}
```

Computer Science & Information Technology

You might also like to view...

Click Print on the ________ tab to display print settings available for the slide presentation

Fill in the blank(s) with correct word

Computer Science & Information Technology

________ in PowerPoint refers to the ease with which a person with physical challenges can access, use, and understand a presentation

Fill in the blank(s) with correct word

Computer Science & Information Technology