package causation.lab.plaf.basic; // packages import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.net.URL; import java.net.MalformedURLException; // custom classes import causation.lab.plaf.RandomizerUI; import causation.lab.Randomizer; /** *

* The basic L&F for Randomizers. * Copyright 1999 by David Danks. All rights reserved. *

* * @see Randomizer * @version 0.9 June 23, 1999 * @author David Danks */ public class BasicRandomizerUI extends RandomizerUI { /////////////////// Static Class Variables /////////////////// private static final Color DEFAULT = Color.black; private static final Color SELECTED = new Color(204, 0, 0); private static final Color DISABLED = new Color(204, 204, 204); private static final String ICON_DIR = "icons/"; private static final String RANDOMIZER_ICON = "randimage.gif"; private static final String GRAY_RANDOMIZER_ICON = "randimagegray.gif"; private static final String RED_RANDOMIZER_ICON = "randimagered.gif"; private static final int DEFAULT_SIZE = 16; /////////////////// Instance Variables /////////////////// private URL codebase = null; private ImageIcon icon = null; private ImageIcon dis_icon = null; private ImageIcon sel_icon = null; /////////////////// Constructor /////////////////// /** * The basic constructor */ public BasicRandomizerUI() { ; } /////////////////// Class Methods /////////////////// /** * @return The BasicRandomizerUI for this object */ public static ComponentUI createUI(JComponent c) { return new BasicRandomizerUI(); } /////////////////// Instance Methods /////////////////// /** * Currently does not do anything */ public void installUI(JComponent c) { ; } /** * Currently does not do anything */ public void uninstallUI(JComponent c) { ; } /** * @return The disabled image of the lock, if there is one */ public Icon getDisabledRandomizerIcon() { return dis_icon; } /** * @return The codebase URL for loading images */ public URL getCodebase() { return codebase; } /** * @param newBase The new codebase from which to load images */ public void setCodebase(URL newBase) { if (newBase == codebase) return; codebase = newBase; if (codebase == null) return; // otherwise, load the new Icons try { icon = new ImageIcon(new URL(codebase+ICON_DIR+RANDOMIZER_ICON)); dis_icon = new ImageIcon(new URL(codebase+ICON_DIR+GRAY_RANDOMIZER_ICON)); sel_icon = new ImageIcon(new URL(codebase+ICON_DIR+RED_RANDOMIZER_ICON)); } catch (MalformedURLException m) { System.out.println("Bad randomizer icon URL: "+m.toString()); return; } // check to ensure that the Icons loaded if (icon.getIconWidth() < 0) icon = null; if (sel_icon.getIconWidth() < 0) sel_icon = null; if (dis_icon.getIconWidth() < 0) dis_icon = null; } /** * @return The preferred size of the Randomizer */ public Dimension getPreferredSize(Randomizer r) { if (r.isExperimenting()) { if (r.isSelected()) { // selected, active randomizer if (sel_icon != null) return new Dimension(sel_icon.getIconWidth(), sel_icon.getIconHeight()); else return new Dimension(DEFAULT_SIZE, DEFAULT_SIZE); } else { // unselected, active randomizer if (icon != null) return new Dimension(icon.getIconWidth(), icon.getIconHeight()); else return new Dimension(DEFAULT_SIZE, DEFAULT_SIZE); } } else { // inactive (so unselected) randomizer if (dis_icon != null) return new Dimension(dis_icon.getIconWidth(), dis_icon.getIconHeight()); else return new Dimension(DEFAULT_SIZE, DEFAULT_SIZE); } } /** * @return The minimum size of the Randomizer */ public Dimension getMinimumSize(Randomizer r) { return getPreferredSize(r); } /** * Paints the Randomizer into the given Graphics context. If there is an * ImageIcon associated with the Randomizer, it paints that. Otherwise, * it paints a die showing a '3'. * @param g The Graphics context in which to paint the Randomizer * @param c The reference to the Randomizer */ public void paint(Graphics g, JComponent c) { if (!(c instanceof Randomizer)) return; Randomizer rand = (Randomizer)c; ImageIcon painticon = null; if (rand.isSelected()) painticon = sel_icon; else if (rand.isExperimenting()) painticon = icon; else painticon = dis_icon; if (painticon != null) painticon.paintIcon(c, g, 0, 0); else { int w = rand.getWidth(); int h = rand.getHeight(); // set the color if (rand.isSelected()) g.setColor(SELECTED); else if (rand.isExperimenting()) g.setColor(DEFAULT); else g.setColor(DISABLED); // draw the die g.drawRect(0, 0, w-1, h-1); g.drawLine(w/4, h/4, w/4, h/4); g.drawLine(w/2, h/2, w/2, h/2); g.drawLine(w*3/4, h*3/4, w*3/4, h*3/4); } } }