1 /* 2 * File: DrawableStrings.java 3 * 4 * A DrawableStrings class with centering capability 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.awt.*; 11 12 public class DrawableStrings extends Font { 13 14 // Use mutators to set private class variables: 15 private int n = 0; // number of strings 16 private String[] string; // strings to be drawn 17 private Color color; // foreground color 18 19 // This constructor takes an array of strings: 20 public DrawableStrings( String[] string ) { 21 super( "SansSerif", Font.BOLD, 18 ); 22 this.color = Color.black; 23 setString( string ); 24 } 25 26 // To do: write a constructor that takes a single string... 27 28 // This mutator takes an array of strings: 29 public void setString( String[] string ) { 30 n = string.length; 31 this.string = new String[n]; 32 for ( int i = 0; i < n; i++ ) { 33 this.string[i] = string[i]; 34 } 35 } 36 // This mutator takes a single string: 37 public void setString( String string ) { 38 String[] s = { string }; 39 setString( s ); 40 } 41 42 // Color accessor and mutator: 43 public Color getColor() { return this.color; } 44 public void setColor( Color color ) { 45 if ( color != null ) this.color = color; 46 } 47 48 // Variables name, style, and size are inherited from Font: 49 public void setFontName( String name ) { 50 if ( name != null ) this.name = name; 51 } 52 public void setFontStyle( int style ) { 53 this.style = style; 54 } 55 public void setFontSize( int size ) { 56 this.size = size; 57 } 58 59 // Center the strings in the given component: 60 public void centerDraw( Component c ) { 61 62 // If there are no strings to draw, do nothing: 63 if ( n == 0 ) return; 64 65 // Save the current foreground color: 66 if ( this.color != null ) { 67 Color color = c.getForeground(); 68 c.setForeground( this.color ); 69 } 70 71 // Get a graphics object for this component: 72 Graphics g = c.getGraphics(); 73 74 // Methods getName(), getStyle(), and getSize() are 75 // inherited from Font: 76 g.setFont( new Font( getName(), getStyle(), getSize() ) ); 77 78 // Get a FontMetrics object: 79 FontMetrics fm = g.getFontMetrics(); 80 81 // Determine the width of the component: 82 int componentWidth = c.getSize().width; 83 84 // Determine the width of the first string: 85 int stringWidth = fm.stringWidth( this.string[0] ); 86 87 // Calculate the x-coordinate of the first string: 88 int x = ( componentWidth - stringWidth )/2; 89 90 // Determine the height of the component: 91 int componentHeight = c.getSize().height; 92 93 // Calculate the height of *any* string: 94 int ascent = fm.getAscent(); 95 int descent = fm.getDescent(); 96 int stringHeight = ascent + descent; 97 98 // Calculate the height of n strings: 99 int leading = fm.getLeading(); 100 int height = n * stringHeight + (n - 1) * leading; 101 102 // Calculate the y-coordinate of the first string: 103 int y = ( componentHeight - height )/2 + ascent; 104 105 // Draw the first string at point (x,y): 106 g.drawString( this.string[0], x, y ); 107 108 // Calculate the font height: 109 int fontHeight = stringHeight + leading; 110 111 for ( int i = 1; i < n; i++ ) { 112 113 // Calculate the width of this string: 114 stringWidth = fm.stringWidth( this.string[i] ); 115 116 // Calculate the x-coordinate of this string: 117 x = ( componentWidth - stringWidth )/2; 118 119 // Calculate the y-coordinate of this string: 120 y += fontHeight; 121 122 // Draw the current string at point (x,y): 123 g.drawString( this.string[i], x, y ); 124 125 } 126 127 // Restore the original foreground color: 128 c.setForeground( color ); 129 130 } 131 132 }