/Users/lyon/j4p/src/bookExamples/ch26Graphics/radar/Target.java

1    package bookExamples.ch26Graphics.radar; 
2     
3    import java.awt.FontMetrics; 
4     
5    public class Target { 
6        String s; 
7        int x; 
8        int y; 
9     
10       Target(String _s, int _x, int _y) { 
11           s = _s; 
12           x = _x; 
13           y = _y; 
14       } 
15    
16       public void draw(java.awt.Graphics g) { 
17           int str_height = g.getFontMetrics().getHeight(); 
18           int y1 = y - (str_height * s.length()) / 2; 
19           for (int i = 0; i < s.length(); i++) { 
20               FontMetrics fontMetrics = g.getFontMetrics(); 
21               int char_width = 
22                       fontMetrics.stringWidth( 
23                               s.substring(i, i + 1)); 
24               g.drawString( 
25                       s.substring(i, i + 1), x - char_width / 2, y1); 
26               y1 += str_height; 
27           } 
28       } 
29   }