//----------------------------------------------------------------------------// // GetAppletParameter.java // // ----------------------- // // This example is from Developer's Daily (http://www.DevDaily.com). // // Copyright (c) 1998 DevDaily Interactive, Inc. // // This example is provided WITHOUT ANY WARRANTY either expressed or implied. // // You may study, use, modify, and distribute it for non-commercial purposes // // as long as this header is retained in the file. // // For any commercial use, contact our editor (editor@DevDaily.com). // //----------------------------------------------------------------------------// // Note: This class borrows from, and was inspired by, the GetParm class // // of the "Instant Java" book by John A. Pew. // //----------------------------------------------------------------------------// import java.applet.Applet; import java.awt.Color; /** * The GetParameter class is used to retrieve parameters that are pass to * applets via the tag in an HTML document. * * @version 1.1 * @author DevDaily Interactive, Inc. * */ public class GetAppletParameter { //----------------------------------------------// // Methods implemented in this class: // // ---------------------------------- // // toColor() // // toString() // // toInt() // // toLong() // // toFloat() // // toDouble() // // toBoolean() // // toChar() // //----------------------------------------------// //------------------------[ toColor() ]-------------------------------// /** * Converts an applet input parameter to a Color object * @return A Color object * @param paramName A String containing the name of the parameter to retrieve. * @param defaultColor A Color object containing the default color that should be used in case the parameter is not supplied or the conversion fails. */ public static Color toColor (Applet applet, String paramName, Color defaultColor) { Color color; String s = applet.getParameter(paramName); if (s == null) // if the parameter was not supplied, just return the defaultColor return defaultColor; else { try { // string should begin with '#' character (something like #996666) if (s.charAt(0) == '#') { char chars[]; chars = new char[s.length()]; s.getChars(0,s.length(), chars, 0); color = new Color(Integer.parseInt(new String(chars, 1, s.length()-1), 16)); //return (new Color(Integer.parseInt(new String(chars, 1, s.length()-1), 16))); return (color); } else { color = new Color(Integer.parseInt(s,16)); return (color); } } catch (NumberFormatException nfe) { System.out.println("Invalid color specification: " + nfe.getMessage()); return null; } // end try-catch } // end if-else } // end toColor() //------------------------[ toString() ]-------------------------------// /** * A convenience method, returns the value of a String parameter * @return A String object * @param paramName A String containing the name of the parameter to retrieve. * @param defaultString A String to use in case it was not supplied in the HTML code. */ public static String toString(Applet applet, String paramName, String defaultString) { String theParam = applet.getParameter(paramName); if (theParam == null) // if the parameter was not supplied, just return the defaultString return defaultString; else { return(theParam); } // end if-else } // end toString() //------------------------[ toInt() ]-------------------------------// /** * Converts an applet parameter to an int value. * @return An int * @param paramName A String containing the name of the parameter to retrieve. * @param defaultInt An int to use as a default in case the parameter is not supplied. */ public static int toInt(Applet applet, String paramName, int defaultInt) { String theParam = applet.getParameter(paramName); if (theParam == null) return defaultInt; // if parameter is not supplied, just return defaultInt try { return(Integer.parseInt(theParam)); // return an int type, not a Integer object } catch (NumberFormatException nfe) { System.out.println("Invalid int specification: " + nfe.getMessage()); return defaultInt; } } //------------------------[ toLong() ]-------------------------------// /** * Converts an applet parameter to a long value. * @return A long * @param paramName A String containing the name of the parameter to retrieve. * @param defaultLong A long to use as a default in case the parameter is not supplied. */ public static long toLong(Applet applet, String paramName, int defaultLong) { String theParam = applet.getParameter(paramName); // if parameter not supplied, just return defaultLong if (theParam == null) return defaultLong; try { return(Long.parseLong(theParam)); // return a long type, not a Long object } catch (NumberFormatException nfe) { System.out.println("Invalid long specification: " + nfe.getMessage()); return defaultLong; } } //------------------------[ toFloat() ]-------------------------------// /** * Converts an applet parameter to a float value. * @return A float value * @param paramName A String containing the name of the parameter to retrieve. * @param defaultFloat A float to use as a default in case the parameter is not supplied. */ public static float toFloat(Applet applet, String paramName, float defaultFloat) { String theParam = applet.getParameter(paramName); // if parameter is not supplied, just return defaultFloat if (theParam == null) return defaultFloat; try { return(new Float(theParam).floatValue()); // returns a float, not a Float } catch (NumberFormatException nfe) { System.out.println("Invalid float specification: " + nfe.getMessage()); return defaultFloat; } } //------------------------[ toDouble() ]-------------------------------// /** * Converts an applet parameter to a double value. * @return A double value * @param paramName A String containing the name of the parameter to retrieve. * @param defaultDouble A double to use as a default in case the parameter is not supplied. */ public static double toDouble(Applet applet, String paramName, double defaultDouble) { String theParam = applet.getParameter(paramName); // if parameter is not supplied, just return defaultDouble if (theParam == null) return defaultDouble; try { return(new Double(theParam).doubleValue()); // returns a double, not a Double } catch (NumberFormatException nfe) { System.out.println("Invalid double specification: " + nfe.getMessage()); return defaultDouble; } } //------------------------[ toBoolean() ]-------------------------------// /** * * Converts an applet parameter to a boolean value. * @return A boolean object * @param paramName A String containing the name of the parameter to retrieve. * @param defaultBoo A boolean to use as a default in case the parameter is not supplied. */ public static boolean toBoolean(Applet applet, String paramName, boolean defaultBoo) { String theParam = applet.getParameter(paramName); // if the parameter was not supplied, just return defaultBoo if (theParam == null) return defaultBoo; // returns true if string is not null and is equal, ignoring case, to the string "true" // otherwise, the object will be false (note: the null case is actually handled above // for a little more flexibility) Boolean b = new Boolean(theParam); return b.booleanValue(); } // end toBoolean() //------------------------[ toChar() ]-------------------------------// /** * Converts an applet input parameter to a char value * @return A char value * @param paramName A String containing the name of the parameter to retrieve. * @param defaultColor A default character to use in case the parameter is not * supplied or the conversion fails. */ public static char toChar (Applet applet, String paramName, char defaultChar) { String s = applet.getParameter(paramName); // if the parameter was not supplied, just return the defaultChar if (s == null) return defaultChar; // the next statement *assumes* that the html code passed us one character; it // makes no effort to check the actual string length. If an entire String is // given, this method returns only the first character of the String. return(s.charAt(0)); } } // end of GetAppletParameter class