/* * Password.java * * 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 java.awt.*; import util.comm.*; public class Password extends Applet { DataChannel dc; Color bgColor; /** * return one of the "primary" colors. */ 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; } return null; } /** * Return a Color value from the parameter list. Colors are * specified using HEX notation as in NetScape. If there is any * error, return the defaultColor instead. */ public Color getColor(String name, Color defaultColor) { String x = getParameter(name); if (x != null) { if (x.startsWith("0x")) { int z; try { z = Integer.parseInt(x.substring(2), 16); return( new Color((z >>> 16) & 0xff, (z >>> 8) & 0xff, (z & 0xff))); } catch (NumberFormatException e) { return defaultColor; } } else 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) { return defaultColor; } } else if (x.substring(0, 4).equalsIgnoreCase("dark")) { Color y = getPrimaryColor(x.substring(4)); if (y != null) return y.darker(); return defaultColor; } else if (x.substring(0, 5).equalsIgnoreCase("light")) { Color y = getPrimaryColor(x.substring(5)); if (y != null) { return y.brighter(); } } else { Color y = getPrimaryColor(x); if (y != null) return y; return defaultColor; } } return defaultColor; } public void init() { bgColor = getColor("bgcolor", Color.lightGray); String x = getParameter("datachannel"); if (x == null) { System.out.println("Datachannel required."); } dc = DataChannel.getChannel(x); } Font myFont = new Font("helvetica", Font.BOLD, 14); /** * This is a raised rectangle drawing method that works the way I want * it to work. */ protected void rect3D(Graphics g, int x, int y, int w, int h, int thick, Color dkColor, Color brColor) { h--; w--; g.setColor(dkColor); 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(brColor); 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); } } char pw[] = new char[8]; int numChars = 0; /* font parameters */ int starWidth = -1; int starAscent = -1; int starHeight = -1; public void paint(Graphics g) { Color lt = bgColor.brighter(); Color dk = bgColor.darker(); int w = size().width; int h = size().height; int btall, bwidth; int dx, dy; g.setFont(myFont); if (starWidth == -1) { FontMetrics fm = g.getFontMetrics(); starAscent = fm.getMaxAscent(); starWidth = fm.stringWidth("*"); starHeight = fm.getHeight(); } bwidth = starWidth * 8; btall = starAscent; dx = (w - bwidth) / 2; dy = (h - btall) / 2; // width was set way too small if ((dx <= 0) || (dy <= 0)) { } g.setColor(bgColor); g.fillRect(0, 0, w, h); rect3D(g, dx-2, dy-2, bwidth+4, btall+4, 2, lt, dk); rect3D(g, dx - 4, dy - 4, bwidth+8, btall+8, 2, dk, lt); g.setColor(Color.white); g.fillRect(dx, dy, bwidth - 1, btall - 1); g.setColor(Color.black); for (int i = 0; i < numChars; i++) { g.drawString("*", dx+i*starWidth, dy+starAscent); } } public boolean keyDown(Event ev, int key) { if (key == 21) { numChars = 0; repaint(); return true; } else if (((key & 0x7f) == 0x7f) || (key == 8)) { if (numChars > 0) numChars = numChars - 1; repaint(); return true; } else if ((key & 0x7f) == '\n') { StringBuffer x = new StringBuffer(8); for (int i = 0; i < numChars; i++) { x.append(pw[i]); } dc.putValue(x.toString()); } else { int z = key & 0x7f; if (((z >= 'A') && (z <= 'Z')) || ((z >= 'a') && (z <= 'z')) || ((z >= '0') && (z <= '9'))) { if (numChars < 8) { pw[numChars] = (char) key; numChars++; repaint(); } } } return true; } }