001: package com.xoetrope.swing.painter;
002:
003: import java.awt.Color;
004: import java.awt.GradientPaint;
005: import java.awt.Graphics2D;
006: import java.awt.geom.RoundRectangle2D;
007: import java.awt.image.BufferedImage;
008: import javax.swing.AbstractButton;
009: import javax.swing.ButtonModel;
010: import javax.swing.JComponent;
011: import net.xoetrope.xui.helper.XuiUtilities;
012: import org.jdesktop.swingx.painter.Painter;
013:
014: /**
015: * A painter for button backgrounds.
016: * <p>Copyright (c) Xoetrope Ltd., 2001-2007<br>
017: * License: see license.txt
018: * @version 1.0
019: */
020: public class XGradientButtonPainter implements Painter {
021: private BufferedImage bimg;
022:
023: public void paint(Graphics2D g, Object object, int width, int height) {
024: paint(g, (JComponent) object, width, height);
025: }
026:
027: /**
028: * Paints a background for a button
029: * @param g the graphics context
030: * @param obj The object/component being painted
031: * @param w the width coordinate
032: * @param h the height coordinate
033: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
034: * License: see license.txt
035: * $Revision: 1.3 $
036: */
037: public void paint(Graphics2D g, JComponent obj, int w, int h) {
038: AbstractButton button = (AbstractButton) obj;
039: Color end = button.getBackground();
040:
041: RoundRectangle2D.Double shape = new RoundRectangle2D.Double(0,
042: 0, w, h, 4, 4);
043: g.setClip(shape);
044:
045: ButtonModel model = button.getModel();
046: if (!model.isEnabled()) {
047: g.setColor(end);
048: g.fill(shape);
049: } else {
050: Color start = end;
051: if (model.isPressed()) {
052: start = end.darker();
053: end = start.darker();
054: } else if (model.isRollover()) {
055: start = end.brighter();
056: end = end.darker();
057: } else
058: end = end.darker();
059:
060: Color middle = new Color(
061: (start.getRed() + end.getRed()) / 2, (start
062: .getGreen() + end.getGreen()) / 2, (start
063: .getBlue() + end.getBlue()) / 2);
064:
065: Object o = obj.getClientProperty("vertical");
066: boolean isVertical = false;
067: if (o != null)
068: isVertical = "true".equals(o.toString());
069:
070: if (isVertical) {
071: if ((bimg == null) || (bimg.getWidth() != w)) {
072: bimg = XuiUtilities.createCompatibleImage(g, w, 1);
073: Graphics2D g2d = (Graphics2D) bimg.createGraphics();
074:
075: double xd = Math.min(20.0, (double) w / 6.0);
076: int x = (int) (3.0 * xd);
077: GradientPaint gradient = new GradientPaint(0, 0,
078: start, x, 0, middle, false);
079: g2d.setPaint(gradient);
080: g2d.fillRect(0, 0, x, 1);
081:
082: g2d.setColor(middle);
083: xd = w - x - xd;
084: g2d.fillRect(x, 0, (int) (xd), 1);
085: x += (int) (xd);
086:
087: gradient = new GradientPaint(x, 0, middle, w, 0,
088: end, false);
089: g2d.setPaint(gradient);
090: g2d.fillRect(x, 0, w - x, 1);
091: g2d.dispose();
092: }
093: } else {
094: if ((bimg == null) || (bimg.getHeight() != h)) {
095: bimg = XuiUtilities.createCompatibleImage(g, 1, h);
096: Graphics2D g2d = (Graphics2D) bimg.createGraphics();
097:
098: double yd = Math.min(20.0, (double) h / 6.0);
099: int y = (int) (3.0 * yd);
100: GradientPaint gradient = new GradientPaint(0, 0,
101: start, 0, y, middle, false);
102: g2d.setPaint(gradient);
103: g2d.fillRect(0, 0, 1, y);
104:
105: g2d.setColor(middle);
106: yd = h - y - yd;
107: g2d.fillRect(0, y, 1, (int) (yd));
108: y += (int) (yd);
109:
110: gradient = new GradientPaint(0, y, middle, 0, h,
111: end, false);
112: g2d.setPaint(gradient);
113: g2d.fillRect(0, y, 1, h - y);
114: g2d.dispose();
115: }
116: }
117:
118: g.drawImage(bimg, 0, 0, w, h, null);
119: }
120:
121: g.setColor(end);
122: g.drawRoundRect(0, 0, w - 1, h - 1, 4, 4);
123: }
124: }
|