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
|
/*
* SidedEtchedBorder.java
*
* Created on 18-Mar-2010
*/
package my.util.ui.border;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
/**
* This class creates an etched border that can be one,
* two, three or four sided.
* <p>
* As his parent <code>javax.swing.border.EtchedBorder</code> {@link javax.swing.border.EtchedBorder}:
* <ul>
* <li> The border can be etched-in or etched-out.
* <li> If colors highlight/shadow aren't initialized, they will be
* derived from the component for which the border is being painted.
* </ul>
*
* @author Julie Duvillier
*/
public class SidedEtchedBorder
extends EtchedBorder{
/* Booleans holding which side of the border will be paint. */
private boolean top, left,bottom,right;
/**
* Creates four sided a lowered etched border with default colors.
*/
public SidedEtchedBorder(){
this(LOWERED,true, true, true, true);
}
/**
* Creates a sided lowered etched border with default colors.
* @param top if <code>true</code> the top side will be painted
* @param left if <code>true</code> the left side will be painted
* @param bottom if <code>true</code> the bottom side will be painted
* @param right if <code>true</code> the right side will be painted
*/
public SidedEtchedBorder(boolean top, boolean left,boolean bottom, boolean right){
this(LOWERED,null,null, top, left,bottom, right);
}
/**
* Created a sided etched border with the specifed etch-type
* and default colors.
* @param etchType the type of etch to be paint
* @param top if <code>true</code> the top side will be painted
* @param left if <code>true</code> the left side will be painted
* @param bottom if <code>true</code> the bottom side will be painted
* @param right if <code>true</code> the right side will be painted
*/
public SidedEtchedBorder(int etchType,
boolean top, boolean left,boolean bottom, boolean right){
this(etchType,null,null, top, left,bottom, right);
}
/**
* Creates a sided lowered etched border with the specified colors.
* @param highlight the color to use for highlight
* @param shadow the color to use for shadow
* @param top if <code>true</code> the top side will be painted
* @param left if <code>true</code> the left side will be painted
* @param bottom if <code>true</code> the bottom side will be painted
* @param right if <code>true</code> the right side will be painted
*/
public SidedEtchedBorder(Color highlight, Color shadow,
boolean top, boolean left,boolean bottom, boolean right){
}
/**
* Creates a sided etched border with the specified etch-type.
* and colors.
* @param etchType the type of etch to be paint
* @param highlight the color to use for highlight
* @param shadow the color to use for shadow
* @param top if <code>true</code> the top side will be painted
* @param left if <code>true</code> the left side will be painted
* @param bottom if <code>true</code> the bottom side will be painted
* @param right if <code>true</code> the right side will be painted
*/
public SidedEtchedBorder(int etchType,
Color highlight, Color shadow,
boolean top, boolean left,boolean bottom, boolean right){
super(etchType, highlight, shadow);
this.top = top;
this.right = right;
this.bottom = bottom;
this.left = left;
}
/**
* Creates a one sided lowered etched border with default colors.
* @param side the side to be draw
*/
public SidedEtchedBorder( int side){
this(side,LOWERED,null,null);
}
/**
* Creates a one sided lowered etched border with the specified etch-type
* and default colors.
* @param side the side to be draw
* @param etchType the type of etch to be paint
*/
public SidedEtchedBorder(int side,int etchType ){
this(side,LOWERED,null,null);
}
/**
* Creates a one sided lowered etched border with specifed colors.
* @param side the side to be draw
* @param highlight the color to use for highlight
* @param shadow the color to use for shadow
*/
public SidedEtchedBorder(int side,Color highlight, Color shadow){
this(side,LOWERED,highlight,shadow);
}
/**
* Creates a one sided etched border with the specified etch-type
* and colors.
* @param side the side to be draw
* @param etchType the type of etch to be paint
* @param highlight the color to use for highlight
* @param shadow the color to use for shadow
*/
public SidedEtchedBorder( int side, int etchType, Color highlight, Color shadow){
this(etchType,highlight,shadow, false,false,false,false);
switch(side){
case SwingConstants.TOP: top =true; break;
case SwingConstants.LEFT: left =true; break;
case SwingConstants.BOTTOM: bottom =true; break;
case SwingConstants.RIGHT: right =true; break;
}
}
/**
* Paints the border for the specified component
* with the specified position and size.
*/
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.translate(x, y);
int w = width ;
int h= height;
Color col1 = (etchType == LOWERED)? getShadowColor(c)
: getHighlightColor(c);
Color col2 = (etchType == LOWERED)? getHighlightColor(c)
: getShadowColor(c);
if( top ){
drawLine(g,col1,0, 0, w-2, 0);
drawLine(g,col2,1, 1, w-3, 1);
}
if( left ){
drawLine(g,col1,0, 0, 0, h-2);
drawLine(g,col2,1, h-3, 1, 1);
}
if( bottom){
drawLine(g,col1,0, h-2, w-2, h-2);
drawLine(g,col2,0, h-1, w-1, h-1);
}
if( right){
drawLine(g,col1, w-2, 0 , w-2, h-2);
drawLine(g,col2,w-1, h-1, w-1, 0);
}
g.translate(-x, -y);
}
/**
* Draw a line with the specifed graphics, color, between the points
* of coordinates A1(x1,y1) and A2(x2,y2).
* @param g the graphics objects to use
* @param color the color of the line to be draw
* @param x1 abscissa of A1
* @param y1 ordinate of A1
* @param x2 abscissa of A2
* @param y2 ordinate of A2
*/
private void drawLine(Graphics g,Color color, int x1, int y1, int x2, int y2 ){
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
/**
* Gets the insets of the border.
* @param c the component for which the border is painted
* @return the <code>Insets</code> object of the border
*/
@Override
public Insets getBorderInsets(Component c) {
return getBorderInsets(c, new Insets(0, 0, 0, 0));
}
/**
* Gets the insets of the border after reinitializing it
* with border insets.
* @param c the component for which the border is painted
* @param insets the <code>Insets</code> object to be reinitialized
* @return the <code>Insets</code> object of the border
*/
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.top = insets.right = insets.bottom = 0;
if( top ){
insets.top = 2;
}
if(left) {
insets.left =2;
}
if(bottom){
insets.bottom = 2;
}
if(right){
insets.right =2;
}
return insets;
}
} |