1 /* 2 * File: DrawableStringsTest.java 3 * 4 * Given an array of strings, center the strings both 5 * vertically and horizontally in the applet window. 6 * 7 * Copyright: Northeast Parallel Architectures Center 8 * 9 */ 10 11 import java.applet.Applet; 12 import java.awt.*; 13 14 public class DrawableStringsTest extends Applet { 15 16 /* 17 * Private variables 18 */ 19 20 private int n; // number of strings 21 private String[] string; // an array of strings to be drawn 22 23 // A reference to a DrawableStrings object: 24 private DrawableStrings drawableStrings; 25 26 // HTML parameters: 27 int bgColor, fgColor; 28 String fontName; 29 int fontStyle, fontSize; 30 31 /* 32 * Public methods 33 * 34 */ 35 36 public void init() { 37 38 // Get the parameters from the HTML document: 39 getParameters(); 40 41 // Instantiate a DrawableString object: 42 drawableStrings = new DrawableStrings( string ); 43 drawableStrings.setColor( new Color( fgColor ) ); 44 drawableStrings.setFontName( fontName ); 45 drawableStrings.setFontStyle( fontStyle ); 46 drawableStrings.setFontSize( fontSize ); 47 48 // Set the background color of the applet: 49 this.setBackground( new Color( bgColor ) ); 50 51 } 52 53 public void paint( Graphics g ) { 54 drawableStrings.centerDraw( this ); 55 } 56 57 /* 58 * Private methods 59 * 60 */ 61 62 private void getParameters() { 63 64 // Get the "numStrings" parameter from the HTML document: 65 String nStr = getParameter( "numStrings" ); 66 // Convert string 'nStr' to an integer: 67 n = ( nStr == null ) ? 0 : Integer.parseInt( nStr ); 68 69 // Instantiate an array of strings: 70 string = new String[n]; 71 72 // Get the strings to be drawn: 73 for ( int i = 0; i < n; i++ ) { 74 string[i] = getParameter( "string" + i ); 75 if ( string[i] == null ) string[i] = ""; 76 } 77 78 // Get the "fontName" parameter from the HTML document: 79 fontName = getParameter( "fontName" ); 80 if ( fontName == null ) fontName = "SansSerif"; 81 82 // Get the "fontStyle" parameter from the HTML document: 83 String fontStyleStr = getParameter( "fontStyle" ); 84 // Default font style is BOLD: 85 if ( fontStyleStr == null ) fontStyleStr = "BOLD"; 86 // Convert font style string to integer: 87 fontStyle = fontStyleStringToInt( fontStyleStr ); 88 89 // Get the "fontSize" parameter from the HTML document: 90 String fontSizeStr = getParameter( "fontSize" ); 91 // Default font size is 18 points: 92 if ( fontSizeStr == null ) fontSizeStr = "18"; 93 // Convert font size string to integer: 94 fontSize = Integer.parseInt( fontSizeStr ); 95 96 // Get the "bgColor" parameter from the HTML document: 97 String bgColorStr = getParameter( "bgColor" ); 98 // Default background color is white: 99 if ( bgColorStr == null ) bgColorStr = "#FFFFFF"; 100 // Convert hexadecimal color string to an integer: 101 bgColor = colorStringToInt( bgColorStr ); 102 103 // Get the "fgColor" parameter from the HTML document: 104 String fgColorStr = getParameter( "fgColor" ); 105 // Default foreground color is black: 106 if ( fgColorStr == null ) fgColorStr = "#000000"; 107 // Convert hexadecimal color string to an integer: 108 fgColor = colorStringToInt( fgColorStr ); 109 110 } 111 112 private int colorStringToInt( String colorString ) { 113 114 // Strip off whitespace from color string: 115 colorString = colorString.trim(); 116 117 // Strip off leading '#' from color string, if necessary: 118 if ( colorString.charAt(0) == '#' ) { 119 colorString = colorString.substring(1); 120 } 121 122 // Convert hexadecimal color string to an integer: 123 return Integer.parseInt( colorString, 16 ); 124 125 } 126 127 private int fontStyleStringToInt( String fontStyleString ) { 128 129 int fontStyle = 0; 130 131 // Strip off whitespace from font style string and 132 // convert to uppercase: 133 fontStyleString = fontStyleString.trim().toUpperCase(); 134 135 // Font styles are additive: 136 if ( fontStyleString.indexOf( "BOLD" ) > -1 ) { 137 fontStyle += Font.BOLD; 138 } 139 if ( fontStyleString.indexOf( "ITALIC" ) > -1 ) { 140 fontStyle += Font.ITALIC; 141 } 142 143 return fontStyle; 144 145 } 146 147 }