001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.border;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Graphics;
025: import java.awt.Insets;
026: import java.awt.Shape;
027:
028: import javax.swing.Icon;
029:
030: public class MatteBorder extends EmptyBorder {
031:
032: protected Color color;
033: protected Icon tileIcon;
034:
035: public MatteBorder(final Insets insets, final Icon tileIcon) {
036: super (insets);
037:
038: this .tileIcon = tileIcon;
039: }
040:
041: public MatteBorder(final Insets insets, final Color color) {
042: super (insets);
043:
044: this .color = color;
045: }
046:
047: public MatteBorder(final Icon tileIcon) {
048: // null vervfication has been added according to HARMONY-2589
049: super ((tileIcon == null) ? -1 : tileIcon.getIconHeight(),
050: (tileIcon == null) ? -1 : tileIcon.getIconWidth());
051:
052: this .tileIcon = tileIcon;
053: }
054:
055: public MatteBorder(final int top, final int left, final int bottom,
056: final int right, final Icon tileIcon) {
057: super (top, left, bottom, right);
058:
059: this .tileIcon = tileIcon;
060: }
061:
062: public MatteBorder(final int top, final int left, final int bottom,
063: final int right, final Color color) {
064: super (top, left, bottom, right);
065:
066: this .color = color;
067: }
068:
069: private void fillRectWithIcon(final Component c, final Graphics g,
070: final int x, final int y, final int width, final int height) {
071: int iconWidth = tileIcon.getIconWidth();
072: int iconHeight = tileIcon.getIconHeight();
073: for (int xOffset = 0; xOffset < width; xOffset += iconWidth) {
074: for (int yOffset = 0; yOffset < height; yOffset += iconHeight) {
075: tileIcon.paintIcon(c, g, x + xOffset, y + yOffset);
076: }
077: }
078: }
079:
080: public void paintBorder(final Component c, final Graphics g,
081: final int x, final int y, final int width, final int height) {
082: if (tileIcon != null) {
083: int iconWidth = tileIcon.getIconWidth();
084: int iconHeight = tileIcon.getIconHeight();
085: int topOffset = 0;
086: int leftOffset = 0;
087: Shape oldClip = g.getClip();
088: g.setClip(x, y, width, top);
089: fillRectWithIcon(c, g, x + leftOffset, y + topOffset,
090: width, top);
091:
092: g.setClip(x, y + top, left, height - top - bottom);
093: topOffset = (int) (top / iconHeight) * iconHeight;
094: fillRectWithIcon(c, g, x + leftOffset, y + topOffset, left,
095: height);
096:
097: g.setClip(x, y + height - bottom, width, bottom);
098: topOffset = (int) ((height - bottom) / iconHeight)
099: * iconHeight;
100: fillRectWithIcon(c, g, x, y + topOffset, width, height
101: - topOffset);
102:
103: g.setClip(x + width - right, y + top, right, height - top
104: - bottom);
105: topOffset = (int) (top / iconHeight) * iconHeight;
106: leftOffset = (int) ((width - right) / iconWidth)
107: * iconWidth;
108: fillRectWithIcon(c, g, x + leftOffset, y + topOffset, width
109: - leftOffset, height - topOffset);
110: g.setClip(oldClip);
111: } else if (color != null) {
112: Color oldColor = g.getColor();
113: g.setColor(color);
114: g.fillRect(x, y, width, top);
115: g.fillRect(x, y + top, left, height - top);
116: g.fillRect(x + width - right, y + top, right, height - top);
117: g.fillRect(x + left, y + height - bottom, width - left
118: - right, bottom);
119: g.setColor(oldColor);
120: }
121: }
122:
123: public Icon getTileIcon() {
124: return tileIcon;
125: }
126:
127: public Color getMatteColor() {
128: return color;
129: }
130:
131: public boolean isBorderOpaque() {
132: return (tileIcon == null);
133: }
134:
135: }
|