Article View: borland.public.jbuilder.java.language
Article #16074Re: don't import java.lang.Math - the question!
From: Fox
Date: Sun, 04 Apr 1999 00:00
Date: Sun, 04 Apr 1999 00:00
268 lines
8755 bytes
8755 bytes
You cannot create an "instance" of the Math object because its constructor is
declared private. This is a REALLY useful technique if you have a specialized
library of routines that really don't need to instantiated as objects.
For example, when working with applets, I allow the use of Netscape color names in
parameters. I feel this is "common" enough material (just as the Math library is) to
set up in the same way:
// this is a final class with a private constructor
// it cannot be instantiated
// it has two functions:
// 1) it checks to see if a string represents a valid "netscape color"
// 2) it returns the integer value of that color
//
// calls to these routines are made just like Math:
// netscapeColors.isNetscapeColor(colorNameString);
// and, netscapeColors.htmlColor(colorString);
// colorString can be 1) a netscape color;
// 2) an html color (#rrggbb)
// 3) a java color (0xaarrggbb or 0xrrggbb)
// note: in java colors, outside a filter, the alpha channel (aa)
// has no effect
// This example also shows how to initialize class "globals" in
// a final class (note the loading of the Hashtable)
// "netscapeColors class by Fox Mahoney ©1998-1999"
// available for free use with appropriate credit (use quote).
// This notice must accompany republished copies of this source code.
// Thank you -- Fox (fox@fxmahoney.com)
import java.lang.*;
import java.util.*;
import java.awt.Color;
public final class netscapeColors extends Object
{
static Hashtable clrs = new Hashtable(140);
static {
clrs.put("aliceblue", "F0F8FF");
clrs.put("antiquewhite", "FAEBD7");
clrs.put("aqua", "00FFFF");
clrs.put("aquamarine", "7FFFD4");
clrs.put("azure", "F0FFFF");
clrs.put("beige", "F5F5DC");
clrs.put("bisque", "FFE4C4");
clrs.put("black", "000000");
clrs.put("blanchedalmond", "FFEBCD");
clrs.put("blue", "0000FF");
clrs.put("blueviolet", "8A2BE2");
clrs.put("brown", "A52A2A");
clrs.put("burlywood", "DEB887");
clrs.put("cadetblue", "5F9EA0");
clrs.put("chartreuse", "7FFF00");
clrs.put("chocolate", "D2691E");
clrs.put("coral", "FF7F50");
clrs.put("cornflowerblue", "6495ED");
clrs.put("cornsilk", "FFF8DC");
clrs.put("crimson", "DC143C");
clrs.put("cyan", "00FFFF");
clrs.put("darkblue", "00008B");
clrs.put("darkcyan", "008B8B");
clrs.put("darkgoldenrod", "B8860B");
clrs.put("darkgray", "A9A9A9");
clrs.put("darkgreen", "006400");
clrs.put("darkkhaki", "BDB76B");
clrs.put("darkmagenta", "8B008B");
clrs.put("darkolivegreen", "556B2F");
clrs.put("darkorange", "FF8C00");
clrs.put("darkorchid", "9932CC");
clrs.put("darkred", "8B0000");
clrs.put("darksalmon", "E9967A");
clrs.put("darkseagreen", "8FBC8F");
clrs.put("darkslateblue", "483D8B");
clrs.put("darkslategray", "2F4F4F");
clrs.put("darkturquoise", "00CED1");
clrs.put("darkviolet", "9400D3");
clrs.put("deeppink", "FF1493");
clrs.put("deepskyblue", "00BFFF");
clrs.put("dimgray", "696969");
clrs.put("dodgerblue", "1E90FF");
clrs.put("firebrick", "B22222");
clrs.put("floralwhite", "FFFAF0");
clrs.put("forestgreen", "228B22");
clrs.put("fuchsia", "FF00FF");
clrs.put("gainsboro", "DCDCDC");
clrs.put("ghostwhite", "F8F8FF");
clrs.put("gold", "FFD700");
clrs.put("goldenrod", "DAA520");
clrs.put("gray", "808080");
clrs.put("green", "008000");
clrs.put("greenyellow", "ADFF2F");
clrs.put("honeydew", "F0FFF0");
clrs.put("hotpink", "FF69B4");
clrs.put("indianred", "CD5C5C");
clrs.put("indigo", "4B0082");
clrs.put("ivory", "FFFFF0");
clrs.put("khaki", "F0E68C");
clrs.put("lavender", "E6E6FA");
clrs.put("lavenderblush", "FFF0F5");
clrs.put("lawngreen", "7CFC00");
clrs.put("lemonchiffon", "FFFACD");
clrs.put("lightblue", "ADD8E6");
clrs.put("lightcoral", "F08080");
clrs.put("lightcyan", "E0FFFF");
clrs.put("lightgoldenrodyellow", "FAFAD2");
clrs.put("lightgreen", "90EE90");
clrs.put("lightgrey", "D3D3D3");
clrs.put("lightpink", "FFB6C1");
clrs.put("lightsalmon", "FFA07A");
clrs.put("lightseagreen", "20B2AA");
clrs.put("lightskyblue", "87CEFA");
clrs.put("lightslategray", "778899");
clrs.put("lightsteelblue", "B0C4DE");
clrs.put("lightyellow", "FFFFE0");
clrs.put("lime", "00FF00");
clrs.put("limegreen", "32CD32");
clrs.put("linen", "FAF0E6");
clrs.put("magenta", "FF00FF");
clrs.put("maroon", "800000");
clrs.put("mediumaquamarine", "66CDAA");
clrs.put("mediumblue", "0000CD");
clrs.put("mediumorchid", "BA55D3");
clrs.put("mediumpurple", "9370DB");
clrs.put("mediumseagreen", "3CB371");
clrs.put("mediumslateblue", "7B68EE");
clrs.put("mediumspringgreen", "00FA9A");
clrs.put("mediumturquoise", "48D1CC");
clrs.put("mediumvioletred", "C71585");
clrs.put("midnightblue", "191970");
clrs.put("mintcream", "F5FFFA");
clrs.put("mistyrose", "FFE4E1");
clrs.put("moccasin", "FFE4B5");
clrs.put("navajowhite", "FFDEAD");
clrs.put("navy", "000080");
clrs.put("oldlace", "FDF5E6");
clrs.put("olive", "808000");
clrs.put("olivedrab", "6B8E23");
clrs.put("orange", "FFA500");
clrs.put("orangered", "FF4500");
clrs.put("orchid", "DA70D6");
clrs.put("palegoldenrod", "EEE8AA");
clrs.put("palegreen", "98FB98");
clrs.put("paleturquoise", "AFEEEE");
clrs.put("palevioletred", "DB7093");
clrs.put("papayawhip", "FFEFD5");
clrs.put("peachpuff", "FFDAB9");
clrs.put("peru", "CD853F");
clrs.put("pink", "FFC0CB");
clrs.put("plum", "DDA0DD");
clrs.put("powderblue", "B0E0E6");
clrs.put("purple", "800080");
clrs.put("red", "FF0000");
clrs.put("rosybrown", "BC8F8F");
clrs.put("royalblue", "4169E1");
clrs.put("saddlebrown", "8B4513");
clrs.put("salmon", "FA8072");
clrs.put("sandybrown", "F4A460");
clrs.put("seagreen", "2E8B57");
clrs.put("seashell", "FFF5EE");
clrs.put("sienna", "A0522D");
clrs.put("silver", "C0C0C0");
clrs.put("skyblue", "87CEEB");
clrs.put("slateblue", "6A5ACD");
clrs.put("slategray", "708090");
clrs.put("snow", "FFFAFA");
clrs.put("springgreen", "00FF7F");
clrs.put("steelblue", "4682B4");
clrs.put("tan", "D2B48C");
clrs.put("teal", "008080");
clrs.put("thistle", "D8BFD8");
clrs.put("tomato", "FF6347");
clrs.put("turquoise", "40E0D0");
clrs.put("violet", "EE82EE");
clrs.put("wheat", "F5DEB3");
clrs.put("white", "FFFFFF");
clrs.put("whitesmoke", "F5F5F5");
clrs.put("yellow", "FFFF00");
clrs.put("yellowgreen", "9ACD32");
} // end hashtable
private netscapeColors (){}
public static boolean
isNetscapeColor(String s)
{
return clrs.containsKey(s);
}
public static Color
htmlColor(String s)
{
Color c = null;
int i;
boolean good = true;
try{
i = Integer.parseInt((String)(clrs.get(s)),16);
c = new Color(i);
}
catch(NumberFormatException n)
{
good = false;
}
if(!good)
{
if(s.indexOf("#") != -1){
try{
i = Integer.parseInt(s.substring(1),16);
c = new Color(i);
}
catch(NumberFormatException n)
{}
}
else
if(s.indexOf("0x") != -1){
try{
i = Integer.parseInt(s.substring(2),16);
c = new Color(i);
}
catch(NumberFormatException n)
{}
}
else
c = Color.black;
}
return c;
}
}
//-----------------------------------------------------
Hope this helps,
Fox
**********************
Mike Booth wrote:
>
> I have an object that refuses to import java.lang.Math! If I try to use the
> pow() method later on, it says it can't find the method in my object. If I
> prefix the object with java.lang.Math.pow(), everything works fine.
>
> Does anyone have any ideas? It's driving me mad.
>
> Thanks in advance,
> Mike.
Technical Details
Message-ID:
<3707D050.D5BBB9FB@eatel.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7e80ba$r1h$1@news5.svr.pol.co.uk>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 10a146,1f5836345807c717 X-Google-Attributes: gid10a146,public X-Google-Thread: f5d71,1f5836345807c717 X-Google-Attributes: gidf5d71,public X-Google-Thread: f74b3,1f5836345807c717 X-Google-Attributes: gidf74b3,public From: Fox <mahoney@eatel.net> Subject: Re: don't import java.lang.Math - the question! Date: 1999/04/04 Message-ID: <3707D050.D5BBB9FB@eatel.net> X-Deja-AN: 462653847 Content-Transfer-Encoding: quoted-printable References: <7e80ba$r1h$1@news5.svr.pol.co.uk> Content-Type: text/plain; charset=iso-8859-1 X-Trace: news2.giganews.com 923258977 206.124.220.170 (Sun, 04 Apr 1999 15:49:37 CDT) MIME-Version: 1.0 Reply-To: fox@fxmahoney.com NNTP-Posting-Date: Sun, 04 Apr 1999 15:49:37 CDT Newsgroups: borland.public.jbuilder.java.language,comp.lang.java,comp.lang.java.programmer