package ca.spaz.gui; import java.awt.*; import javax.swing.border.*; /** * PoliceTape 1.0 * * Draws police tape around a component. * * Created on March 24, 2007 * * Source code gifted to public domain. Enjoy. * * @author Aaron Davidson <aaron@spaz.ca> */ public class PoliceTape extends AbstractBorder { private int thickness = 15; private int stripeLength = 50; public PoliceTape() { } public PoliceTape(int thickness) { setThickness(thickness); } public PoliceTape(int thickness, int stripeLength) { setThickness(thickness); setStripeLength(stripeLength); } /** * Returns the length of each stripe */ public int getStripeLength() { return stripeLength; } /** * Returns the thickness of the border. */ public int getThickness() { return thickness; } /** * Paints the border for the specified component with the specified position * and size. * * @param c * the component for which this border is being painted * @param g * the paint graphics * @param x * the x position of the painted border * @param y * the y position of the painted border * @param width * the width of the painted border * @param height * the height of the painted border */ public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { Image img = createPoliceTape(width, height, stripeLength); // draw top tape g.drawImage(img, x, y, x+width, y+thickness, 0, 0, width, thickness, null); // draw bottom tape g.drawImage(img, x, y+height-thickness, x+width, y+height, 0, height-thickness, width, height, null); // draw left tape g.drawImage(img, x, y+thickness, x+thickness, y+height-thickness, 0, thickness, thickness, height-thickness, null); // draw right tape g.drawImage(img, x+width-thickness, y+thickness, x+width, y+height-thickness, width-thickness, thickness, width, height-thickness, null); } /** * Return an image filled with police tape. * * @param width the width of the image * @param height the height of the image * @param stripe the width of each bumblebee stripe * @return a police tape image */ public static Image createPoliceTape(int width, int height, int stripe) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); Image img = gc.createCompatibleImage(width, height, Transparency.OPAQUE); Graphics g = img.getGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, width, height); g.setColor(Color.YELLOW); for (int i = 0; i < height; i++) { for (int j = -height / stripe; j <= width / stripe; j += 2) { g.drawLine(Math.max(0, j * stripe + i), i, Math.min(width, (j + 1) * stripe + i), i); } } return img; } /** * Sets the length of each stripe */ public void setStripeLength(int stripeLength) { this.stripeLength = stripeLength; } /** * Sets the thickness of the border. */ public void setThickness(int thickness) { this.thickness = thickness; } public Insets getBorderInsets(Component c) { return new Insets(thickness, thickness, thickness, thickness); } public Insets getBorderInsets(Component c, Insets insets) { insets.left = insets.top = insets.right = insets.bottom = thickness; return insets; } }
|