Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / pixelincrease / PixelncreaseDialog.java @ 12197

History | View | Annotate | Download (9.57 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.pixelincrease;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Color;
23
import java.awt.Graphics;
24
import java.awt.Graphics2D;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.awt.event.MouseEvent;
28
import java.awt.event.MouseListener;
29
import java.awt.geom.AffineTransform;
30
import java.awt.image.BufferedImage;
31

    
32
import javax.swing.JCheckBoxMenuItem;
33
import javax.swing.JMenuItem;
34
import javax.swing.JPanel;
35
import javax.swing.JPopupMenu;
36
import javax.swing.event.PopupMenuEvent;
37
import javax.swing.event.PopupMenuListener;
38

    
39
import com.iver.andami.PluginServices;
40
import com.iver.andami.ui.mdiManager.IWindow;
41
import com.iver.andami.ui.mdiManager.WindowInfo;
42
import com.iver.cit.gvsig.fmap.tools.CompoundBehavior;
43
import com.iver.cit.gvsig.project.documents.view.gui.IView;
44

    
45

    
46
/**
47
 * Panel del zoom de la vista sobre el cursor.
48
 * 
49
 * @author Nacho Brodin (nachobrodin@gmail.com)
50
 */
51
public class PixelncreaseDialog extends JPanel implements IWindow, MouseListener {
52
    final private static long                         serialVersionUID = -3370601314380922368L;
53
    private JPopupMenu                                        menu = null;
54
    /**
55
     * Ancho y alto de la ventana
56
     */
57
    private int                                                width = 170;
58
    private int                                                height = 170;
59
    /**
60
     * Posici?n de la ventana en X y en Y
61
     */
62
    private int                                                posWindowX = 0;
63
    private int                                                posWindowY = 0;
64
    /**
65
     * Escala del zoom
66
     */
67
    private int                                                scale = 8;
68
    /**
69
     * Vista asociada al inspector de pixels
70
     */
71
    public IView                                                view = null;
72
    /**
73
     * Posici?n en X e Y donde se comienza a dibujar dentro del inspector de pixeles
74
     */
75
    private int                                                posX = 0;
76
    private int                                                posY = 0;
77
    /**
78
     * Posici?n del pixel en X e Y en relaci?n a las coordenadas del buffer de la vista
79
     */
80
    public int                                                        pixX = 0;
81
    public int                                                        pixY = 0;
82
    /**
83
     * Valores RGB del pixel seleccionado
84
     */    
85
    int                                                                 red = 0, green = 0, blue = 0;
86
    private WindowInfo                                 m_viewinfo = null;
87
    public boolean                                        clear = false;
88
    private Color                                        color = Color.red;
89
    private JCheckBoxMenuItem[]         entry = new JCheckBoxMenuItem[6];
90
    
91
        /**
92
         * Constructor de la ventana de dialogo para gvSIG.
93
         */
94
        public PixelncreaseDialog() {
95
                BorderLayout layout = new BorderLayout();
96
                setLayout(layout);
97
                setSize(width, height);
98
                addMouseListener(this);
99
                
100
                IWindow active = PluginServices.getMDIManager().getActiveWindow();
101
                if(active instanceof IView) {
102
                        view = (IView)active;
103
                        WindowInfo wInfo = PluginServices.getMDIManager().getWindowInfo(active);
104
                        posWindowX = wInfo.getX() + wInfo.getWidth() - width;
105
                        posWindowY = wInfo.getY();
106
                }        
107
                
108
                CompoundBehavior.setAllControlsBehavior(new PixelIncreaseBehavior(this));
109
                initMenu();
110
                
111
        }
112
        
113
        /**
114
         * Inicializa el men? contextual con las opciones de selecci?n del 
115
         * zoom.
116
         */
117
        private void initMenu() {
118
                menu = new JPopupMenu();
119
                PopupMenuListener lis = new PopupMenuListener() {
120
                        public void popupMenuCanceled( PopupMenuEvent evt ) {
121
                                clear = true;
122
                                repaint();
123
                        }
124

    
125
                        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
126
                        }
127

    
128
                        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
129
                        }
130
                };
131
                menu.addPopupMenuListener(lis);
132
                                
133
                ActionListener al = new ActionListener() {
134
                        public void actionPerformed( ActionEvent evt ){
135
                                String txt = ((JMenuItem)evt.getSource()).getText();
136
                                if(txt.compareTo("X4") == 0) {
137
                                        scale = 4;
138
                                        for (int i = 1; i <= 3; i++) 
139
                                                entry[i].setSelected(false);        
140
                                        entry[0].setSelected(true);
141
                                }
142
                                if(txt.compareTo("X8") == 0) {
143
                                        scale = 8;
144
                                        entry[0].setSelected(false);
145
                                        entry[1].setSelected(true);
146
                                        entry[2].setSelected(false);
147
                                        entry[3].setSelected(false);
148
                                }
149
                                if(txt.compareTo("X16") == 0) {
150
                                        scale = 16;
151
                                        entry[0].setSelected(false);
152
                                        entry[1].setSelected(false);
153
                                        entry[2].setSelected(true);
154
                                        entry[3].setSelected(false);
155
                                }
156
                                if(txt.compareTo("X32") == 0) {
157
                                        scale = 32;
158
                                        for (int i = 0; i < 3; i++) 
159
                                                entry[i].setSelected(false);        
160
                                        entry[3].setSelected(true);
161
                                }
162
                                if(txt.compareTo(PluginServices.getText(this, "green")) == 0) {
163
                                        color = Color.GREEN;
164
                                        entry[4].setSelected(false);
165
                                        entry[5].setSelected(true);
166
                                }
167
                                if(txt.compareTo(PluginServices.getText(this, "red")) == 0) {
168
                                        color = Color.RED;
169
                                        entry[4].setSelected(true);
170
                                        entry[5].setSelected(false);
171
                                }
172
                        }
173
                };
174
                
175
                entry[0] = new JCheckBoxMenuItem( "X4" );
176
                entry[0].addActionListener( al );
177
            menu.add(entry[0]);
178
            entry[1] = new JCheckBoxMenuItem( "X8" );
179
            entry[1].setSelected(true);
180
            entry[1].addActionListener( al );
181
            menu.add(entry[1]);
182
            entry[2] = new JCheckBoxMenuItem( "X16" );
183
            entry[2].addActionListener( al );
184
            menu.add(entry[2]);
185
            entry[3] = new JCheckBoxMenuItem( "X32" );
186
            entry[3].addActionListener( al );
187
            menu.add(entry[3]);
188
            entry[4] = new JCheckBoxMenuItem( PluginServices.getText(this, "red") );
189
            entry[4].addActionListener( al );
190
            entry[4].setSelected(true);
191
            menu.add(entry[4]);
192
            entry[5] = new JCheckBoxMenuItem( PluginServices.getText(this, "green") );
193
            entry[5].addActionListener( al );
194
            menu.add(entry[5]);
195
        }
196
        
197
        /**
198
         * Obtiene el buffer de la vista activa y lo dibuja sobre el panel
199
         * con los datos de escala y desplazamiento seleccionados.
200
         */
201
        protected void paintComponent(Graphics g) {
202
                if(clear) {
203
                        g.setColor(Color.WHITE);
204
                        g.fillRect(0, 0, width, height);
205
                        return;
206
                }
207
                
208
                if(view != null) {
209
                        int dto = 30; //Desplazamiento del zoom en vertical
210
                        int sizeCrux = 10;
211
                        
212
                        //Obtenemos valores RGB del Image
213
                        BufferedImage img = view.getMapControl().getImage();
214
                        int value = 0;
215
                        try {
216
                                value = img.getRGB(pixX, pixY);
217
                        } catch (ArrayIndexOutOfBoundsException e) {
218
                                
219
                        }
220
                        red = ((value & 0x00ff0000) >> 16);
221
                        green = ((value & 0x0000ff00) >> 8);
222
                        blue = (value & 0x000000ff);
223
                        
224
                        //Dibujamos el graphics con el zoom
225
                        g.setColor(Color.BLACK);
226
                        g.fillRect(0, 0, width, height);
227
                        ((Graphics2D)g).scale(scale, scale);
228
                        g.drawImage(img, posX, posY - (dto / scale), this);
229
                        ((Graphics2D)g).setTransform(new AffineTransform());
230
                        
231
                        //Dibujamos la informaci?n RGB y la cruz
232
                        //g.setXORMode(Color.WHITE);
233
                        g.setColor(color);
234
                        int middleW = width >> 1;
235
                        int middleH = ((height - dto) >> 1);
236
                        g.drawLine(middleW - sizeCrux, middleH, middleW + sizeCrux, middleH);
237
                        g.drawLine(middleW, middleH - sizeCrux, middleW , middleH + sizeCrux);
238
                        g.drawString(red + "," + green + "," + blue, width - 85, height - 30);
239
                }
240
        }
241

    
242
        /**
243
         * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
244
         */
245
        public WindowInfo getWindowInfo() {
246
                m_viewinfo = new WindowInfo(WindowInfo.MODELESSDIALOG);
247
            m_viewinfo.setTitle(PluginServices.getText(this, "increase"));
248
            m_viewinfo.setX(posWindowX);
249
            m_viewinfo.setY(posWindowY);
250
                return m_viewinfo;
251
        }
252

    
253
        /**
254
         * Asigna el zoom de la vista sobre el inspector de pixels
255
         * @param scale Escala
256
         */
257
        public void setScale(int scale) {
258
                this.scale = scale;
259
        }
260

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

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

    
277
        /**
278
         * Obtiene el alto del control
279
         */
280
        public int getHeight() {
281
                return height;
282
        }
283

    
284
        /**
285
         * Obtiene el ancho del control
286
         */
287
        public int getWidth() {
288
                return width;
289
        }
290

    
291
        /**
292
         * Obtiene el factor de escala
293
         * @return 
294
         */
295
        public int getScale() {
296
                return scale;
297
        }
298

    
299
        /**
300
         * Obtiene la vista asociada al inspector de pixeles
301
         * @return IView
302
         */
303
        public IView getView() {
304
                return view;
305
        }
306

    
307
        /**
308
         * Asigna la vista asociada al inspector de pixeles
309
         * @param IView
310
         */
311
        public void setView(IView view) {
312
                this.view = view;
313
        }
314

    
315
        /*
316
         * (non-Javadoc)
317
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
318
         */
319
        public void mouseClicked(MouseEvent e) {
320
        }
321

    
322
        /*
323
         * (non-Javadoc)
324
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
325
         */
326
        public void mouseEntered(MouseEvent e) {
327
        }
328

    
329
        /*
330
         * (non-Javadoc)
331
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
332
         */
333
        public void mouseExited(MouseEvent e) {
334
        }
335

    
336
        /*
337
         * (non-Javadoc)
338
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
339
         */
340
        public void mousePressed(MouseEvent e) {
341
                if(e.getButton() == MouseEvent.BUTTON3) {
342
                        menu.show( e.getComponent(), e.getX(), e.getY() );
343
                        clear = true;
344
                        repaint();
345
                }
346
        }
347

    
348
        /*
349
         * (non-Javadoc)
350
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
351
         */
352
        public void mouseReleased(MouseEvent e) {
353
        }
354

    
355
}