FileDocCategorySizeDatePackage
MatteBorder.javaAPI DocJava SE 5 API8173Fri Aug 26 14:58:00 BST 2005javax.swing.border

MatteBorder

public 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}.

version
1.22 12/19/03
author
Amy Fowler

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.

param
top the top inset of the border
param
left the left inset of the border
param
bottom the bottom inset of the border
param
right the right inset of the border
param
matteColor the color rendered for the border

        super(top, left, bottom, right);
        this.color = matteColor;
    
public MatteBorder(Insets borderInsets, Color matteColor)
Creates a matte border with the specified insets and color.

param
borderInsets the insets of the border
param
matteColor the color rendered for the border

        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.

param
top the top inset of the border
param
left the left inset of the border
param
bottom the bottom inset of the border
param
right the right inset of the border
param
tileIcon the icon to be used for tiling the border

        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.

param
borderInsets the insets of the border
param
tileIcon the icon to be used for tiling the border

        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.

param
tileIcon the icon to be used for tiling the border

        this(-1,-1,-1,-1, tileIcon);
    
Methods Summary
private java.awt.InsetscomputeInsets(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.InsetsgetBorderInsets(java.awt.Component c)
Returns the insets of the border.

param
c the component for which this border insets value applies

        return getBorderInsets();
    
public java.awt.InsetsgetBorderInsets(java.awt.Component c, java.awt.Insets insets)
Reinitialize the insets parameter with this Border's current Insets.

param
c the component for which this border insets value applies
param
insets the object to be reinitialized

        return computeInsets(insets);
    
public java.awt.InsetsgetBorderInsets()
Returns the insets of the border.

        return computeInsets(new Insets(0,0,0,0));
    
public java.awt.ColorgetMatteColor()
Returns the color used for tiling the border or null if a tile icon is being used.

        return color;
    
public javax.swing.IcongetTileIcon()
Returns the icon used for tiling the border or null if a solid color is being used.

        return tileIcon;
    
public booleanisBorderOpaque()
Returns whether or not the border is opaque.

 
        // If a tileIcon is set, then it may contain transparent bits
        return color != null; 
    
public voidpaintBorder(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);