import java.awt.Color; import java.io.*; import java.util.Scanner; import java.util.Vector; import java.util.Hashtable; /** * A class that can be used to retrieve Color objects * by name (using the W3C names defined in * http://www.w3.org/TR/2003/CR-css3-color-20030514/ ) */ public class ColorFinder { // original // private Color[] colors; // private String[] names; // part 4 // private Vector colors; // private Vector names; // part 5 // private Vector colors; // private Vector names; // part 6 private Hashtable colors; /** * Default Constructor */ public ColorFinder() throws IOException { Color value; int blue, green, red; Scanner in; String key; // original // colors = new Color[147]; // names = new String[147]; // part 4 // colors = new Vector(); // or colors = new Vector (147); // names = new Vector (); // or names = new Vector (147); // part 5 // colors = new Vector () ; // names = new Vector (); // part 6 colors = new Hashtable (); in = new Scanner(new File("colors.txt")); for (int i=0; in.hasNext(); i++) { key = in.next(); red = in.nextInt(); green = in.nextInt(); blue = in.nextInt(); value = new Color(red, green, blue); // original // names[i] = key; // colors[i] = value; // parts 4 & 5 // names.add(key); // colors.add(value); // part 6 colors.put (key, value); } } /** * Get the Color with the given name/description * * @param description The name/description of the Color * @return The Color (or null if no such name exists) */ public Color getColor(String description) { Color result; // parts 3,4,& 5 // String key; result = null; // parts 3,4,& 5 // for (int i = 0; i < names.size() && result == null; i++) // { //part 4 // key = (String) names.elementAt(i); // part 5 // key = names.elementAt(i); // if (description.equals(key)) // { // part 4 // result = (Color) colors.elementAt(i); // part 5 //result = colors.elementAt(i); // part 6 result = colors.get(description); // } // } return result; } }