Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src-test-ui / org / gvsig / rastertools / enhanced / graphics / panels / EqualizationHistogram.java @ 19558

History | View | Annotate | Download (9.22 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.functions.BaseFunction;
39
import org.gvsig.raster.beans.canvas.layers.functions.StraightLine;
40

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

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

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

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

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

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

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