View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.gui;
14  
15  import java.awt.Color;
16  import java.awt.GradientPaint;
17  import java.awt.Graphics;
18  import java.awt.Graphics2D;
19  import java.awt.LayoutManager;
20  import java.awt.Paint;
21  import java.awt.RenderingHints;
22  
23  import javax.swing.JPanel;
24  
25  /**
26   * Panel with gradient in the background.
27   * 
28   * @author Daniel Sendula
29   */
30  public class GradientPanel extends JPanel {
31  
32      /** First colour */
33      protected Color color1 = Color.WHITE;
34  
35      /** Second colour */
36      protected Color color2 = Color.LIGHT_GRAY;
37      
38      /**
39       * Constructor
40       */
41      public GradientPanel() {
42          setOpaque(false);
43      }
44  
45      /**
46       * Constructor
47       * @param layout layout
48       */
49      public GradientPanel(LayoutManager layout) {
50          super(layout);
51          setOpaque(false);
52      }
53  
54      /**
55       * Constructor
56       * @param isDoubleBuffered is double buffered
57       */
58      public GradientPanel(boolean isDoubleBuffered) {
59          super(isDoubleBuffered);
60          setOpaque(false);
61      }
62  
63      /**
64       * Constructor
65       * @param layout layout
66       * @param isDoubleBuffered is double buffered
67       */
68      public GradientPanel(LayoutManager layout, boolean isDoubleBuffered) {
69          super(layout, isDoubleBuffered);
70          setOpaque(false);
71      }
72  
73      /**
74       * Sets gradient colours
75       * @param color1 first colour
76       * @param color2 second colour
77       */
78      public void setGradientColors(Color color1, Color color2) {
79          this.color1 = color1;
80          this.color2 = color2;
81      }
82      
83      /**
84       * Paints gradient
85       * @param g graphics
86       */
87      public void paint(Graphics g) {
88          Graphics2D g2 = (Graphics2D)g;
89          
90          RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
91          hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
92          
93          g2.setRenderingHints(hints);
94          
95          int w = getWidth();
96          int h = getHeight();
97          
98          Paint oldPaint = g2.getPaint();
99          GradientPaint newPaint = new GradientPaint(w / 2.2f, 0.0f, color1, w - w / 2.2f, h, color2);
100         
101         g2.setPaint(newPaint);
102         g2.fillRect(0, 0, w, h);
103         g2.setPaint(oldPaint);
104         super.paint(g);
105     }
106 }