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