001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.design.decoration.components;
021:
022: import java.awt.BasicStroke;
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Graphics;
026: import java.awt.Graphics2D;
027: import java.awt.Image;
028: import java.awt.RenderingHints;
029: import java.awt.Shape;
030: import java.awt.geom.RoundRectangle2D;
031: import java.awt.image.BufferedImage;
032: import javax.swing.GrayFilter;
033: import javax.swing.Icon;
034: import javax.swing.ImageIcon;
035:
036: public class ButtonRenderer {
037:
038: public static void paintButton(Component c, Graphics g,
039: Color fillColor, boolean gradient, Color strokeColor,
040: float strokeWidth, Icon icon) {
041: if ((fillColor == null) && (strokeColor == null)
042: && (icon == null)) {
043: return;
044: }
045:
046: Graphics2D g2 = (Graphics2D) g.create();
047: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
048: RenderingHints.VALUE_ANTIALIAS_ON);
049: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
050: RenderingHints.VALUE_STROKE_PURE);
051:
052: float arcSize = Math.max(0, ARC_SIZE - strokeWidth);
053: float strokeHalf = strokeWidth / 2;
054:
055: int w = c.getWidth();
056: int h = c.getHeight();
057:
058: Shape shape = new RoundRectangle2D.Float(strokeHalf,
059: strokeHalf, w - strokeWidth, h - strokeWidth, arcSize,
060: arcSize);
061:
062: if (fillColor != null) {
063: if (gradient && strokeColor != null) {
064: g2.setPaint(fillColor);
065: g2.fill(shape);
066:
067: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
068: RenderingHints.VALUE_STROKE_NORMALIZE);
069:
070: int rv = strokeColor.getRed();
071: int gv = strokeColor.getGreen();
072: int bv = strokeColor.getBlue();
073:
074: g2.setPaint(new Color(rv, gv, bv, 255 * 3 / 8));
075: g2.drawLine(1, h - 2, w - 2, h - 2);
076:
077: g2.setPaint(new Color(rv, gv, bv, 255 / 4));
078: g2.drawLine(2, h - 3, w - 3, h - 3);
079:
080: g2.setPaint(new Color(rv, gv, bv, 255 / 8));
081: g2.drawLine(3, h - 4, w - 4, h - 4);
082:
083: g2.setPaint(new Color(rv, gv, bv, 255 / 4));
084: g2.drawLine(w - 2, 1, w - 2, h - 2);
085:
086: g2.setPaint(new Color(rv, gv, bv, 255 / 8));
087: g2.drawLine(w - 3, 2, w - 3, h - 3);
088:
089: g2.setPaint(new Color(rv, gv, bv, 255 / 16));
090: g2.drawLine(w - 4, 3, w - 4, h - 4);
091:
092: g2.setPaint(new Color(255, 255, 255, 255 * 3 / 4));
093: g2.drawLine(1, 1, w - 2, 1);
094: g2.drawLine(1, 1, 1, h - 2);
095:
096: g2.setPaint(new Color(255, 255, 255, 255 / 2));
097: g2.drawLine(2, 2, w - 3, 2);
098: g2.drawLine(2, 2, 2, h - 3);
099:
100: g2.setPaint(new Color(255, 255, 255, 255 / 4));
101: g2.drawLine(3, 3, w - 4, 3);
102: g2.drawLine(3, 3, 3, h - 4);
103:
104: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
105: RenderingHints.VALUE_STROKE_PURE);
106: } else {
107: g2.setPaint(fillColor);
108: g2.fill(shape);
109: }
110: }
111:
112: if (strokeColor != null) {
113: g2.setStroke(new BasicStroke(strokeWidth));
114: g2.setPaint(strokeColor);
115: g2.draw(shape);
116: }
117:
118: if (icon != null) {
119: int x = (w - icon.getIconWidth()) / 2;
120: int y = (h - icon.getIconHeight()) / 2;
121:
122: icon.paintIcon(c, g, x, y);
123: }
124:
125: g2.dispose();
126: }
127:
128: public static Icon createDisabledIcon(Component c, Icon icon) {
129: Image iconImage;
130:
131: if (icon instanceof ImageIcon) {
132: iconImage = ((ImageIcon) icon).getImage();
133: } else {
134: iconImage = new BufferedImage(icon.getIconWidth(), icon
135: .getIconHeight(), BufferedImage.TYPE_INT_ARGB);
136: icon.paintIcon(c, iconImage.getGraphics(), 0, 0);
137: }
138:
139: return new ImageIcon(GrayFilter.createDisabledImage(iconImage));
140: }
141:
142: public static final float ARC_SIZE = 8;
143:
144: public static final Color NORMAL_BORDER_COLOR = new Color(0x999999);
145: public static final Color NORMAL_FILL_COLOR = null;
146: public static final float NORMAL_STROKE_WIDTH = 1;
147:
148: public static final Color DISABLED_BORDER_COLOR = new Color(
149: 0xCCCCCC);
150: public static final Color DISABLED_FILL_COLOR = null;
151: public static final float DISABLED_STROKE_WIDTH = 1;
152:
153: public static final Color PRESSED_BORDER_COLOR = new Color(0x999999);
154: public static final Color PRESSED_FILL_COLOR = new Color(0xDDDDDD);
155: public static final float PRESSED_STROKE_WIDTH = 1.3f;
156:
157: public static final Color ROLLOVER_BORDER_COLOR = new Color(
158: 0x999999);
159: public static final Color ROLLOVER_FILL_COLOR = new Color(0xF4F4F4);
160: public static final float ROLLOVER_STROKE_WIDTH = 1f;
161: }
|