/* * BorderedComponent.java * * Copyright (C) 1996 Shaun Terry. All Rights Reserved. */ package spt.gui; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Panel; import java.awt.Component; import java.awt.Rectangle; import spt.gui.Rectangle3D; /** * Draws a border arround any Component. Six styles are supported: * inset, raised, border etched-in, border raised, 2D and no border. * * @author Shaun Terry */ public class BorderedComponent extends Panel { /* Border Styles */ public final static int NONE = 0; public final static int IN = 1; public final static int OUT = 2; public final static int BORDER_IN = 3; public final static int BORDER_OUT = 4; public final static int BORDER_2D = 5; private int borderW = 1; private int padding = 0; private Color borderColor = Color.black; private Color padColor; private boolean f3D = false; private int type3D = IN; private Component co; private Dimension co_b; private boolean fAdjustable = true; /** Create an object to border a Component. */ public BorderedComponent(Component c) { this(c, 1); } /** Create an object to border a Component and make it the * given width (applies to 2D borders only). */ public BorderedComponent(Component c, int border) { co = c; borderW = border; co_b = co.size(); if (c.size().width > 0 && c.size().height > 0) resizeToChild(); else { super.reshape(0, 0, 10, 10); // Give ourself a temp shape // due to a bug in the Win32 // toolkit behavior co.resize(0, 0); co_b = co.size(); } add(co); setLayout(null); } /** Set the style of the border. One of: * NONE, IN, OUT, BORDER_IN, BORDER_OUT or BORDER_2D. */ public void setBorderStyle(int m) { if (m == NONE) { f3D = false; borderW = 0; } else if (m == BORDER_2D) f3D = false; else if (m == IN || m == OUT || m == BORDER_IN || m == BORDER_OUT) { f3D = true; type3D = m; borderW = 2; } else throw new IllegalArgumentException(); resizeToChild(); } /** The space, in pixels, between the border and Component (the * default is 0). */ public void setPadding(int p) { if (p >= 0) padding = p; else throw new IllegalArgumentException(); resizeToChild(); } /** The color to paint the padding are between the border and * Component (the default is the background color). */ public void setPaddingColor(Color c) { padColor = c; } /** The width of the border (applies to 2D borders only). */ public void setBorderWidth(int p) { if (p >= 0) borderW = p; else throw new IllegalArgumentException(); resizeToChild(); } /** The color of the border (applies to 2D borders only). */ public void setBorderColor(Color c) { borderColor = c; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (((co_b.width != co.size().width || co_b.height != co.size().height) && fAdjustable) || (co_b.width <= 0 || co_b.height <= 0)) { resize(co.size().width+(2*borderW)+(2*padding), co.size().height+(2*borderW)+(2*padding)); } Rectangle r = bounds(); if (padColor != null && padding > 0) { g.setColor(padColor); g.fillRect(borderW, borderW, r.width-(2*borderW)-1, r.height-(2*borderW)-1); } g.setColor(borderColor); if (!f3D) { for (int i=0; i < borderW; ++i) g.drawRect(i, i, r.width-(2*i)-1, r.height-(2*i)-1); } else { int m; if (type3D == IN) m = Rectangle3D.IN; else if (type3D == OUT) m = Rectangle3D.OUT; else if (type3D == BORDER_IN) m = Rectangle3D.BORDER_IN; else /*if (type3D == BORDER_OUT)*/ m = Rectangle3D.BORDER_OUT; Rectangle3D d = new Rectangle3D(this, 0, 0, r.width-1, r.height-1); d.setDrawingMode(m); d.paint(g); } co.move(borderW+padding, borderW+padding); } public int getBorderWidth() { return borderW; } /** Resize the parent border to the current size of it's child * plus the required border width and padding area. */ public void resizeToChild() { if (co.size().width > 0 && co.size().height > 0) resize(co.size().width + (2 * borderW) + (2 * padding), co.size().height + (2 * borderW) + (2 * padding)); } public synchronized void reshape(int x, int y, int w, int h) { super.reshape(x, y, w, h); if (co != null) { co.reshape(borderW+padding, borderW+padding, w-(2*borderW)-(2*padding), h-(2*borderW)-(2*padding)); co_b = co.size(); } } }