001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: RoundedCornerBorder.java,v 1.9 2005/02/16 11:28:13 jesper Exp $
023: package net.infonode.gui.shaped.border;
024:
025: import net.infonode.gui.colorprovider.ColorProvider;
026: import net.infonode.gui.colorprovider.FixedColorProvider;
027:
028: import java.awt.*;
029:
030: /**
031: * @author johan
032: */
033: public class RoundedCornerBorder extends PolygonBorder {
034: private static final long serialVersionUID = 1;
035:
036: private static int[][] corner1 = { { 0, 0 }, { 0, 1, 1, 0 },
037: { 0, 2, 2, 0 }, { 0, 4, 1, 3, 1, 2, 2, 1, 3, 1, 4, 0 },
038: { 0, 5, 1, 4, 1, 3, 3, 1, 4, 1, 5, 0 } };
039:
040: private static int[][] corner2 = { { -1, 0 }, { -2, 0, -1, 1 },
041: { -3, 0, -1, 2 },
042: { -5, 0, -4, 1, -3, 1, -2, 2, -2, 3, -1, 4 },
043: { -6, 0, -5, 1, -4, 1, -2, 3, -2, 4, -1, 5 } };
044:
045: private static int[][] corner3 = { { -1, -1 }, { -1, -2, -2, -1 },
046: { -1, -3, -3, -1 },
047: { -1, -5, -2, -4, -2, -3, -3, -2, -4, -2, -5, -1 },
048: { -1, -6, -2, -5, -2, -4, -4, -2, -5, -2, -6, -1 } };
049:
050: private static int[][] corner4 = { { 0, -1 }, { 1, -1, 0, -2 },
051: { 2, -1, 0, -3 },
052: { 4, -1, 3, -2, 2, -2, 1, -3, 1, -4, 0, -5 },
053: { 5, -1, 4, -2, 3, -2, 1, -4, 1, -5, 0, -6 } };
054:
055: private Insets insets;
056:
057: public RoundedCornerBorder(ColorProvider lineColor, int cType) {
058: this (lineColor, cType, true, true, true, true);
059: }
060:
061: public RoundedCornerBorder(ColorProvider lineColor, int cType,
062: boolean drawTop, boolean drawLeft, boolean drawBottom,
063: boolean drawRight) {
064: this (lineColor, FixedColorProvider.WHITE, cType, cType, cType,
065: cType, drawTop, drawLeft, drawBottom, drawRight);
066: }
067:
068: public RoundedCornerBorder(ColorProvider lineColor,
069: ColorProvider highlightColor, int cType1, int cType2,
070: int cType3, int cType4) {
071: this (lineColor, highlightColor, cType1, cType2, cType3, cType4,
072: true, true, true, true);
073: }
074:
075: public RoundedCornerBorder(ColorProvider lineColor,
076: ColorProvider highlightColor, int cType1, int cType2,
077: int cType3, int cType4, boolean drawTop, boolean drawLeft,
078: boolean drawBottom, boolean drawRight) {
079: super (lineColor, highlightColor, createCoordinates(cType1,
080: cType2, cType3, cType4, drawTop, drawLeft, drawBottom,
081: drawRight), createXScales(cType1, cType2, cType3,
082: cType4), createYScales(cType1, cType2, cType3, cType4));
083:
084: insets = new Insets(drawTop ? 1 : 0, drawLeft ? 1 : 0,
085: drawBottom ? 1 : 0, drawRight ? 1 : 0);
086: }
087:
088: protected Insets getShapedBorderInsets(Component c) {
089: return insets;
090: }
091:
092: private static int[] createCoordinates(int cType1, int cType2,
093: int cType3, int cType4, boolean drawTop, boolean drawLeft,
094: boolean drawBottom, boolean drawRight) {
095: int c1[] = corner1[cType1];
096: int c2[] = corner2[cType2];
097: int c3[] = corner3[cType3];
098: int c4[] = corner4[cType4];
099:
100: int coords[] = new int[c1.length + c2.length + c3.length
101: + c4.length];
102: int index = 0;
103:
104: for (int i = 0; i < c1.length; i++) {
105: coords[index] = drawLeft ? c1[i] : c1[i] - 1;
106: index++;
107: i++;
108: coords[index] = drawTop ? c1[i] : c1[i] - 1;
109: index++;
110: }
111:
112: for (int i = 0; i < c2.length; i++) {
113: coords[index] = drawRight ? c2[i] : c2[i] + 1;
114: index++;
115: i++;
116: coords[index] = drawTop ? c2[i] : c2[i] - 1;
117: index++;
118: }
119:
120: for (int i = 0; i < c3.length; i++) {
121: coords[index] = drawRight ? c3[i] : c3[i] + 1;
122: index++;
123: i++;
124: coords[index] = drawBottom ? c3[i] : c3[i] + 1;
125: index++;
126: }
127:
128: for (int i = 0; i < c4.length; i++) {
129: coords[index] = drawLeft ? c4[i] : c4[i] - 1;
130: index++;
131: i++;
132: coords[index] = drawBottom ? c4[i] : c4[i] + 1;
133: index++;
134: }
135:
136: return coords;
137: }
138:
139: private static float[] createXScales(int cType1, int cType2,
140: int cType3, int cType4) {
141: int c[][] = new int[4][1];
142: c[0] = corner1[cType1];
143: c[1] = corner2[cType2];
144: c[2] = corner3[cType3];
145: c[3] = corner4[cType4];
146:
147: float xscales[] = new float[c[0].length + c[1].length
148: + c[2].length + c[3].length];
149: int index = 0;
150:
151: for (int k = 0; k < c.length; k++) {
152: for (int i = 0; i < c[k].length; i++) {
153: xscales[index] = c[k][i] < 0 ? 1 : 0;
154: i++;
155: index += 2;
156: }
157:
158: }
159:
160: return xscales;
161: }
162:
163: private static float[] createYScales(int cType1, int cType2,
164: int cType3, int cType4) {
165: int c[][] = new int[4][1];
166: c[0] = corner1[cType1];
167: c[1] = corner2[cType2];
168: c[2] = corner3[cType3];
169: c[3] = corner4[cType4];
170:
171: float yscales[] = new float[c[0].length + c[1].length
172: + c[2].length + c[3].length];
173: int index = 1;
174:
175: for (int k = 0; k < c.length; k++) {
176: for (int i = 1; i < c[k].length; i++) {
177: yscales[index] = c[k][i] < 0 ? 1 : 0;
178: i++;
179: index += 2;
180: }
181: }
182:
183: return yscales;
184: }
185: }
|