BorderPanel.java
7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
** AWT Widget Package.
** Copyright (c) 1997 by Timothy Gerard Endres
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package com.sitech.ismp.ice.widget;
import java.awt.*;
public class
BorderPanel extends Panel
{
static public final int NONE = 0;
static public final int FLAT = 1;
static public final int RIDGE = 2;
static public final int GROOVE = 3;
static public final int RAISED = 4;
static public final int SUNKEN = 5;
protected int insetWidth;
protected int marginWidth;
protected int borderWidth;
protected int borderStyle;
protected double darkFactor;
protected double liteFactor;
public
BorderPanel( int inset, int border, int margin, int style )
{
super();
// Sanity
switch ( this.borderStyle )
{
case BorderPanel.RIDGE:
case BorderPanel.GROOVE:
case BorderPanel.RAISED:
case BorderPanel.SUNKEN:
if ( (border & 1) != 0 ) border++;
if ( border < 2 ) border = 2;
break;
case BorderPanel.FLAT:
break;
case BorderPanel.NONE:
break;
}
if ( border >= inset )
{
margin = 0;
border = inset;
}
else if ( border + margin >= inset )
{
margin = inset - border;
}
this.insetWidth = inset;
this.marginWidth = margin;
this.borderWidth = border;
this.borderStyle = style;
this.darkFactor = 0.70; // larger multiplier makes lighter
this.liteFactor = 0.82; // larger multiplier makes lighter
}
public Insets
getInsets()
{
Insets peer = super.getInsets();
Insets result =
new Insets(
peer.top + this.insetWidth,
peer.left + this.insetWidth,
peer.bottom + this.insetWidth,
peer.right + this.insetWidth );
return result;
}
public double
getDarkFactor()
{
return this.darkFactor;
}
public void
setDarkFactor( double factor )
{
this.darkFactor = factor; // larger multiplier makes lighter
}
public double
getLiteFactor()
{
return this.liteFactor;
}
public void
setLiteFactor( double factor )
{
this.liteFactor = factor; // larger multiplier makes lighter
}
public void
update( Graphics updateG )
{
this.paint( updateG );
}
public void
paint( Graphics g )
{
super.paint( g );
Dimension sz = this.getSize();
Insets insets = super.getInsets();
// The origin and dimensions of our frame's area
// (which is inside the panel's insets.
int xorigin = insets.left;
int yorigin = insets.top;
int width = sz.width - (insets.left + insets.right);
int height = sz.height - (insets.top + insets.bottom);
g.setClip( xorigin, yorigin, width, height );
switch ( this.borderStyle )
{
case BorderPanel.RIDGE:
this.drawRidgeBorder
( g, xorigin, yorigin, width, height );
break;
case BorderPanel.GROOVE:
this.drawGrooveBorder
( g, xorigin, yorigin, width, height );
break;
case BorderPanel.RAISED:
this.drawRaisedBorder
( g, xorigin, yorigin, width, height );
break;
case BorderPanel.SUNKEN:
this.drawSunkenBorder
( g, xorigin, yorigin, width, height );
break;
case BorderPanel.FLAT:
this.drawFlatBorder
( g, xorigin, yorigin, width, height );
break;
}
}
protected void
drawRidgeBorder( Graphics g, int xorigin, int yorigin, int width, int height )
{
Color bgColor = this.getBackground();
Color liteColor = this.lighten( bgColor );
Color darkColor = this.darken( bgColor );
int inside =
this.insetWidth -
( this.borderWidth + this.marginWidth );
int bX = xorigin + inside;
int bY = yorigin + inside;
int bW = width - (2 * inside);
int bH = height - (2 * inside);
if ( this.borderWidth == 2 )
{
g.setColor( liteColor );
g.drawLine( bX, bY, (bX + (bW - 1)), bY ); // top
g.drawLine( bX, bY, bX, (bY + (bH - 1)) ); // left
g.drawLine( (bX + (bW - 2)), (bY + 1),
(bX + (bW - 2)), (bY + (bH - 2)) ); // right
g.drawLine( (bX + 1), (bY + (bH - 2)),
(bX + (bW - 2)), (bY + (bH - 2)) ); // bottom
g.setColor( darkColor );
g.drawLine( (bX + 1), (bY + 1),
(bX + (bW - 2)), (bY + 1) ); // top
g.drawLine( (bX + 1), (bY + 1),
(bX + 1), (bY + (bH - 2)) ); // left
g.drawLine( (bX + (bW - 1)), (bY + 1),
(bX + (bW - 1)), (bY + (bH - 2)) ); // right
g.drawLine( (bX + 1), (bY + (bH - 1)),
(bX + (bW - 2)), (bY + (bH - 1)) ); // bottom
}
else
{
}
}
protected void
drawGrooveBorder( Graphics g, int xorigin, int yorigin, int width, int height )
{
}
protected void
drawRaisedBorder( Graphics g, int xorigin, int yorigin, int width, int height )
{
}
protected void
drawSunkenBorder( Graphics g, int xorigin, int yorigin, int width, int height )
{
}
protected void
drawFlatBorder( Graphics g, int xorigin, int yorigin, int width, int height )
{
Color saveColor = g.getColor();
g.setColor( Color.black );
if ( this.borderWidth == 1 )
{
g.drawRect( 0, 0, width - 1, height - 1 );
}
else
{
g.fillRect( 0, 0, width - 1, this.borderWidth - 1 ); // top
g.fillRect( width - (this.borderWidth + 1), 0,
this.borderWidth - 1, height - 1 ); // right
g.fillRect( 0, (height - this.borderWidth),
width - 1, this.borderWidth - 1 ); // bottom
g.fillRect( 0, 0, this.borderWidth - 1, height - 1 ); // left
}
}
protected Color
darken( Color color )
{
int newval;
double minFactor, factor;
minFactor = 1.0;
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
factor = ((double)red / 255.0);
if ( factor < minFactor ) minFactor = factor;
factor = ((double)green / 255.0);
if ( factor < minFactor ) minFactor = factor;
factor = ((double)blue / 255.0);
if ( factor < minFactor ) minFactor = factor;
factor = minFactor * this.darkFactor;
newval = (int)((double)red * factor);
red = Math.max( newval, 0 );
newval = (int)((double)green * factor);
green = Math.max( newval, 0 );
newval = (int)((double)blue * factor);
blue = Math.max( newval, 0 );
return new Color( red, green, blue );
}
protected Color
lighten( Color color )
{
int newval;
double maxFactor, factor;
maxFactor = 0.0;
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
factor = ((double)red / 255.0);
if ( factor > maxFactor ) maxFactor = factor;
factor = ((double)green / 255.0);
if ( factor > maxFactor ) maxFactor = factor;
factor = ((double)blue / 255.0);
if ( factor > maxFactor ) maxFactor = factor;
if ( maxFactor > this.liteFactor )
maxFactor = this.liteFactor;
factor = (1.0 / maxFactor);
newval = (int)((double)red * factor);
red = Math.min( newval, 255 );
newval = (int)((double)green * factor);
green = Math.min( newval, 255 );
newval = (int)((double)blue * factor);
blue = Math.min( newval, 255 );
return new Color( red, green, blue );
}
}