001    /*
002     * Copyright 2002-2006 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.components;
017    
018    import java.awt.AWTException;
019    import java.awt.Color;
020    import java.awt.Graphics;
021    import java.awt.Graphics2D;
022    import java.awt.Rectangle;
023    import java.awt.Robot;
024    import java.awt.image.BufferedImage;
025    import java.awt.image.ConvolveOp;
026    import java.awt.image.Kernel;
027    
028    import javax.swing.JFrame;
029    
030    /**
031     * Idea originally from http://jroller.com/page/gfx?entry=better_shadow
032     * 
033     * @author Romain Guy
034     * @author Peter De Bruycker
035     */
036    public class ShadowBorderFrame extends JFrame {
037        private BufferedImage backgroundImage;
038        private static final int SHADOW_WIDTH = 14;
039    
040        public void paint(Graphics g) {
041            g.drawImage(backgroundImage, 0, 0, this);
042            super.paint(g);
043        }
044    
045        public void show() {
046            createShadowBorder();
047            super.show();
048            setSize(getWidth() + SHADOW_WIDTH, getHeight() + SHADOW_WIDTH);
049        }
050    
051        private void createShadowBorder() {
052            backgroundImage =
053                new BufferedImage(getWidth() + SHADOW_WIDTH, getHeight() + SHADOW_WIDTH, BufferedImage.TYPE_INT_ARGB);
054            Graphics2D g2 = (Graphics2D) backgroundImage.getGraphics();
055    
056            try {
057                Robot robot = new Robot(getGraphicsConfiguration().getDevice());
058                BufferedImage capture =
059                    robot.createScreenCapture(
060                        new Rectangle(getX(), getY(), getWidth() + SHADOW_WIDTH, getHeight() + SHADOW_WIDTH));
061                g2.drawImage(capture, null, 0, 0);
062            }
063            catch (AWTException e) {
064                e.printStackTrace();
065            }
066    
067            BufferedImage shadow =
068                new BufferedImage(getWidth() + SHADOW_WIDTH, getHeight() + SHADOW_WIDTH, BufferedImage.TYPE_INT_ARGB);
069            Graphics graphics = shadow.getGraphics();
070            graphics.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f));
071            graphics.fillRoundRect(6, 6, getWidth(), getHeight(), 12, 12);
072    
073            g2.drawImage(shadow, getBlurOp(7), 0, 0);
074        }
075        
076    
077        private ConvolveOp getBlurOp(int size) {
078            float[] data = new float[size * size];
079            float value = 1 / (float) (size * size);
080            for (int i = 0; i < data.length; i++) {
081                data[i] = value;
082            }
083            return new ConvolveOp(new Kernel(size, size, data));
084        }
085    }