/* * RandCharPanel.java ------------ starter code version -------------- * * Created on February 5, 2003, 5:04 PM */ package randchars; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Point; import javax.swing.JPanel; import MDUtils.*; /** A panel which displays a list of characters. * A controller or model should activate this viewer by calling repaint(); * * @author dickey */ public class OuijaViewer extends JPanel implements IPublicInformation { private static final int panelWidth = 400; private static final int panelHeight = 200; /** Position in the panel where we want the first character to display. */ private int xStart = 10, yStart = 30; /** Size of a character area on the screen; the area may include more * than just the single character (for example). */ private int charWidth, charHeight; /** The model of the list of characters. */ private RandCharsModel charsModel; /** A code prefix that is the same for each object. */ private final static String IDPrefix = "Ouija Panel-"; /** A suffix that is unique for each object.*/ private static char objtag = 'a'; /** A value that is unique for each object. */ private String objectID; private Font niceFont; /** Creates a new instance of OuijaPanel */ //shouldn't need changing. public OuijaViewer(RandCharsModel charsModel) { assert charsModel != null; this.charsModel = charsModel; this.setPreferredSize(new java.awt.Dimension(panelWidth, panelHeight)); this.objtag = (char) (1 + (int) objtag); this.objectID = IDPrefix + objtag; this.niceFont = new Font("Arial Unicode MS", Font.BOLD, 24); if (this.niceFont == null) { this.niceFont = new Font("SansSerif", Font.BOLD, 24); //if the result is still null, paint will use whatever it can. } } /** Paint the screen with information from the model. */ public void paintComponent(java.awt.Graphics origG) { java.awt.Graphics2D g = (java.awt.Graphics2D) origG; this.setBackground(this.getColor()); super.paintComponent(g); if (this.niceFont != null) { g.setFont(this.niceFont); } g.setColor(new Color(180, 40, (int) (50 * Math.random()))); //random dark red Point position = new Point(xStart, yStart); //place to draw the next char. //-------- add code here ------ // --- get data (chars) from the model, draw it on the panel // --- update the position before drawing each character //end paint } /** Update the drawing position for next time. */ //no changes needed. private void updatePosition(Point position, String lastString, FontMetrics fm) { int latestWidth = fm.stringWidth(lastString); position.x += latestWidth; if (position.x + latestWidth > panelWidth) { //start a new line position.x = xStart; position.y += fm.getHeight(); if (position.y >= panelHeight) { //at bottom of panel already -- no room. Wrap to top. position.y = yStart; } } } /** 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 PUBLICLY IDENTIFIED, return a pseudonym or * code name. */ public String getAuthor() { return "Gess Hoo"; } /** 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 Color getColor() { return new Color(0xff, 0xaa, 20 + (int)(30 * Math.random())); //random brown } /** 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() { //return "C:\\Documents and Settings\\dickey\\My Documents\\My Pictures\\2003_01_19 & 20\\144-4444_IMG.JPG"; return "http://www.prairieghosts.com/ouija1.jpg"; //return "http://www.unicode.org/img/globe_iuc23.gif"; } /** Return a "long string" describing the object or how to use it. */ public String getInstructions() { return "Tap on the Ouija Board to see the next character"; } /** Return a "long string" describing this object. The description might * be the same for all objects of the type. */ public String getObjectDescription() { String retString = "The Ouija Board is like a telegraph receiver." + " It hears messages coming through the ether from the spirit world, " + "and reveals them to you."; //" For Unicode character charts, see http://www.unicode.org/charts/"; String fontName = ""; if (this.niceFont != null) { fontName = this.niceFont.getFontName(); } else { fontName = " a default."; } retString += " The font in use is " + fontName + "."; return retString; } /** Return a short string identifying this object. The object name should * be unique for each object instantiated. */ public String getObjectName() { return this.objectID; } public String toString() { return this.getObjectName(); } //end class RandCharPanel }