001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui;
034:
035: import java.awt.*;
036:
037: import javax.swing.JPanel;
038:
039: import com.vividsolutions.jump.workbench.ui.renderer.style.BasicStyle;
040:
041: /**
042: * Displays a colour.
043: */
044: public class ColorPanel extends JPanel {
045: private Color fillColor = Color.red;
046: private Color lineColor = Color.green;
047: private int margin = 0;
048:
049: public ColorPanel() {
050: setBackground(Color.white);
051: }
052:
053: protected void paintComponent(Graphics g) {
054: super .paintComponent(g);
055:
056: Graphics2D g2 = (Graphics2D) g;
057: //Before I simply set the ColorPanel's background colour. But I found this
058: //caused weird paint effects e.g. a second copy of the panel appearing
059: //at the top left corner of the rendered image. [Jon Aquino].
060: //<<TODO:DESIGN>> Use the GraphicsState class [Jon Aquino]
061: Color originalColor = g.getColor();
062: g.setColor(getBackground());
063: ((Graphics2D) g).setStroke(fillStroke);
064: g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
065: g.setColor(fillColor);
066: if (fillPattern != null)
067: ((Graphics2D) g).setPaint(fillPattern);
068: g.fillRect(margin, margin, getWidth() - 1 - margin - margin,
069: getHeight() - 1 - margin - margin);
070:
071: if (lineColor != null) {
072: g.setColor(lineColor);
073: ((Graphics2D) g).setStroke(lineStroke);
074:
075: //-1 to ensure the rectangle doesn't extend past the panel [Jon Aquino]
076: g.drawRect(margin, margin,
077: getWidth() - 1 - margin - margin, getHeight() - 1
078: - margin - margin);
079: }
080:
081: //<<TODO:DESIGN>> Put the next line in a finally block [Jon Aquino]
082: g.setColor(originalColor);
083: }
084:
085: /** Workaround for bug 4238829 in the Java bug database */
086: public void setBounds(int x, int y, int w, int h) {
087: super .setBounds(x, y, w, h);
088: validate();
089: }
090:
091: public void setFillColor(Color fillColor) {
092: this .fillColor = fillColor;
093: }
094:
095: /**
096: * @param lineColor the new line colour, or null to not draw the line
097: */
098: public void setLineColor(Color lineColor) {
099: this .lineColor = lineColor;
100: }
101:
102: public void setMargin(int margin) {
103: this .margin = margin;
104: }
105:
106: public Color getFillColor() {
107: return fillColor;
108: }
109:
110: public Color getLineColor() {
111: return lineColor;
112: }
113:
114: private BasicStroke fillStroke = new BasicStroke(1);
115: private Paint fillPattern = null;
116: private int lineWidth = 1;
117: private BasicStroke lineStroke = new BasicStroke(lineWidth);
118:
119: public void setLineWidth(int lineWidth) {
120: lineStroke = new BasicStroke(lineWidth);
121: this .lineWidth = lineWidth;
122: }
123:
124: public void setStyle(BasicStyle style) {
125: //if (style.isRenderingLinePattern())
126: setLineStroke(style.getLineStroke());
127: if (style.isRenderingFillPattern())
128: fillPattern = style.getFillPattern();
129: }
130:
131: public void setLineStroke(BasicStroke stroke) {
132: float width = Math.min(3, stroke.getLineWidth());
133: lineStroke = new BasicStroke(width, BasicStroke.CAP_BUTT,
134: BasicStroke.JOIN_BEVEL, 1.0f, stroke.getDashArray(), 0);
135: }
136:
137: public int getLineWidth() {
138: return lineWidth;
139: }
140:
141: }
|