001: /*
002: * GradientPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Color;
025: import java.awt.GradientPaint;
026: import java.awt.Graphics;
027: import java.awt.Graphics2D;
028: import java.awt.LayoutManager;
029: import java.awt.Paint;
030: import javax.swing.JPanel;
031: import javax.swing.UIManager;
032:
033: /* ----------------------------------------------------------
034: * CVS NOTE: Changes to the CVS repository prior to the
035: * release of version 3.0.0beta1 has meant a
036: * resetting of CVS revision numbers.
037: * ----------------------------------------------------------
038: */
039:
040: /**
041: * Simple panel with a left-right gradient background.
042: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.4 $
045: * @date $Date: 2006/05/14 06:56:07 $
046: */
047: public class GradientPanel extends JPanel {
048:
049: /** The component's gradient colour */
050: private Color gradientColor;
051:
052: /** The component's fill colour */
053: private Color fillColor;
054:
055: /** Creates a new instance of GradientPanel */
056: public GradientPanel() {
057: super ();
058: }
059:
060: public GradientPanel(boolean isDoubleBuffered) {
061: super (isDoubleBuffered);
062: }
063:
064: public GradientPanel(LayoutManager layout) {
065: super (layout);
066: }
067:
068: public GradientPanel(LayoutManager layout, boolean isDoubleBuffered) {
069: super (layout, isDoubleBuffered);
070: }
071:
072: public void setColours(Color colour1, Color colour2) {
073: gradientColor = colour1;
074: fillColor = colour2;
075: }
076:
077: public boolean isOpaque() {
078: return true;
079: }
080:
081: /**
082: * Performs the painting for this component.
083: *
084: * @param the <code>Graphics</code> object to
085: * perform the painting
086: */
087: protected void paintComponent(Graphics g) {
088:
089: super .paintComponent(g);
090:
091: // if we have no colours - use the InternalFrame colours
092:
093: if (gradientColor == null) {
094: gradientColor = UIManager
095: .getColor("InternalFrame.activeTitleBackground");
096: }
097:
098: if (fillColor == null) {
099: fillColor = UIManager
100: .getColor("InternalFrame.inactiveTitleBackground");
101: }
102:
103: int width = getWidth();
104: int height = getHeight();
105:
106: Graphics2D g2 = (Graphics2D) g;
107:
108: Paint originalPaint = g2.getPaint();
109: if (gradientColor != null && fillColor != null) {
110: GradientPaint fade = new GradientPaint(0, 0, gradientColor,
111: (int) (width * 0.9), 0, fillColor);
112:
113: g2.setPaint(fade);
114: g2.fillRect(0, 0, width, height);
115: }
116:
117: g2.setPaint(originalPaint);
118: }
119:
120: }
|