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...

The storage capacity and format for each data type can be adjusted in the Field Property box

Indicate whether the statement is true or false

Computer Science & Information Technology

Iteration is a design process the involves,____________ , ________________ and ____________

Fill in the blank(s) with correct word

Computer Science & Information Technology

The If...Then...Else statement is similar to the Excel ____________________ function.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

The figure below illustrates a system in which operations occur as three consecutive clock cycles. Actions taking place in clock cycle 1 are scalable; that is, if the clock cycle time changes, the actions can be speeded up or slowed down correspondingly. In cycle 2, the action process 1 requires 25 ns and in clock cycle 3 the action process 2 requires 32 ns. If the clock cycle is less than the time required for process 1 or process 2, then one or more wait cycles have to be inserted for the process to complete.



Cycle and process times
What is the time to complete an operation if the clock cycle time is
a. 50 ns
b. 40 ns
c. 30 ns
d. 20 ns
e. 10 ns

Computer Science & Information Technology