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;
021:
022: import java.awt.AlphaComposite;
023: import java.awt.BasicStroke;
024: import java.awt.Color;
025: import java.awt.Composite;
026: import java.awt.Dimension;
027: import java.awt.Font;
028: import java.awt.GradientPaint;
029: import java.awt.Graphics;
030: import java.awt.Graphics2D;
031: import java.awt.Paint;
032: import java.awt.RenderingHints;
033: import java.awt.Shape;
034: import java.awt.Stroke;
035: import java.awt.TexturePaint;
036: import java.awt.geom.AffineTransform;
037: import java.awt.geom.Area;
038: import java.awt.geom.Ellipse2D;
039: import java.awt.geom.GeneralPath;
040: import java.awt.geom.Point2D;
041: import java.awt.geom.Rectangle2D;
042: import java.awt.geom.RoundRectangle2D;
043: import java.awt.image.BufferedImage;
044: import javax.swing.JLabel;
045: import org.netbeans.modules.bpel.design.geometry.FBounds;
046:
047: public class GUtils {
048:
049: private static final JLabel LABEL = new JLabel();
050:
051: public static Graphics2D createGraphics(Graphics g) {
052: Graphics2D g2 = (Graphics2D) g.create();
053: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
054: RenderingHints.VALUE_ANTIALIAS_ON);
055: // g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
056: // RenderingHints.VALUE_FRACTIONALMETRICS_ON);
057: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
058: RenderingHints.VALUE_STROKE_NORMALIZE);
059:
060: return g2;
061: }
062:
063: // public static void draw(Graphics2D g2, Shape shape, boolean normalized) {
064: // if (normalized) {
065: // g2.draw(shape);
066: // } else {
067: // g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
068: // RenderingHints.VALUE_STROKE_PURE);
069: // g2.draw(shape);
070: // g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
071: // RenderingHints.VALUE_STROKE_NORMALIZE);
072: // }
073: // }
074:
075: public static float getScale(AffineTransform at) {
076: if (at == null)
077: return 1;
078: return (float) Math.hypot(at.getScaleX(), at.getShearY());
079: }
080:
081: // public static void fill(Graphics2D g2, Shape shape) {
082: // g2.fill(shape);
083: // }
084:
085: // public static Point2D getNormalizedCenter(Graphics2D g, Shape s) {
086: //// Rectangle2D r = s.getBounds2D();
087: //// return new Point2D.Double(r.getCenterX(), r.getCenterY());
088: //
089: // AffineTransform at = g.getTransform();
090: //
091: // Rectangle2D bounds = s.getBounds2D();
092: //
093: // float x = (float) bounds.getX();
094: // float y = (float) bounds.getY();
095: // float w = (float) bounds.getWidth();
096: // float h = (float) bounds.getHeight();
097: //
098: // float[] coords = { x, y, x + w, y + h };
099: //
100: // Point2D center = null;
101: //
102: // try {
103: // at.transform(coords, 0, coords, 0, 2);
104: // float cx = 0.5f * (1 + ((int) coords[0]) + ((int) coords[2]));
105: // float cy = 0.5f * (1 + ((int) coords[1]) + ((int) coords[3]));
106: //
107: // center = new Point2D.Float(cx, cy);
108: //
109: // at.inverseTransform(center, center);
110: // } catch (Exception e) {
111: // center = new Point2D.Double(bounds.getCenterX(),
112: // bounds.getCenterY());
113: // }
114: //
115: // return center;
116: // }
117:
118: // public static void setPaint(Graphics2D g, Paint paint) {
119: // g.setPaint(paint);
120: // }
121:
122: // public static void setSolidStroke(Graphics2D g, double width) {
123: // double scale = getScale(g.getTransform());
124: //
125: // if (scale * width < 1) {
126: // width = 1.0 / scale;
127: // }
128: //
129: // g.setStroke(new BasicStroke((float) width, BasicStroke.CAP_ROUND,
130: // BasicStroke.JOIN_ROUND));
131: // }
132: //
133: //
134: // public static void setDashedStroke(Graphics2D g, double width, double dash,
135: // double space)
136: // {
137: // double scale = getScale(g.getTransform());
138: //
139: // if (scale * width < 1) {
140: // width = 1.0 / scale;
141: // }
142: //
143: // g.setStroke(new BasicStroke((float) width,
144: // BasicStroke.CAP_ROUND,
145: // BasicStroke.JOIN_ROUND, 1,
146: // new float[] { (float) dash, (float) (space + width) }, 0));
147: // }
148: //
149: //
150: // public static void setDashedStroke(Graphics2D g, double width, double dash)
151: // {
152: // setDashedStroke(g, width, dash, dash);
153: // }
154:
155: //
156: //
157: //
158: //
159: //
160: // public static Shape getTriangle(double x1, double y1, double x2, double y2,
161: // double x3, double y3)
162: // {
163: // GeneralPath path = new GeneralPath();
164: // path.moveTo((float) x1, (float) y1);
165: // path.lineTo((float) x2, (float) y2);
166: // path.lineTo((float) x3, (float) y3);
167: // path.closePath();
168: //
169: // return path;
170: // }
171:
172: public static FBounds drawString(Graphics2D g2, String string,
173: double x, double y, double width) {
174: float zoom = getScale(g2.getTransform());
175:
176: Font oldFont = g2.getFont();
177: Font font = oldFont.deriveFont(oldFont.getSize2D() * zoom);
178:
179: g2.translate(x, y);
180: g2.scale(1 / zoom, 1 / zoom);
181:
182: LABEL.setHorizontalAlignment(JLabel.LEFT);
183: LABEL.setText(string);
184: LABEL.setForeground(g2.getColor());
185: LABEL.setFont(font);
186:
187: Dimension d = LABEL.getPreferredSize();
188:
189: int w = Math.min((int) Math.round(width * zoom), d.width);
190: int h = d.height;
191:
192: LABEL.setBounds(0, 0, w, h);
193: LABEL.paint(g2);
194:
195: g2.scale(zoom, zoom);
196: g2.translate(-x, -y);
197:
198: g2.setFont(oldFont);
199:
200: return new FBounds(x, y, w / zoom, h / zoom);
201: }
202:
203: public static FBounds drawXCenteredString(Graphics2D g2,
204: String text, double cx, double y, double width) {
205: float zoom = getScale(g2.getTransform());
206:
207: Font oldFont = g2.getFont();
208: Font font = oldFont.deriveFont(oldFont.getSize2D() * zoom);
209:
210: LABEL.setHorizontalAlignment(JLabel.CENTER);
211: LABEL.setText(text.trim());
212: LABEL.setForeground(g2.getColor());
213: LABEL.setFont(font);
214:
215: Dimension d = LABEL.getPreferredSize();
216:
217: int w = (int) Math.min(Math.round(width * zoom), d.width);
218: int h = d.height;
219:
220: double x = cx - ((double) w / 2 / zoom);
221:
222: g2.translate(x, y);
223: g2.scale(1 / zoom, 1 / zoom);
224:
225: LABEL.setBounds(0, 0, w, h);
226: LABEL.paint(g2);
227:
228: g2.scale(zoom, zoom);
229: g2.translate(-x, -y);
230: g2.setFont(oldFont);
231:
232: return new FBounds(x, y, w / zoom, h / zoom);
233: }
234:
235: public static FBounds drawCenteredString(Graphics2D g2,
236: String string, double cx, double cy, double width) {
237: float zoom = getScale(g2.getTransform());
238:
239: Font oldFont = g2.getFont();
240: Font font = oldFont.deriveFont(oldFont.getSize2D() * zoom);
241:
242: LABEL.setHorizontalAlignment(JLabel.CENTER);
243: LABEL.setText(string);
244: LABEL.setFont(font);
245: LABEL.setForeground(g2.getColor());
246:
247: Dimension d = LABEL.getPreferredSize();
248:
249: int w = (int) Math.min(Math.round(width * zoom), d.width);
250: int h = Math.round(d.height);
251:
252: double x = cx - 0.5 * w / zoom;
253: double y = cy - 0.5 * h / zoom;
254:
255: g2.translate(x, y);
256: g2.scale(1.0 / zoom, 1.0 / zoom);
257:
258: LABEL.setBounds(0, 0, w, h);
259: LABEL.paint(g2);
260:
261: g2.scale(zoom, zoom);
262: g2.translate(-x, -y);
263: g2.setFont(oldFont);
264:
265: return new FBounds(x, y, w / zoom, h / zoom);
266: }
267:
268: public static void drawGlow(Graphics2D g2, Area area, double width,
269: Color color) {
270: Shape oldClip = g2.getClip();
271: Stroke oldStroke = g2.getStroke();
272: Composite oldComposite = g2.getComposite();
273: Paint oldPaint = g2.getPaint();
274:
275: AffineTransform at = g2.getTransform();
276:
277: double pixelWidth = getScale(g2.getTransform()) * width;
278:
279: int steps = Math.min(Math.max(2, (int) Math
280: .floor(pixelWidth / 1.4)), 8);
281:
282: Area newClip = new Area(oldClip);
283: newClip.subtract(area);
284:
285: g2.setClip(newClip);
286: g2.setPaint(color);
287: g2.setComposite(AlphaComposite.getInstance(
288: AlphaComposite.SRC_OVER, 0.25f / steps));
289:
290: width *= 2;
291:
292: for (int i = steps - 1; i >= 0; i--) {
293: double t = ((double) (i + 1)) / steps;
294: g2.setStroke(new BasicStroke(
295: (float) ((Math.pow(2, t) - 1) * width)));
296: g2.draw(area);
297: }
298:
299: g2.setComposite(oldComposite);
300: g2.setStroke(oldStroke);
301: g2.setClip(oldClip);
302: g2.setPaint(oldPaint);
303: }
304: }
|