| C:\upload\java\Life\source\java\GenerationNumberCanvas.java |
/******************************************************************* This class takes care of the canvas on which the generation number is painted. ******************************************************************** */ import java.awt.*; final class GenerationNumberCanvas extends Canvas implements Constants, Strings { private Font genFont; // Font we use in displaying private PopupWindow error; // In case of a mistake. private Frame theFrame; // To be used with < error >. private Dimension size; // How big we are. private boolean trueSizeKnown; /* A boolean, set "true" when the window know's its size. */ Vars vars; /******************************************************************* Constructor follows: ******************************************************************** */ GenerationNumberCanvas(Frame parentFrame, ScreenLocations screenLocations, int w, int h) /* Initial guess at my size. Will change. */ { String errorString = ((parentFrame != null) && (screenLocations != null)) ? NULL_STRING : "parentFrame = " + parentFrame + " screenLocations = " + screenLocations; if (!errorString.equals(NULL_STRING)) { System.out.println(errorString); return; } error = new PopupWindow(parentFrame, screenLocations, ERROR_WINDOW_NAME); size = new Dimension(w, h); // Will be re-set genFont = new Font("TimeRoman", Font.BOLD, 25); trueSizeKnown = false; // We don't know our true size yet. } /******************************************************************* Get variables in parameter *vars*. ******************************************************************** */ protected final void SetVars(Vars vars) { this.vars = vars; } /******************************************************************* Over-ride next for good measure. ******************************************************************** */ public void update(Graphics g) { super.paint(g); } /******************************************************************* The routine < paint(Graphics g) > follows: ******************************************************************** */ public void paint(Graphics g) { /* We are very, very careful. Perhaps we have not been fed our true size, or an essential parameter is garbage. Best be safe. Do nothing if something isn't right. */ boolean showGenerationNumber = trueSizeKnown && (g != null) && (genFont != null); if (showGenerationNumber) { /* Steps: clear rectangle, set the background color, fill rectangle with said background color, set the font, set the color of the text, paint the text. */ Point loc = getLocation(); // Get int xVal = loc.x; // local int yVal = loc.y; // copies int height = getSize().height; // of int width = getSize().width; // these. g.clearRect(xVal, yVal - height, width*2, 2*height); g.setColor(COLOR_BACKGROUND); g. fillRect(xVal, yVal - height, width*2, 2*height); g.setFont(genFont); // Get g.setColor(COLOR_OF_GENERATION_NUMBER); // ready, and ... g.drawString("" + vars.GetGenerationNumber(), // Paint xVal, yVal); // it. } else // A needed parameter is garbage. { boolean gNull = (g == null); boolean genFontNull = (genFont == null); String errorString = GARBAGE_VALUE_OF_PARAMETER + " < trueSizeKnown > = " + trueSizeKnown + ", g " + NULL + ": " + gNull + ", genFont " + NULL + ": " + genFontNull + ". "; System.out.println(errorString); } } // End < paint() > routine. /******************************************************************* Over-ridden routines that follow take care of dimension problems. Note how < setSize > sets a boolean telling the code, "I know how big I am." ******************************************************************** */ public Dimension getSize() // Tell caller our size. Over-ridden. { return getMinimumSize(); } public Dimension getMinimumSize() // Tell caller our min size. Over-ridden. { return size; } public Dimension getPreferredSize() // Tell caller our max size. Over-ridden. { return getMinimumSize(); } /* public void setLocation() // Tell caller our location. Over-ridden. { // qz why did this crash Netscape? if ((vars != null ) && (vars.GetGenNumLocation() != null)) { super.setLocation(vars.GetGenNumLocation()); } } */ /******************************************************************* Next sets a boolean telling the code, "I know how big I am." ******************************************************************** */ public void setSize(int width, int height) /* Set our size. Over-ridden. */ { size = new Dimension(width, height); trueSizeKnown = true; /* That boolean. The main code of this class knows that our size has been set. */ } } // End class < GenerationNumberCanvas >.