001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visual.border;
042:
043: import org.netbeans.api.visual.border.Border;
044:
045: import java.awt.*;
046:
047: /**
048: * @author David Kaspar
049: */
050: public final class ImageBorder implements Border {
051:
052: private Insets borderInsets;
053: private Insets imageInsets;
054: private Image image;
055: private int width, height;
056: private int verStep, horStep;
057: private int verEdge, horEdge;
058:
059: public ImageBorder(Insets borderInsets, Insets imageInsets,
060: Image image) {
061: this .borderInsets = borderInsets;
062: this .imageInsets = imageInsets;
063: this .image = image;
064: width = image.getWidth(null);
065: height = image.getHeight(null);
066: horEdge = width - this .imageInsets.right;
067: verEdge = height - this .imageInsets.bottom;
068: horStep = horEdge - this .imageInsets.left;
069: verStep = verEdge - this .imageInsets.top;
070: }
071:
072: public Insets getInsets() {
073: return borderInsets;
074: }
075:
076: public void paint(Graphics2D gr, Rectangle bounds) {
077: int destVerMax = bounds.y + bounds.height;
078: int destHorMax = bounds.x + bounds.width;
079: int destVerEdge = destVerMax - imageInsets.bottom;
080: int destHorEdge = destHorMax - imageInsets.right;
081:
082: int horInner = bounds.width - imageInsets.left
083: - imageInsets.right;
084: int xdiv = horInner / horStep;
085: int xmod = horInner % horStep;
086:
087: gr.drawImage(image, bounds.x, bounds.y, bounds.x + xmod
088: + imageInsets.left, bounds.y + imageInsets.top, 0, 0,
089: xmod + imageInsets.left, imageInsets.top, null);
090: gr.drawImage(image, destHorEdge - xmod, destVerEdge,
091: destHorMax, destVerMax, horEdge - xmod, verEdge, width,
092: height, null);
093:
094: for (int i = 0, x = bounds.x + xmod + imageInsets.left; i < xdiv; i++, x += horStep) {
095: gr.drawImage(image, x, bounds.y, x + horStep, bounds.y
096: + imageInsets.top, imageInsets.left, 0, horEdge,
097: imageInsets.top, null);
098: gr.drawImage(image, x - xmod, destVerEdge, x - xmod
099: + horStep, destVerMax, imageInsets.left, verEdge,
100: horEdge, height, null);
101: }
102:
103: int verInner = bounds.height - imageInsets.top
104: - imageInsets.bottom;
105: int ydiv = verInner / verStep;
106: int ymod = verInner % verStep;
107:
108: gr.drawImage(image, destHorEdge, bounds.y, destHorMax, bounds.y
109: + ymod + imageInsets.top, horEdge, 0, width, ymod
110: + imageInsets.top, null);
111: gr.drawImage(image, bounds.x, destVerEdge - ymod, bounds.x
112: + imageInsets.left, destVerMax, 0, verEdge - ymod,
113: imageInsets.left, height, null);
114:
115: for (int i = 0, y = bounds.y + ymod + imageInsets.top; i < ydiv; i++, y += verStep) {
116: gr.drawImage(image, destHorEdge, y, destHorMax,
117: y + verStep, horEdge, imageInsets.top, width,
118: verEdge, null);
119: gr.drawImage(image, bounds.x, y - ymod, bounds.x
120: + imageInsets.left, y - ymod + verStep, 0,
121: imageInsets.top, imageInsets.left, verEdge, null);
122: }
123: }
124:
125: public boolean isOpaque() {
126: return false;
127: }
128:
129: }
|