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
023public class MirrorFilter extends AbstractBufferedImageOp {
024    private float opacity = 1.0f;
025        private float centreY = 0.5f;
026    private float distance;
027    private float angle;
028    private float rotation;
029    private float gap;
030
031    public MirrorFilter() {
032        }
033        
034        /**
035     * Specifies the angle of the mirror.
036     * @param angle the angle of the mirror.
037     * @angle
038     * @see #getAngle
039     */
040        public void setAngle( float angle ) {
041                this.angle = angle;
042        }
043
044        /**
045     * Returns the angle of the mirror.
046     * @return the angle of the mirror.
047     * @see #setAngle
048     */
049        public float getAngle() {
050                return angle;
051        }
052        
053        public void setDistance( float distance ) {
054                this.distance = distance;
055        }
056
057        public float getDistance() {
058                return distance;
059        }
060        
061        public void setRotation( float rotation ) {
062                this.rotation = rotation;
063        }
064
065        public float getRotation() {
066                return rotation;
067        }
068        
069        public void setGap( float gap ) {
070                this.gap = gap;
071        }
072
073        public float getGap() {
074                return gap;
075        }
076        
077        /**
078     * Set the opacity of the reflection.
079     * @param opacity the opacity.
080     * @see #getOpacity
081     */
082        public void setOpacity( float opacity ) {
083                this.opacity = opacity;
084        }
085
086        /**
087     * Get the opacity of the reflection.
088     * @return the opacity.
089     * @see #setOpacity
090     */
091        public float getOpacity() {
092                return opacity;
093        }
094        
095        public void setCentreY( float centreY ) {
096                this.centreY = centreY;
097        }
098
099        public float getCentreY() {
100                return centreY;
101        }
102        
103    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
104        if ( dst == null )
105            dst = createCompatibleDestImage( src, null );
106        BufferedImage tsrc = src;
107                Shape clip;
108                int width = src.getWidth();
109                int height = src.getHeight();
110                int h = (int)(centreY * height);
111                int d = (int)(gap * height);
112
113                Graphics2D g = dst.createGraphics();
114                clip = g.getClip();
115                g.clipRect( 0, 0, width, h );
116                g.drawRenderedImage( src, null );
117                g.setClip( clip );
118                g.clipRect( 0, h+d, width, height-h-d );
119                g.translate( 0, 2*h+d );
120                g.scale( 1, -1 );
121                g.drawRenderedImage( src, null );
122                g.setPaint( new GradientPaint( 0, 0, new Color( 1.0f, 0.0f, 0.0f, 0.0f ), 0, h, new Color( 0.0f, 1.0f, 0.0f, opacity ) ) );
123                g.setComposite( AlphaComposite.getInstance( AlphaComposite.DST_IN ) );
124                g.fillRect( 0, 0, width, h );
125                g.setClip( clip );
126                g.dispose();
127        
128        return dst;
129    }
130    
131        public String toString() {
132                return "Effects/Mirror...";
133        }
134}