01: /*
02: * The contents of this file are subject to the terms of the Common Development
03: * and Distribution License (the License). You may not use this file except in
04: * compliance with the License.
05: *
06: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
07: * or http://www.netbeans.org/cddl.txt.
08: *
09: * When distributing Covered Code, include this CDDL Header Notice in each file
10: * and include the License file at http://www.netbeans.org/cddl.txt.
11: * If applicable, add the following below the CDDL Header, with the fields
12: * enclosed by brackets [] replaced by your own identifying information:
13: * "Portions Copyrighted [year] [name of copyright owner]"
14: *
15: * The Original Software is NetBeans. The Initial Developer of the Original
16: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17: * Microsystems, Inc. All Rights Reserved.
18: */
19:
20: package org.netbeans.modules.bpel.design.decoration.components;
21:
22: import java.awt.Graphics;
23: import java.awt.Graphics2D;
24: import java.awt.Paint;
25: import java.awt.RenderingHints;
26: import java.awt.Stroke;
27:
28: /**
29: *
30: * @author aa160298
31: */
32: public abstract class Icon2D {
33:
34: protected double getDesignOriginX() {
35: return 0;
36: }
37:
38: protected double getDesignOriginY() {
39: return 0;
40: }
41:
42: protected abstract double getDesignWidth();
43:
44: protected abstract double getDesignHeight();
45:
46: public final void paint(Graphics g, int x, int y, int w, int h) {
47: Graphics2D g2 = (Graphics2D) g;
48:
49: Object oldAntialiasing = g2
50: .getRenderingHint(RenderingHints.KEY_ANTIALIASING);
51: Object oldStrokeControl = g2
52: .getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
53:
54: Stroke oldStroke = g2.getStroke();
55: Paint oldPaint = g2.getPaint();
56:
57: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
58: RenderingHints.VALUE_ANTIALIAS_ON);
59: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
60: RenderingHints.VALUE_STROKE_PURE);
61:
62: double desWidth = getDesignWidth();
63: double desHeight = getDesignHeight();
64:
65: double cx = x + 0.5 * w;
66: double cy = y + 0.5 * h;
67:
68: double scale = Math.min((double) w / desWidth, (double) h
69: / desHeight);
70: double backScale = 1.0 / scale;
71:
72: double tx = -desWidth / 2 + getDesignOriginX();
73: double ty = -desHeight / 2 + getDesignOriginY();
74:
75: g2.translate(cx, cy);
76: g2.scale(scale, scale);
77: g2.translate(tx, ty);
78:
79: paint(g2);
80:
81: g2.translate(-tx, -ty);
82: g2.scale(backScale, backScale);
83: g2.translate(-cx, -cy);
84:
85: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
86: oldAntialiasing);
87: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
88: oldStrokeControl);
89: g2.setStroke(oldStroke);
90: g2.setPaint(oldPaint);
91: }
92:
93: protected abstract void paint(Graphics2D g2);
94: }
|