svn-gvsig-desktop / tags / v1_1_Build_1001 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / ColorPanel.java @ 11984
History | View | Annotate | Download (4.85 KB)
1 |
|
---|---|
2 |
/*
|
3 |
* The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
|
4 |
* for visualizing and manipulating spatial features with geometry and attributes.
|
5 |
*
|
6 |
* Copyright (C) 2003 Vivid Solutions
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Vivid Solutions
|
25 |
* Suite #1A
|
26 |
* 2328 Government Street
|
27 |
* Victoria BC V8T 5G5
|
28 |
* Canada
|
29 |
*
|
30 |
* (250)385-6040
|
31 |
* www.vividsolutions.com
|
32 |
*/
|
33 |
|
34 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
35 |
*
|
36 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
37 |
*
|
38 |
* This program is free software; you can redistribute it and/or
|
39 |
* modify it under the terms of the GNU General Public License
|
40 |
* as published by the Free Software Foundation; either version 2
|
41 |
* of the License, or (at your option) any later version.
|
42 |
*
|
43 |
* This program is distributed in the hope that it will be useful,
|
44 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
45 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
46 |
* GNU General Public License for more details.
|
47 |
*
|
48 |
* You should have received a copy of the GNU General Public License
|
49 |
* along with this program; if not, write to the Free Software
|
50 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
51 |
*
|
52 |
* For more information, contact:
|
53 |
*
|
54 |
* Generalitat Valenciana
|
55 |
* Conselleria d'Infraestructures i Transport
|
56 |
* Av. Blasco Ib??ez, 50
|
57 |
* 46010 VALENCIA
|
58 |
* SPAIN
|
59 |
*
|
60 |
* +34 963862235
|
61 |
* gvsig@gva.es
|
62 |
* www.gvsig.gva.es
|
63 |
*
|
64 |
* or
|
65 |
*
|
66 |
* IVER T.I. S.A
|
67 |
* Salamanca 50
|
68 |
* 46005 Valencia
|
69 |
* Spain
|
70 |
*
|
71 |
* +34 963163400
|
72 |
* dac@iver.es
|
73 |
*/
|
74 |
package com.iver.cit.gvsig.gui.panels; |
75 |
|
76 |
import java.awt.BasicStroke; |
77 |
import java.awt.Color; |
78 |
import java.awt.Graphics; |
79 |
import java.awt.Graphics2D; |
80 |
|
81 |
import javax.swing.JPanel; |
82 |
|
83 |
|
84 |
/**
|
85 |
* Displays a colour.
|
86 |
*/
|
87 |
public class ColorPanel extends JPanel { |
88 |
private Color fillColor = Color.red; |
89 |
private Color lineColor = Color.green; |
90 |
private int margin = 0; |
91 |
|
92 |
public ColorPanel() {
|
93 |
setBackground(Color.white);
|
94 |
} |
95 |
|
96 |
protected void paintComponent(Graphics g) { |
97 |
super.paintComponent(g);
|
98 |
|
99 |
//Before I simply set the ColorPanel's background colour. But I found this
|
100 |
//caused weird paint effects e.g. a second copy of the panel appearing
|
101 |
//at the top left corner of the rendered image. [Jon Aquino].
|
102 |
//<<TODO:DESIGN>> Use the GraphicsState class [Jon Aquino]
|
103 |
Color originalColor = g.getColor();
|
104 |
g.setColor(getBackground()); |
105 |
((Graphics2D)g).setStroke(fillStroke);
|
106 |
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); |
107 |
g.setColor(fillColor); |
108 |
g.fillRect(margin, margin, getWidth() - 1 - margin - margin,
|
109 |
getHeight() - 1 - margin - margin);
|
110 |
|
111 |
if (lineColor != null) { |
112 |
g.setColor(lineColor); |
113 |
((Graphics2D)g).setStroke(lineStroke);
|
114 |
|
115 |
//-1 to ensure the rectangle doesn't extend past the panel [Jon Aquino]
|
116 |
g.drawRect(margin, margin, getWidth() - 1 - margin - margin,
|
117 |
getHeight() - 1 - margin - margin);
|
118 |
} |
119 |
|
120 |
//<<TODO:DESIGN>> Put the next line in a finally block [Jon Aquino]
|
121 |
g.setColor(originalColor); |
122 |
} |
123 |
|
124 |
/** Workaround for bug 4238829 in the Java bug database */
|
125 |
public void setBounds(int x, int y, int w, int h) { |
126 |
super.setBounds(x, y, w, h);
|
127 |
validate(); |
128 |
} |
129 |
|
130 |
public void setFillColor(Color fillColor) { |
131 |
this.fillColor = fillColor;
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* @param lineColor the new line colour, or null to not draw the line
|
136 |
*/
|
137 |
public void setLineColor(Color lineColor) { |
138 |
this.lineColor = lineColor;
|
139 |
} |
140 |
|
141 |
public void setMargin(int margin) { |
142 |
this.margin = margin;
|
143 |
} |
144 |
public Color getFillColor() { |
145 |
return fillColor;
|
146 |
} |
147 |
|
148 |
public Color getLineColor() { |
149 |
return lineColor;
|
150 |
} |
151 |
|
152 |
private BasicStroke fillStroke = new BasicStroke(1); |
153 |
private int lineWidth = 1; |
154 |
private BasicStroke lineStroke = new BasicStroke(lineWidth); |
155 |
|
156 |
public void setLineWidth(int lineWidth) { |
157 |
lineStroke = new BasicStroke(lineWidth); |
158 |
this.lineWidth = lineWidth;
|
159 |
} |
160 |
|
161 |
public int getLineWidth() { |
162 |
return lineWidth;
|
163 |
} |
164 |
|
165 |
} |