001: /*************************************************************************
002: * *
003: * 1) This source code file, in unmodified form, and compiled classes *
004: * derived from it can be used and distributed without restriction, *
005: * including for commercial use. (Attribution is not required *
006: * but is appreciated.) *
007: * *
008: * 2) Modified versions of this file can be made and distributed *
009: * provided: the modified versions are put into a Java package *
010: * different from the original package, edu.hws; modified *
011: * versions are distributed under the same terms as the original; *
012: * and the modifications are documented in comments. (Modification *
013: * here does not include simply making subclasses that belong to *
014: * a package other than edu.hws, which can be done without any *
015: * restriction.) *
016: * *
017: * David J. Eck *
018: * Department of Mathematics and Computer Science *
019: * Hobart and William Smith Colleges *
020: * Geneva, New York 14456, USA *
021: * Email: eck@hws.edu WWW: http://math.hws.edu/eck/ *
022: * *
023: *************************************************************************/package edu.hws.jcm.draw;
024:
025: import java.awt.Graphics;
026: import java.awt.Color;
027:
028: /**
029: * A DrawBorder object is just a simple border around the edges of its CoordinateRect, with
030: * a specified width, in pixels, and a specified color.
031: */
032: public class DrawBorder extends Drawable {
033:
034: /**
035: * A non-null Color, giving the color of the bortder.
036: */
037: protected Color color;
038:
039: /**
040: * A non-negative integer giving the width of the border in pixels.
041: */
042: protected int width;
043:
044: /**
045: * Create a black border that is one pixel thick.
046: */
047: public DrawBorder() {
048: this (Color.black, 1);
049: }
050:
051: /**
052: * Create a border with the spcified color and width. If the color is null,
053: * black is used. If the width is less than zero, a width of 1 is used.
054: * A border of width zero is invisible.
055: *
056: */
057: public DrawBorder(Color color, int width) {
058: this .color = ((color == null) ? Color.black : color);
059: this .width = ((width >= 0) ? width : 1);
060: }
061:
062: /**
063: * Get the color of the border.
064: *
065: */
066: public Color getColor() {
067: return color;
068: }
069:
070: /**
071: * Set the color of the border to the specified color. If the color is null, nothing is done.
072: *
073: */
074: public void setColor(Color c) {
075: if (c != null && !c.equals(color)) {
076: color = c;
077: needsRedraw();
078: }
079: }
080:
081: /**
082: * Get the width of the border, in pixels.
083: *
084: */
085: public int getWidth() {
086: return width;
087: }
088:
089: /**
090: * Set the width of the border to be w pixels. If w is negative,
091: * this is ignored. A border of witdth 0 is invisible.
092: *
093: * @param w the desired width for the border.
094: */
095: public void setWidth(int w) {
096: if (w >= 0 && width != width) {
097: width = w;
098: }
099: }
100:
101: /**
102: * Draw the border in the given graphics context. This is not ordinarily called directly.
103: *
104: */
105: public void draw(Graphics g, boolean changed) {
106: if (coords == null || width == 0)
107: return;
108: g.setColor(color);
109: for (int i = 0; i < width; i++)
110: g.drawRect(coords.getLeft() + i, coords.getTop() + i,
111: coords.getWidth() - 2 * i - 1, coords.getHeight()
112: - 2 * i - 1);
113: }
114:
115: } // end class DrawBorder
|