/* * VJApplet.java - Visual Java Applet superclass. * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.applet.Applet; import util.comm.DataChannel; import java.awt.Graphics; import java.awt.Font; import java.awt.Color; import java.awt.Image; /** * This class provides some of the basic helper methods for all * of the VJControls. It also double buffers all subclasses of * itself. This makes the code in the control applets simpler and * easier to read. */ public class VJApplet extends Applet { /** * Collect an Integer parameter from the applet parameters. */ public int getInteger(String param, int defaultValue) { try { return (Integer.parseInt(getParameter(param))); } catch (NumberFormatException e) { System.out.println("Invalid number syntax for : "+param); return defaultValue; } catch (NullPointerException e) { return defaultValue; } } /** * Get a string from the parameter list. */ public String getString(String name, String defaultName) { String x = getParameter(name); return ((x == null) ? defaultName : x); } /** * Collect a Double parameter from the applet parameters. */ public double getDouble(String param, double defaultValue) { try { return ((Double.valueOf(getParameter(param))).doubleValue()); } catch (NumberFormatException e) { System.out.println("Invalid number format for : "+param); return defaultValue; } catch (NullPointerException e) { return defaultValue; } } /** * return one of the "primary" colors. */ private Color getPrimaryColor(String xcolor) { if (xcolor.equalsIgnoreCase("red")) { return Color.red; } else if (xcolor.equalsIgnoreCase("green")) { return Color.green; } else if (xcolor.equalsIgnoreCase("yellow")) { return Color.yellow; } else if (xcolor.equalsIgnoreCase("blue")) { return Color.blue; } else if (xcolor.equalsIgnoreCase("cyan")) { return Color.cyan; } else if (xcolor.equalsIgnoreCase("gray")) { return Color.gray; } else if (xcolor.equalsIgnoreCase("magenta")) { return Color.magenta; } else if (xcolor.equalsIgnoreCase("orange")) { return Color.orange; } else if (xcolor.equalsIgnoreCase("pink")) { return Color.pink; } else if (xcolor.equalsIgnoreCase("white")) { return Color.white; } else if (xcolor.equalsIgnoreCase("black")) { return Color.black; } return null; } private Color returnPrimaryColor(String x, Color defaultColor) { Color y = getPrimaryColor(x); if (y != null) return y; else { System.out.println("Unrecognized color named : "+x); return defaultColor; } } /** * Get a color from the parameter list. The color may * be specified as either a name, such as 'red', or a name * with a modifier such as 'lightred' or 'darkred', or as * a series of hexadecimal digits for the red, green, and * blue components of a 24 bit color as in '#fffff0' (paper white) * */ public Color getColor(String name, Color defaultColor) { String x = getParameter(name); if (x == null) return defaultColor; // # introduces the numeric colors. if (x.startsWith("#")) { int z; try { z = Integer.parseInt(x.substring(1), 16); return( new Color((z >>> 16) & 0xff, (z >>> 8) & 0xff, (z & 0xff))); } catch (NumberFormatException e) { System.out.println("Badly specified color for : "+name); return defaultColor; } } Color y; // if len(x) < 5 then it can't have 'light' or 'dark' with it if (x.length() < 5) return (returnPrimaryColor(x, defaultColor)); // check for 'dark' and check for 'light' if (x.substring(0, 4).equalsIgnoreCase("dark")) { y = getPrimaryColor(x.substring(4)); if (y != null) return y.darker(); else { System.out.println("Badly specified dark color : "+x); return defaultColor; } } // check for the light case if (x.substring(0, 5).equalsIgnoreCase("light")) { y = getPrimaryColor(x.substring(5)); if (y != null) { return y.brighter(); } else { System.out.println("Badly specified light color : "+x); return defaultColor; } } return (returnPrimaryColor(x, defaultColor)); } /** * This draws a raised rectangle look of varying thickness. */ void rect3D(Graphics g, int x, int y, int w, int h, int thick, boolean raised) { h--; w--; g.setColor(raised ? dkColor : brColor); for (int i = 0; i < thick; i++) { g.drawLine(x+i, y+h - i, x+w - i, y+h -i); g.drawLine(x+w - i, y+h - i, x+w - i, y+i); } g.setColor(raised ? brColor : dkColor); for (int i = 0; i < thick; i++) { g.drawLine(i+x, y+h - i, x+i, y+i); g.drawLine(i+x, y+i, x+w - i, y+i); } } Graphics offscreen; Image altImage; int myWidth, myHeight; public void update(Graphics g) { // try to detect when we need a new offscreen buffer if ((offscreen == null) || (myWidth != size().width) || (myHeight != size().height)) { if (offscreen != null) { System.out.println("Disposing graphics context..."); offscreen.dispose(); // cleanse the old one. } myWidth = size().width; myHeight = size().height; altImage = createImage(myWidth, myHeight); offscreen = altImage.getGraphics(); } paint(offscreen); // update our display. g.drawImage(altImage, 0, 0, null); } Color bgColor; Color dkColor; Color brColor; public void init() { bgColor = getColor("bgcolor", Color.lightGray); dkColor = bgColor.darker(); brColor = bgColor.brighter(); } }