MatteBorderpublic class MatteBorder extends EmptyBorder A class which provides a matte-like border of either a solid color
or a tiled icon.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see {@link java.beans.XMLEncoder}. |
Fields Summary |
---|
protected Color | color | protected Icon | tileIcon |
Constructors Summary |
---|
public MatteBorder(int top, int left, int bottom, int right, Color matteColor)Creates a matte border with the specified insets and color.
super(top, left, bottom, right);
this.color = matteColor;
| public MatteBorder(Insets borderInsets, Color matteColor)Creates a matte border with the specified insets and color.
super(borderInsets);
this.color = matteColor;
| public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon)Creates a matte border with the specified insets and tile icon.
super(top, left, bottom, right);
this.tileIcon = tileIcon;
| public MatteBorder(Insets borderInsets, Icon tileIcon)Creates a matte border with the specified insets and tile icon.
super(borderInsets);
this.tileIcon = tileIcon;
| public MatteBorder(Icon tileIcon)Creates a matte border with the specified tile icon. The
insets will be calculated dynamically based on the size of
the tile icon, where the top and bottom will be equal to the
tile icon's height, and the left and right will be equal to
the tile icon's width.
this(-1,-1,-1,-1, tileIcon);
|
Methods Summary |
---|
private java.awt.Insets | computeInsets(java.awt.Insets insets)
if (tileIcon != null && top == -1 && bottom == -1 &&
left == -1 && right == -1) {
int w = tileIcon.getIconWidth();
int h = tileIcon.getIconHeight();
insets.top = h;
insets.right = w;
insets.bottom = h;
insets.left = w;
} else {
insets.left = left;
insets.top = top;
insets.right = right;
insets.bottom = bottom;
}
return insets;
| public java.awt.Insets | getBorderInsets(java.awt.Component c)Returns the insets of the border.
return getBorderInsets();
| public java.awt.Insets | getBorderInsets(java.awt.Component c, java.awt.Insets insets)Reinitialize the insets parameter with this Border's current Insets.
return computeInsets(insets);
| public java.awt.Insets | getBorderInsets()Returns the insets of the border.
return computeInsets(new Insets(0,0,0,0));
| public java.awt.Color | getMatteColor()Returns the color used for tiling the border or null
if a tile icon is being used.
return color;
| public javax.swing.Icon | getTileIcon()Returns the icon used for tiling the border or null
if a solid color is being used.
return tileIcon;
| public boolean | isBorderOpaque()Returns whether or not the border is opaque.
// If a tileIcon is set, then it may contain transparent bits
return color != null;
| public void | paintBorder(java.awt.Component c, java.awt.Graphics g, int x, int y, int width, int height)Paints the matte border.
Insets insets = getBorderInsets(c);
Color oldColor = g.getColor();
g.translate(x, y);
// If the tileIcon failed loading, paint as gray.
if (tileIcon != null) {
color = (tileIcon.getIconWidth() == -1) ? Color.gray : null;
}
if (color != null) {
g.setColor(color);
g.fillRect(0, 0, width - insets.right, insets.top);
g.fillRect(0, insets.top, insets.left, height - insets.top);
g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
} else if (tileIcon != null) {
int tileW = tileIcon.getIconWidth();
int tileH = tileIcon.getIconHeight();
int xpos, ypos, startx, starty;
Graphics cg;
// Paint top matte edge
cg = g.create();
cg.setClip(0, 0, width, insets.top);
for (ypos = 0; insets.top - ypos > 0; ypos += tileH) {
for (xpos = 0; width - xpos > 0; xpos += tileW) {
tileIcon.paintIcon(c, cg, xpos, ypos);
}
}
cg.dispose();
// Paint left matte edge
cg = g.create();
cg.setClip(0, insets.top, insets.left, height - insets.top);
starty = insets.top - (insets.top%tileH);
startx = 0;
for (ypos = starty; height - ypos > 0; ypos += tileH) {
for (xpos = startx; insets.left - xpos > 0; xpos += tileW) {
tileIcon.paintIcon(c, cg, xpos, ypos);
}
}
cg.dispose();
// Paint bottom matte edge
cg = g.create();
cg.setClip(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
starty = (height - insets.bottom) - ((height - insets.bottom)%tileH);
startx = insets.left - (insets.left%tileW);
for (ypos = starty; height - ypos > 0; ypos += tileH) {
for (xpos = startx; width - xpos > 0; xpos += tileW) {
tileIcon.paintIcon(c, cg, xpos, ypos);
}
}
cg.dispose();
// Paint right matte edge
cg = g.create();
cg.setClip(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
starty = insets.top - (insets.top%tileH);
startx = width - insets.right - ((width - insets.right)%tileW);
for (ypos = starty; height - ypos > 0; ypos += tileH) {
for (xpos = startx; width - xpos > 0; xpos += tileW) {
tileIcon.paintIcon(c, cg, xpos, ypos);
}
}
cg.dispose();
}
g.translate(-x, -y);
g.setColor(oldColor);
|
|