/* * RandCharModel.java * * Created on February 5, 2003, 5:06 PM */ package randchars; import MDUtils.*; import java.awt.Component; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; /** Maintain a list of randomly generated characters. * When the list changes, a viewer is notified. * * @author dickey 2/2003 */ public class RandCharModel implements IPublicInformation { /** All the characters picked so far, in order, the most recent last. */ List chars; /** All the characters picked so far, as a set. */ Set charSet; /** The first and last characters of the block of Unicode characters, * within which the random selection is made. */ char firstCharInRange, lastCharInRange; /** The component responsible for displaying the characters and status. */ Component viewer; /** Object id -- incremented by 1 for each object. */ private static char OBid = 'a'; /** Creates a new instance of RandCharsMode. * @param firstChar The first character of a range within which (inclusive) * the random characters will be generated. * @param lastChar The last character of the ranged. * @param viewer The component that should be notified (via a call to * repaint()) that the model has changed. */ public RandCharModel(char firstChar, char lastChar) { this.chars = new ArrayList(); this.charSet = new HashSet(); this.firstCharInRange = firstChar; this.lastCharInRange = lastChar; this.OBid ++; } /** Generate a random character, between the limits set earlier, * and remember it at the end of the list (and in the set). */ public void randomlyAddChar() { char newChar = (char) ((int) firstCharInRange + (Math.random() * (lastCharInRange - firstCharInRange + 1))); assert newChar >= firstCharInRange; assert newChar <= lastCharInRange; boolean listAddStatus = chars.add(new Character(newChar)); assert listAddStatus; charSet.add(new Character(newChar)); //end randomlyAddChar } /** @return a copy of the current list of characters. */ public List getCharList() { return new ArrayList(chars); } /** Tells a small amount of author information. * @return a "short string" identifying the author(s) * of the class. * **** IF YOU DO NOT WISH TO BE PUBLICALLY IDENTIFIED, return a pseudonym or * code name. */ public String getAuthor() { return "anonymous"; } /** Return a preferred color for the object. Colors might be used by * clients to set a background color, a border color, etc. * @return A Color, or null if there is no preferred color. */ public java.awt.Color getColor() { return java.awt.Color.cyan; } /** Gives a URL to an image representing this class. * @return a string which is a full URL to a .jpg or .gif file. The * image is expected to in some way stand for or depict the class. It * is acceptable to return null. */ public String getImageURLString() { String[] images = {"http://www.cs.washington.edu/images/sptm133.jpg", "http://lazowska.cs.washington.edu/sieg/siegfrontsm.jpg", "http://www.cs.washington.edu/images/cse_logo_133.gif" }; int iCount = images.length; int index = (int) (images.length * Math.random()); assert index >= 0 && index < images.length; String istring = images[index]; return istring; //return "http://www.unicode.org/img/globe_iuc23.gif"; //return "C:\\DELL\\DOCS\\Bedirect.jpg"; } /** Return a "long string" describing the object or how to use it. */ public String getInstructions() { return null; } /** Tell the name of this class (or better, what this class represents). * @return A short string with the name of the oracle; there should be * no leading or trailing whitespace. The same value should be returned * for all objects of this type (a "static final" instance variable would * help to enforce this). * */ public String getName() { return "Random Unicode Character Generator"; } /** Return a "long string" describing this object. The description might * be the same for all objects of the type. */ public String getObjectDescription() { return "Random Character Generator. The characters will be " + "in the range from " + this.firstCharInRange + " (" + charUnicodeID (firstCharInRange)+ ")" + " to " + this.lastCharInRange + " (" + charUnicodeID(lastCharInRange) + ")"; } /** Generate a string identifying the unicode hex value and block of the char. * @return a string containing the identifying information, with no leading * or trailing spaces, and not containing the character itself. */ public static String charUnicodeID(char c) { String retString = "u" + Integer.toHexString((int) c) + " in Unicode " + Character.UnicodeBlock.of(c) + " block"; return retString; } /** Return a short string identifying this object. The object name should * be unique for each object instantiated. */ public String getObjectName() { return "RandChar Gen #" + OBid; } // end class RandCharModel }