Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src-test-ui / org / gvsig / rastertools / enhanced / graphics / panels / EcualizationHistogram.java @ 19378

History | View | Annotate | Download (9.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.rastertools.enhanced.graphics.panels;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Color;
23
import java.awt.Dimension;
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.util.ArrayList;
27

    
28
import javax.swing.JComboBox;
29
import javax.swing.JFormattedTextField;
30
import javax.swing.JPanel;
31
import javax.swing.JSlider;
32

    
33
import org.gvsig.raster.beans.canvas.DrawableElement;
34
import org.gvsig.raster.beans.canvas.GCanvas;
35
import org.gvsig.raster.beans.canvas.layers.Border;
36
import org.gvsig.raster.beans.canvas.layers.GraphicHistogram;
37
import org.gvsig.raster.beans.canvas.layers.MinMaxLines;
38
import org.gvsig.raster.beans.canvas.layers.StraightLine;
39

    
40
/**
41
 * Gr?fico con los elementos para la modificaci?n de un histograma.
42
 * 
43
 * 14-oct-2007
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class EcualizationHistogram  extends JPanel {
47
        public Color                             borderColor = Color.WHITE;
48
        public Color                             minmaxColor = Color.WHITE;
49
        private static final long                serialVersionUID = 5431466034535083594L;
50
        private JComboBox                        lineType = null;
51
        private JComboBox                        graphicType = null;
52
        private JComboBox                        histogramBand = null;
53
        private JSlider                          slider = null;
54
        private JFormattedTextField              minValue = null;
55
        private JFormattedTextField              maxValue = null;
56
        private GCanvas                          canvas = null;
57
        private JPanel                           north = null;
58
        private JPanel                           south = null;
59
        private DrawableElement                  dElement = null;
60
        private double[]                         histogramR = new double[]{0, 0, 3, 4, 5, 8, 7, 18, 45, 36, 21, 36, 12, 23, 23, 40, 17, 10, 5, 1, 0, 0, 0};
61
        private double[]                         histogramG = new double[]{8, 7, 18, 45, 36, 21, 36, 12, 23, 23, 40, 17, 16, 15, 13, 10, 5, 6, 7, 0, 2, 1};
62
        private double[]                         histogramB = new double[]{0, 0, 3, 4, 5, 8, 7, 18, 45, 36, 21, 36, 12, 23, 23, 40, 17, 10, 5, 1, 0, 0, 0};
63
        public EcualizationHistogramListener     listener = null;
64
        private int                              min = 0, max = 255;
65
        private MinMaxLines                      minMaxLines = null;               
66
        
67
        /**
68
         * Constructor. Se asignan los histogramas correspondientes a las bandas
69
         * R, G y B
70
         * @param hR Histograma de la banda R
71
         * @param hG Histograma de la banda G
72
         * @param hB Histograma de la banda B
73
         */
74
        public EcualizationHistogram(double[] hR, double[] hG, double[] hB, int min, int max) {
75
                this.histogramR = hR;
76
                this.histogramG = hG;
77
                this.histogramB = hB;
78
                this.min = min;
79
                this.max = max;
80
                init();
81
        }
82
        
83
        /**
84
         * Constructor para pruebas. Se toman los valores por defecto de los histogramas
85
         */
86
        public EcualizationHistogram() {
87
                init();
88
        }
89
        
90
        /**
91
         * A?ade un elemento dibujable a la lista
92
         * @param line Elemento dibujable
93
         * @param name Nombre del elemento
94
         */
95
        public void addDrawableElement(DrawableElement line, String name) {
96
                getLineType().addItem(name);
97
        }
98
        
99
        private void init() {
100
                createDrawableElements();
101
                listener = new EcualizationHistogramListener(this);
102
                this.setLayout(new BorderLayout());
103
                this.add(getNorthPanel(), BorderLayout.NORTH);
104
                this.add(getCanvas(), BorderLayout.CENTER);
105
                this.add(getSouthPanel(), BorderLayout.SOUTH);
106
        }
107

    
108
        /**
109
         * Crea la lista de objetos dibujables y la guarda en un array
110
         */
111
        private void createDrawableElements() {                
112
                dElement = new StraightLine(Color.YELLOW);
113
                getLineType().addItem("Lineal");
114
        }
115
        
116
        /**
117
         * Obtiene un elemento dibujable
118
         * @param pos
119
         * @return
120
         */
121
        public DrawableElement getDrawableElement(int pos) {
122
                return (DrawableElement)canvas.getDrawableElements().get(pos);//(dElements != null && pos >= 0 && pos < dElements.length) ? dElements[pos] : null;
123
        }
124
        
125
        /**
126
         * Obtiene el n?mero de elementos dibujables en el array
127
         * @return Numero de elementos dibujables
128
         */
129
        public int getDrawableElementsCount() {
130
                return canvas.getDrawableElements().size();
131
        }
132
        
133
        /**
134
         * Obtiene la lista de elementos que se dibujan sobre el canvas
135
         * @return DrawableElement[]
136
         */
137
        public ArrayList getDrawableElements() {
138
                return canvas.getDrawableElements();
139
        }
140
        
141
        /**
142
         * Obtiene el panel con los combos de selecci?n de gr?fico y bandas
143
         * @return JPanel
144
         */
145
        public JPanel getNorthPanel() {
146
                if(north == null) {
147
                        north = new JPanel();
148
                        north.setLayout(new BorderLayout());
149
                        
150
                        JPanel combos = new JPanel();
151
                        combos.setLayout(new GridBagLayout());
152
                        combos.add(getLineType());
153
                        combos.add(getHistogramBand());
154
                        combos.add(getGraphicType());
155
                        
156
                        north.add(combos, BorderLayout.NORTH);
157
                        north.add(getSlider(), BorderLayout.CENTER);
158
                }
159
                return north;
160
        }
161
        
162
        /**
163
         * Obtiene el panel con las entradas de texto para los valores
164
         * m?ximo y m?nimo
165
         * @return JPanel
166
         */
167
        public JPanel getSouthPanel() {
168
                if(south == null) {
169
                        south = new JPanel();
170
                        south.setLayout(new GridBagLayout());
171
                        GridBagConstraints gb = new GridBagConstraints();
172
                        gb.weightx = 1;
173
                        
174
                        gb.anchor = GridBagConstraints.WEST;
175
                        south.add(getMinValue(), gb);
176
                        
177
                        gb.anchor = GridBagConstraints.EAST;
178
                        south.add(getMaxValue(), gb);
179
                }
180
                return south;
181
        }
182
        
183
        /**
184
         * Obtiene el lienzo donde se dibujan las gr?ficas
185
         * @return GCanvas
186
         */
187
        public GCanvas getCanvas() {
188
                if(canvas == null) {
189
                        GraphicHistogram gHistR = new GraphicHistogram(histogramR, Color.RED);
190
                        gHistR.setType(GraphicHistogram.TYPE_LINE);
191
                        minMaxLines = new MinMaxLines(minmaxColor);
192
                        
193
                        canvas = new GCanvas(Color.BLACK);
194
                        canvas.addDrawableElement(new Border(borderColor));
195
                        canvas.addDrawableElement(gHistR);
196
                        canvas.addDrawableElement(minMaxLines);
197
                        canvas.addDrawableElement(dElement); 
198
                }
199
                return canvas;
200
        }
201
                
202
        /**
203
         * Obtiene el borde del gr?fico si lo tiene
204
         * return borde del gr?fico o null si no lo tiene
205
         */
206
        public Border getGraphicBorder() {
207
                for (int i = 0; i < canvas.getDrawableElements().size(); i++) {
208
                        if(canvas.getDrawableElements().get(i) instanceof Border)
209
                                return (Border)canvas.getDrawableElements().get(i);
210
                }
211
                return null;
212
        }
213

    
214
        /**
215
         * Obtiene las l?neas de m?ximo y m?nimo
216
         * return L?neas de m?ximo y m?nimo
217
         */
218
        public MinMaxLines getMinMaxLines() {
219
                for (int i = 0; i < canvas.getDrawableElements().size(); i++) {
220
                        if(canvas.getDrawableElements().get(i) instanceof MinMaxLines)
221
                                return (MinMaxLines)canvas.getDrawableElements().get(i);
222
                }
223
                return null;
224
        }
225
        
226
        /**
227
         * Obtiene el combo con la banda seleccionada
228
         * @return JComboBox
229
         */
230
        public JComboBox getHistogramBand() {
231
                if(histogramBand == null) {
232
                        histogramBand = new JComboBox();
233
                        histogramBand.addItem("R");
234
                        histogramBand.addItem("G");
235
                        histogramBand.addItem("B");
236
                }
237
                return histogramBand;
238
        }
239

    
240
        /**
241
         * Obtiene el JComboBox que selecciona el tipo de l?nea
242
         * @return JComboBox
243
         */
244
        public JComboBox getLineType() {
245
                if(lineType == null) {
246
                        lineType = new JComboBox();
247
                }
248
                return lineType;
249
        }
250
        
251
        /**
252
         * Obtiene el JComboBox que selecciona el tipo de gr?fico
253
         * @return JComboBox
254
         */
255
        public JComboBox getGraphicType() {
256
                if(graphicType == null) {
257
                        graphicType = new JComboBox();
258
                        graphicType.addItem("Line");
259
                        graphicType.addItem("Fill");
260
                }
261
                return graphicType;
262
        }
263

    
264
        /**
265
         * Obtiene el campo de texto con el valor m?ximo
266
         * @return DataInputField
267
         */
268
        public JFormattedTextField getMaxValue() {
269
                if(maxValue == null) {
270
                        maxValue = new JFormattedTextField(String.valueOf(max));
271
                        maxValue.setPreferredSize(new Dimension(34, 20));
272
                }
273
                return maxValue;
274
        }
275

    
276
        /**
277
         * Obtiene el campo de texto con el valor m?nimo
278
         * @return DataInputField
279
         */
280
        public JFormattedTextField getMinValue() {
281
                if(minValue == null) {
282
                        minValue = new JFormattedTextField(String.valueOf(min));
283
                        minValue.setPreferredSize(new Dimension(34, 20));
284
                }
285
                return minValue;
286
        }
287

    
288
        /**
289
         * Obtiene el slider 
290
         * @return JSlider
291
         */
292
        public JSlider getSlider() {
293
                if(slider == null) {
294
                        slider = new JSlider();
295
                }
296
                return slider;
297
        }
298
        
299
        /**
300
         * Obtiene el histograma correspondiente a una banda de color
301
         * @param value Banda de la cual se quiere obtener el histograma
302
         * @return histograma
303
         */
304
        public double[] getHistogram(String value) {
305
                if(value.equals("R"))
306
                        return histogramR;
307
                if(value.equals("G"))
308
                        return histogramG;
309
                if(value.equals("B"))
310
                        return histogramB;
311
                return null;
312
        }
313
}