001: package com.xoetrope.swing.painter;
002:
003: import java.awt.BasicStroke;
004: import java.awt.Color;
005: import java.awt.GradientPaint;
006: import java.awt.Graphics2D;
007: import java.awt.RenderingHints;
008: import java.awt.geom.Arc2D;
009: import java.awt.geom.Area;
010: import java.awt.geom.Ellipse2D;
011: import java.awt.geom.Rectangle2D;
012: import java.awt.image.BufferedImage;
013: import javax.imageio.ImageIO;
014: import javax.swing.AbstractButton;
015: import javax.swing.ButtonModel;
016: import javax.swing.JComponent;
017:
018: /**
019: * A painter for toggle button backgrounds.
020: * <p>Copyright (c) Xoetrope Ltd., 2001-2007<br>
021: * License: see license.txt
022: * @version 1.0
023: */
024: public class XGradientToggleButtonPainter extends
025: XGradientButtonPainter {
026: /**
027: * Paints a background for a button
028: * @param g the graphics context
029: * @param obj The object/component being painted
030: * @param w the width coordinate
031: * @param h the height coordinate
032: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
033: * License: see license.txt
034: * $Revision: 1.3 $
035: */
036: public void paint(Graphics2D g, JComponent obj, int w, int h) {
037: try {
038: Graphics2D g2d = (Graphics2D) g.create();
039:
040: super .paint(g, obj, w, h);
041:
042: AbstractButton button = (AbstractButton) obj;
043: ButtonModel model = button.getModel();
044: BufferedImage bi = ImageIO.read(getClass().getResource(
045: "/com/xoetrope/swing/painter/"
046: + (model.isSelected() ? "open.gif"
047: : "closed.gif")));
048:
049: Object o = obj.getClientProperty("vertical");
050: boolean isVertical = false;
051: if (o != null)
052: isVertical = "true".equals(o.toString());
053:
054: int size = isVertical ? w : h;
055: int dx = (size - bi.getWidth()) / 2;
056: int dy = (size - bi.getHeight()) / 2;
057:
058: Color end = button.getBackground().darker();
059: Color start = end;
060:
061: double hd = (double) (isVertical ? w : h);
062: double midPoint = 2.0 * hd / 3.0;
063: Rectangle2D.Double rect = new Rectangle2D.Double(0.0, 0.0,
064: midPoint, hd);
065: Ellipse2D.Double circle = new Ellipse2D.Double(midPoint
066: - hd / 2.0, 0.0, hd, hd);
067: Area area = new Area(rect);
068: area.add(new Area(circle));
069:
070: if (model.isEnabled()) {
071: if (model.isPressed()) {
072: start = end.darker();
073: end = start.darker();
074: } else if (model.isRollover()) {
075: start = end.brighter();
076: end = end.darker();
077: } else {
078: end = end.darker();
079: }
080: }
081:
082: start = new Color(start.getRed(), start.getGreen(), start
083: .getBlue(), 96);
084: end = new Color(end.getRed(), end.getGreen(),
085: end.getBlue(), 96);
086:
087: GradientPaint gradient = new GradientPaint(0, size / 2,
088: start, size + size / 2, size / 2, end, false);
089: g2d.setPaint(gradient);
090:
091: g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
092: RenderingHints.VALUE_RENDER_QUALITY);
093: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
094: RenderingHints.VALUE_ANTIALIAS_ON);
095: g2d.fill(area);
096:
097: g2d.setColor(end);
098: g2d.draw(area);
099:
100: g2d.setColor(new Color(end.getRed(), end.getGreen(), end
101: .getBlue(), 56));
102: g2d.setStroke(new BasicStroke(2.0F, BasicStroke.CAP_ROUND,
103: BasicStroke.JOIN_ROUND));
104: Arc2D.Double arc = new Arc2D.Double(midPoint - hd / 2.0
105: + 1.0, 1.0, hd - 2.0, hd - 2.0, 0.0, -75.0,
106: Arc2D.OPEN);
107: g2d.draw(arc);
108:
109: if (isVertical)
110: g2d.drawLine(1, (int) hd - 1, (int) midPoint,
111: (int) hd - 1);
112:
113: g2d.drawImage(bi, isVertical ? dy : dx, isVertical ? dy
114: : dx, null);
115: } catch (Exception ex) {
116: ex.printStackTrace();
117: }
118: }
119: }
|