Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / tool / infobypoint / PixelInspectorPanel.java @ 2443

History | View | Annotate | Download (8.79 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22
package org.gvsig.raster.swing.impl.tool.infobypoint;
23

    
24
import java.awt.Color;
25
import java.awt.Graphics;
26
import java.awt.Graphics2D;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.awt.event.MouseEvent;
30
import java.awt.event.MouseListener;
31
import java.awt.geom.AffineTransform;
32
import java.awt.image.BufferedImage;
33
import java.util.HashMap;
34

    
35
import javax.swing.JCheckBoxMenuItem;
36
import javax.swing.JMenuItem;
37
import javax.swing.JPanel;
38
import javax.swing.JPopupMenu;
39
import javax.swing.event.PopupMenuEvent;
40
import javax.swing.event.PopupMenuListener;
41

    
42
import org.gvsig.raster.swing.pixelinspector.PixelInspector;
43

    
44

    
45

    
46
/**
47
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public class PixelInspectorPanel extends JPanel implements MouseListener, PixelInspector {
50
        final private static long      serialVersionUID = -3370601314380922368L;
51
        private JPopupMenu             menu = null;
52
        /**
53
         * Ancho y alto de la ventana
54
         */
55
        private int                    w = 0;
56
        private int                    h = 0;
57
        /**
58
         * Posici?n de la ventana en X y en Y
59
         */
60
        //private int                    posWindowX = 0;
61
        //private int                    posWindowY = 0;
62
        /**
63
         * Escala del zoom
64
         */
65
        private int                    scale = 8;
66
        /**
67
         * Vista asociada al inspector de pixels
68
         */
69
        public BufferedImage            img = null;
70
        /**
71
         * Posici?n en X e Y donde se comienza a dibujar dentro del inspector de pixeles
72
         */
73
        private int                                                posX = 0;
74
        private int                                                posY = 0;
75
        /**
76
         * Posici?n del pixel en X e Y en relaci?n a las coordenadas del buffer de la vista
77
         */
78
        public int                      pixX = 0;
79
        public int                      pixY = 0;
80
        /**
81
         * Valores RGB del pixel seleccionado
82
         */    
83
        int                             red = 0, green = 0, blue = 0;
84
        private boolean                                        clear = false;
85
        private Color                                        color = Color.red;
86
        private JCheckBoxMenuItem[]         entry = new JCheckBoxMenuItem[6];
87
        private HashMap<String, String>            
88
                                    tr                      = null;
89
        /**
90
         * Constructor de la ventana de dialogo para gvSIG.
91
         */
92
        public PixelInspectorPanel(HashMap<String, String> translations) {
93
                tr = translations;
94
                if(tr == null || tr.get("layer_list") == null) {
95
                        tr = new HashMap<String, String>();
96
                        tr.put("green", "green");
97
                        tr.put("red", "red");
98
                        tr.put("blue", "blue");
99
                }
100
                
101
                addMouseListener(this);
102

    
103
                initMenu();
104
        }
105

    
106
        public void setViewCoordinates(int pixX, int pixY) {
107
                this.pixX = pixX;
108
                this.pixY = pixY;
109
        }
110

    
111
        /**
112
         * 
113
         * @param clear
114
         */
115
        public void setClear(boolean clear) {
116
                this.clear = clear;
117
        }
118

    
119
        /**
120
         * Inicializa el men? contextual con las opciones de selecci?n del 
121
         * zoom.
122
         */
123
        private void initMenu() {
124
                menu = new JPopupMenu();
125
                PopupMenuListener lis = new PopupMenuListener() {
126
                        public void popupMenuCanceled( PopupMenuEvent evt ) {
127
                                clear = true;
128
                                repaint();
129
                        }
130

    
131
                        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
132
                        }
133

    
134
                        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
135
                        }
136
                };
137
                menu.addPopupMenuListener(lis);
138

    
139
                ActionListener al = new ActionListener() {
140
                        public void actionPerformed( ActionEvent evt ){
141
                                String txt = ((JMenuItem)evt.getSource()).getText();
142
                                if(txt.compareTo("X4") == 0) {
143
                                        scale = 4;
144
                                        for (int i = 1; i <= 3; i++) 
145
                                                entry[i].setSelected(false);        
146
                                        entry[0].setSelected(true);
147
                                }
148
                                if(txt.compareTo("X8") == 0) {
149
                                        scale = 8;
150
                                        entry[0].setSelected(false);
151
                                        entry[1].setSelected(true);
152
                                        entry[2].setSelected(false);
153
                                        entry[3].setSelected(false);
154
                                }
155
                                if(txt.compareTo("X16") == 0) {
156
                                        scale = 16;
157
                                        entry[0].setSelected(false);
158
                                        entry[1].setSelected(false);
159
                                        entry[2].setSelected(true);
160
                                        entry[3].setSelected(false);
161
                                }
162
                                if(txt.compareTo("X32") == 0) {
163
                                        scale = 32;
164
                                        for (int i = 0; i < 3; i++) 
165
                                                entry[i].setSelected(false);        
166
                                        entry[3].setSelected(true);
167
                                }
168
                                if(txt.compareTo(tr.get("green")) == 0) {
169
                                        color = Color.GREEN;
170
                                        entry[4].setSelected(false);
171
                                        entry[5].setSelected(true);
172
                                }
173
                                if(txt.compareTo(tr.get("red")) == 0) {
174
                                        color = Color.RED;
175
                                        entry[4].setSelected(true);
176
                                        entry[5].setSelected(false);
177
                                }
178
                        }
179
                };
180

    
181
                entry[0] = new JCheckBoxMenuItem( "X4" );
182
                entry[0].addActionListener( al );
183
                menu.add(entry[0]);
184
                entry[1] = new JCheckBoxMenuItem( "X8" );
185
                entry[1].setSelected(true);
186
                entry[1].addActionListener( al );
187
                menu.add(entry[1]);
188
                entry[2] = new JCheckBoxMenuItem( "X16" );
189
                entry[2].addActionListener( al );
190
                menu.add(entry[2]);
191
                entry[3] = new JCheckBoxMenuItem( "X32" );
192
                entry[3].addActionListener( al );
193
                menu.add(entry[3]);
194
                entry[4] = new JCheckBoxMenuItem(tr.get("red") );
195
                entry[4].addActionListener( al );
196
                entry[4].setSelected(true);
197
                menu.add(entry[4]);
198
                entry[5] = new JCheckBoxMenuItem(tr.get("green") );
199
                entry[5].addActionListener( al );
200
                menu.add(entry[5]);
201
        }
202

    
203
        /**
204
         * Obtiene el buffer de la vista activa y lo dibuja sobre el panel
205
         * con los datos de escala y desplazamiento seleccionados.
206
         */
207
        protected void paintComponent(Graphics g) {
208
                w = getVisibleRect().width;
209
                h = getVisibleRect().height;
210

    
211
                if(clear) {
212
                        g.setColor(Color.BLACK);
213
                        g.fillRect(0, 0, w, h);
214
                        return;
215
                }
216

    
217
                if(img != null) {
218
                        int sizeCrux = 10;
219
                        
220
                        //Obtenemos valores RGB del Image
221
                        int value = 0;
222
                        try {
223
                                value = img.getRGB(pixX, pixY);
224
                        } catch (ArrayIndexOutOfBoundsException e) {
225

    
226
                        }
227
                        red = ((value & 0x00ff0000) >> 16);
228
                        green = ((value & 0x0000ff00) >> 8);
229
                        blue = (value & 0x000000ff);
230

    
231
                        //Dibujamos el graphics con el zoom
232
                        g.setColor(Color.BLACK);
233
                        g.fillRect(0, 0, w, h);
234
                        ((Graphics2D)g).scale(scale, scale);
235
                        g.drawImage(img, posX, posY , this);
236
                        ((Graphics2D)g).setTransform(new AffineTransform());
237

    
238
                        //Dibujamos la informaci?n RGB y la cruz
239
                        g.setXORMode(Color.WHITE);
240
                        g.setColor(color);
241
                        int middleW = w >> 1;
242
                        int middleH = h >> 1;
243
                        g.drawLine(middleW - sizeCrux, middleH, middleW + sizeCrux, middleH);
244
                        g.drawLine(middleW, middleH - sizeCrux, middleW , middleH + sizeCrux);
245
                        //g.drawString(red + "," + green + "," + blue, w - 85, h - 3);
246
                }
247
        }
248

    
249
        /**
250
         * Asigna el zoom de la vista sobre el inspector de pixels
251
         * @param scale Escala
252
         */
253
        public void setScale(int scale) {
254
                this.scale = scale;
255
        }
256

    
257
        /**
258
         * Asigna la posici?n en X del control donde se empieza a dibujar
259
         * @param posX posici?n X del Graphics donde se empieza a dibujar
260
         */
261
        public void setPosX(int posX) {
262
                this.posX = posX;
263
        }
264

    
265
        /**
266
         * Asigna la posici?n en Y del control donde se empieza a dibujar
267
         * @param posY posici?n Y del Graphics donde se empieza a dibujar
268
         */
269
        public void setPosY(int posY) {
270
                this.posY = posY;
271
        }
272

    
273
        /**
274
         * Obtiene el factor de escala
275
         * @return 
276
         */
277
        public int getScale() {
278
                return scale;
279
        }
280

    
281
        /**
282
         * Obtiene la vista asociada al inspector de pixeles
283
         * @return IView
284
         */
285
        public BufferedImage getDataBuffer() {
286
                return img;
287
        }
288

    
289
        /**
290
         * Asigna la vista asociada al inspector de pixeles
291
         * @param IView
292
         */
293
        public void setDataBuffer(BufferedImage img) {
294
                this.img = img;
295
        }
296

    
297
        /*
298
         * (non-Javadoc)
299
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
300
         */
301
        public void mouseClicked(MouseEvent e) {
302
                clear = true;
303
                repaint();
304
        }
305

    
306
        /*
307
         * (non-Javadoc)
308
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
309
         */
310
        public void mouseEntered(MouseEvent e) {
311
                clear = true;
312
                repaint();
313
        }
314

    
315
        /*
316
         * (non-Javadoc)
317
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
318
         */
319
        public void mouseExited(MouseEvent e) {
320
                clear = true;
321
                repaint();
322
        }
323

    
324
        /*
325
         * (non-Javadoc)
326
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
327
         */
328
        public void mousePressed(MouseEvent e) {
329
                if(e.getButton() == MouseEvent.BUTTON3) {
330
                        menu.show( e.getComponent(), e.getX(), e.getY() );
331
                        clear = true;
332
                        repaint();
333
                }
334
        }
335

    
336
        /*
337
         * (non-Javadoc)
338
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
339
         */
340
        public void mouseReleased(MouseEvent e) {
341
        }
342

    
343
}