001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of 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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.components;
017    
018    import java.awt.Color;
019    import java.awt.Dimension;
020    import java.awt.GradientPaint;
021    import java.awt.Graphics;
022    import java.awt.Graphics2D;
023    import java.awt.geom.Rectangle2D;
024    
025    import javax.swing.JPanel;
026    
027    import org.springframework.util.Assert;
028    
029    /**
030     * A JPanel with a nice gradient background (should be move to another package)
031     * @author cro
032     */
033    public class GradientPanel extends JPanel {
034    
035        private Color upperLeftColor;
036    
037        private Color lowerRightColor;
038    
039        public GradientPanel(Color lowerRightColor, Color upperLeftColor) {
040            this.lowerRightColor = lowerRightColor;
041            this.upperLeftColor = upperLeftColor;
042        }
043    
044        public GradientPanel(Color lowerRightColor) {
045            this(lowerRightColor, Color.white);
046        }
047    
048        public GradientPanel() {
049            this(null);
050        }
051    
052        private void paintGradientComponent(int w, int h, Graphics2D g2) {
053            if (lowerRightColor == null)
054                lowerRightColor = getBackground();
055    
056            Assert.notNull(upperLeftColor, "The OuterColor cannot be null");
057    
058            Rectangle2D rect1 = new Rectangle2D.Float(0, 0, w, h);
059            GradientPaint gp = new GradientPaint(w * .80f, h * .30f, upperLeftColor, w * .90f, h * .70f, lowerRightColor);
060            g2.setPaint(gp);
061            g2.fill(rect1);
062        }
063    
064        public void paintComponent(Graphics g) {
065            Graphics2D g2 = (Graphics2D)g;
066            Dimension d = getSize();
067            g2.setBackground(lowerRightColor);
068            g2.clearRect(0, 0, d.width, d.height);
069            paintGradientComponent(d.width, d.height, g2);
070        }
071    
072        public Color getLowerRightColor() {
073            return lowerRightColor;
074        }
075    
076        public void setLowerRightColor(Color lowerRightColor) {
077            this.lowerRightColor = lowerRightColor;
078        }
079    
080        public Color getUpperLeftColor() {
081            return upperLeftColor;
082        }
083    
084        public void setUpperLeftColor(Color upperLeftColor) {
085            this.upperLeftColor = upperLeftColor;
086        }
087    }