001: /*
002: * RectanglePainter.java
003: *
004: * Created on July 12, 2006, 6:57 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.jdesktop.swingx.painter;
011:
012: import java.awt.BasicStroke;
013: import java.awt.Color;
014: import java.awt.Graphics2D;
015: import java.awt.Insets;
016: import java.awt.Paint;
017: import java.awt.Shape;
018: import java.awt.geom.Rectangle2D;
019: import java.awt.geom.RoundRectangle2D;
020: import javax.swing.JComponent;
021:
022: //import org.jdesktop.swingx.painter.Mask;
023:
024: /**
025: *
026: * @author jm158417
027: */
028: public class RectanglePainter extends AbstractPainter {
029: private Paint fillPaint = Color.RED;
030: private Paint borderPaint = Color.BLACK;
031: private boolean rounded = false;
032: private Insets insets = new Insets(5, 5, 5, 5);
033: private int roundWidth = 20;
034: private int roundHeight = 20;
035: private double strokeWidth = 1;
036:
037: /** Creates a new instance of RectanglePainter */
038: public RectanglePainter() {
039: }
040:
041: public RectanglePainter(int top, int left, int bottom, int right,
042: int roundWidth, int roundHeight, boolean rounded,
043: Paint fillPaint, double strokeWidth, Paint borderPaint) {
044: this ();
045: insets = new Insets(top, left, bottom, right);
046: this .roundWidth = roundWidth;
047: this .roundHeight = roundHeight;
048: this .rounded = rounded;
049: this .fillPaint = fillPaint;
050: this .strokeWidth = strokeWidth;
051: this .borderPaint = borderPaint;
052: }
053:
054: protected Shape calculateShape(JComponent component) {
055: Shape shape = new Rectangle2D.Double(insets.left, insets.top,
056: component.getWidth() - insets.left - insets.right,
057: component.getHeight() - insets.top - insets.bottom);
058: if (rounded) {
059: shape = new RoundRectangle2D.Double(insets.left,
060: insets.top, component.getWidth() - insets.left
061: - insets.right, component.getHeight()
062: - insets.top - insets.bottom, roundWidth,
063: roundHeight);
064: }
065: return shape;
066: }
067:
068: protected void paintBackground(Graphics2D g, JComponent component) {
069: Shape shape = calculateShape(component);
070:
071: // background
072: g.setPaint(fillPaint);
073: g.fill(shape);
074:
075: // border
076: g.setPaint(borderPaint);
077: g.setStroke(new BasicStroke((float) strokeWidth));
078: g.draw(shape);
079:
080: // leave the clip to support masking other painters
081: g.setClip(shape);
082: }
083:
084: public Paint getFillPaint() {
085: return fillPaint;
086: }
087:
088: public void setFillPaint(Paint fillPaint) {
089: Paint oldFillPaint = getFillPaint();
090: this .fillPaint = fillPaint;
091: firePropertyChange("fillPaint", oldFillPaint, fillPaint);
092: }
093:
094: public Paint getBorderPaint() {
095: return borderPaint;
096: }
097:
098: public void setBorderPaint(Paint borderPaint) {
099: Paint oldBorderPaint = getBorderPaint();
100: this .borderPaint = borderPaint;
101: firePropertyChange("fillPaint", oldBorderPaint, borderPaint);
102: }
103:
104: public boolean isRounded() {
105: return rounded;
106: }
107:
108: public void setRounded(boolean rounded) {
109: boolean oldRounded = isRounded();
110: this .rounded = rounded;
111: firePropertyChange("rounded", oldRounded, rounded);
112: }
113:
114: public Insets getInsets() {
115: return insets;
116: }
117:
118: public void setInsets(Insets insets) {
119: Insets oldInsets = getInsets();
120: this .insets = insets;
121: firePropertyChange("insets", oldInsets, insets);
122: }
123:
124: public int getRoundWidth() {
125: return roundWidth;
126: }
127:
128: public void setRoundWidth(int roundWidth) {
129: int oldRoundWidth = getRoundWidth();
130: this .roundWidth = roundWidth;
131: firePropertyChange("roundWidth", oldRoundWidth, roundWidth);
132: }
133:
134: public int getRoundHeight() {
135: return roundHeight;
136: }
137:
138: public void setRoundHeight(int roundHeight) {
139: int oldRoundHeight = getRoundHeight();
140: this .roundHeight = roundHeight;
141: firePropertyChange("roundHeight", oldRoundHeight, roundHeight);
142: }
143:
144: public double getStrokeWidth() {
145: return strokeWidth;
146: }
147:
148: public void setStrokeWidth(double strokeWidth) {
149: double oldStrokeWidth = getStrokeWidth();
150: this .strokeWidth = strokeWidth;
151: firePropertyChange("strokeWidth", oldStrokeWidth, strokeWidth);
152: }
153:
154: /**
155: * Getter for property maskShape.
156: * @return Value of property maskShape.
157: */
158: /*
159: public Shape getMaskShape(JComponent component) {
160: return calculateShape(component);
161: }
162: */
163:
164: }
|