001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.geom.*;
021import java.awt.image.*;
022
023/**
024 * A filter to add a border around an image using the supplied Paint, which may be null for no painting.
025 */
026public class BorderFilter extends AbstractBufferedImageOp {
027
028        private int leftBorder, rightBorder;
029        private int topBorder, bottomBorder;
030        private Paint borderPaint;
031
032    /**
033     * Construct a BorderFilter which does nothing.
034     */
035        public BorderFilter() {
036        }
037
038    /**
039     * Construct a BorderFilter.
040         * @param leftBorder the left border value
041         * @param topBorder the top border value
042         * @param rightBorder the right border value
043         * @param bottomBorder the bottom border value
044         * @param borderPaint the paint with which to fill the border
045     */
046        public BorderFilter( int leftBorder, int topBorder, int rightBorder, int bottomBorder, Paint borderPaint ) {
047                this.leftBorder = leftBorder;
048                this.topBorder = topBorder;
049                this.rightBorder = rightBorder;
050                this.bottomBorder = bottomBorder;
051                this.borderPaint = borderPaint;
052        }
053
054        /**
055         * Set the border size on the left edge.
056         * @param leftBorder the number of pixels of border to add to the edge
057     * @min-value 0
058     * @see #getLeftBorder
059         */
060        public void setLeftBorder(int leftBorder) {
061                this.leftBorder = leftBorder;
062        }
063        
064    /**
065     * Returns the left border value.
066     * @return the left border value.
067     * @see #setLeftBorder
068     */
069        public int getLeftBorder() {
070                return leftBorder;
071        }
072        
073        /**
074         * Set the border size on the right edge.
075         * @param rightBorder the number of pixels of border to add to the edge
076     * @min-value 0
077     * @see #getRightBorder
078         */
079        public void setRightBorder(int rightBorder) {
080                this.rightBorder = rightBorder;
081        }
082        
083    /**
084     * Returns the right border value.
085     * @return the right border value.
086     * @see #setRightBorder
087     */
088        public int getRightBorder() {
089                return rightBorder;
090        }
091        
092        /**
093         * Set the border size on the top edge.
094         * @param topBorder the number of pixels of border to add to the edge
095     * @min-value 0
096     * @see #getTopBorder
097         */
098        public void setTopBorder(int topBorder) {
099                this.topBorder = topBorder;
100        }
101
102    /**
103     * Returns the top border value.
104     * @return the top border value.
105     * @see #setTopBorder
106     */
107        public int getTopBorder() {
108                return topBorder;
109        }
110
111        /**
112         * Set the border size on the bottom edge.
113         * @param bottomBorder the number of pixels of border to add to the edge
114     * @min-value 0
115     * @see #getBottomBorder
116         */
117        public void setBottomBorder(int bottomBorder) {
118                this.bottomBorder = bottomBorder;
119        }
120
121    /**
122     * Returns the border border value.
123     * @return the border border value.
124     * @see #setBottomBorder
125     */
126        public int getBottomBorder() {
127                return bottomBorder;
128        }
129
130        /**
131         * Set the border paint.
132         * @param borderPaint the paint with which to fill the border
133     * @see #getBorderPaint
134         */
135        public void setBorderPaint( Paint borderPaint ) {
136                this.borderPaint = borderPaint;
137        }
138
139        /**
140         * Get the border paint.
141         * @return the paint with which to fill the border
142     * @see #setBorderPaint
143         */
144        public Paint getBorderPaint() {
145                return borderPaint;
146        }
147
148        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
149                int width = src.getWidth();
150                int height = src.getHeight();
151
152                if ( dst == null )
153                        dst = new BufferedImage( width+leftBorder+rightBorder, height+topBorder+bottomBorder, src.getType() );
154                Graphics2D g = dst.createGraphics();
155                if ( borderPaint != null ) {
156                        g.setPaint( borderPaint );
157                        if ( leftBorder > 0 )
158                                g.fillRect( 0, 0, leftBorder, height );
159                        if ( rightBorder > 0 )
160                                g.fillRect( width-rightBorder, 0, rightBorder, height );
161                        if ( topBorder > 0 )
162                                g.fillRect( leftBorder, 0, width-leftBorder-rightBorder, topBorder );
163                        if ( bottomBorder > 0 )
164                                g.fillRect( leftBorder, height-bottomBorder, width-leftBorder-rightBorder, bottomBorder );
165                }
166                g.drawRenderedImage( src, AffineTransform.getTranslateInstance( leftBorder, rightBorder ) );
167                g.dispose();
168                return dst;
169        }
170
171        public String toString() {
172                return "Distort/Border...";
173        }
174}