root / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / gui / Panels / ZoomControlPanel.java @ 3039
History | View | Annotate | Download (14.4 KB)
1 |
package com.iver.cit.gvsig.gui.Panels; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Canvas; |
5 |
import java.awt.Color; |
6 |
import java.awt.Graphics; |
7 |
import java.awt.Graphics2D; |
8 |
import java.awt.GridBagConstraints; |
9 |
import java.awt.GridBagLayout; |
10 |
import java.awt.Image; |
11 |
import java.awt.event.ActionEvent; |
12 |
import java.awt.event.ActionListener; |
13 |
import java.awt.event.MouseEvent; |
14 |
import java.awt.event.MouseListener; |
15 |
import java.awt.event.MouseMotionListener; |
16 |
import java.awt.geom.Point2D; |
17 |
import java.awt.geom.Rectangle2D; |
18 |
import java.awt.image.BufferedImage; |
19 |
|
20 |
import javax.swing.ImageIcon; |
21 |
import javax.swing.JButton; |
22 |
import javax.swing.JPanel; |
23 |
|
24 |
import com.iver.andami.PluginServices; |
25 |
import com.iver.cit.gvsig.fmap.DriverException; |
26 |
import com.iver.cit.gvsig.fmap.ViewPort; |
27 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
28 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
29 |
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster; |
30 |
import com.iver.cit.gvsig.fmap.layers.FLyrPoints; |
31 |
import com.iver.cit.gvsig.fmap.operations.Cancellable; |
32 |
import com.iver.cit.gvsig.gui.View; |
33 |
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog; |
34 |
|
35 |
/**
|
36 |
* Panel que contiene los controles de zoom de la vista para
|
37 |
* afinar en la georreferenciaci?n.
|
38 |
*
|
39 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
40 |
*
|
41 |
*/
|
42 |
public class ZoomControlPanel extends JPanel implements ActionListener{ |
43 |
private JPanel pImage = null; |
44 |
private JPanel pControls = null; |
45 |
private JButton bZoomMas = null; |
46 |
private JButton bZoomMenos = null; |
47 |
|
48 |
private int sX = 140, sY = 140; |
49 |
private CanvasZone canvas = null; |
50 |
private ViewPort viewPort = null; |
51 |
|
52 |
private int numberPoint = -1; |
53 |
|
54 |
public FLyrPoints lyrPoints = null; |
55 |
public FLyrGeoRaster lyrGeoRaster = null; |
56 |
public GeoreferencingDialog dialog = null; |
57 |
|
58 |
|
59 |
|
60 |
/**
|
61 |
* This is the default constructor
|
62 |
*/
|
63 |
public ZoomControlPanel(boolean rightControl) { |
64 |
super();
|
65 |
initialize(rightControl); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* This method initializes this
|
70 |
*
|
71 |
* @return void
|
72 |
*/
|
73 |
private void initialize(boolean rightControl) { |
74 |
|
75 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
76 |
if(rightControl)
|
77 |
gridBagConstraints1.gridx = 1;
|
78 |
else
|
79 |
gridBagConstraints1.gridx = 0;
|
80 |
gridBagConstraints1.gridy = 0;
|
81 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
82 |
if(rightControl)
|
83 |
gridBagConstraints.gridx = 0;
|
84 |
else
|
85 |
gridBagConstraints.gridx = 1;
|
86 |
gridBagConstraints.gridy = 0;
|
87 |
this.setLayout(new GridBagLayout()); |
88 |
this.setPreferredSize(new java.awt.Dimension(175,142)); |
89 |
this.setSize(new java.awt.Dimension(175,142)); |
90 |
this.setLocation(new java.awt.Point(0,0)); |
91 |
this.add(getPImage(), gridBagConstraints);
|
92 |
this.add(getPControls(), gridBagConstraints1);
|
93 |
} |
94 |
|
95 |
/**
|
96 |
* Carga las capas de puntos y de raster georreferenciado
|
97 |
*
|
98 |
*/
|
99 |
public void loadLayer(){ |
100 |
//Cargamos la capa
|
101 |
View theView = (View) PluginServices.getMDIManager().getActiveView(); |
102 |
for(int i=0;i<theView.getMapControl().getMapContext().getLayers().getLayersCount();i++){ |
103 |
FLayer lyr = theView.getMapControl().getMapContext().getLayers().getLayer(i); |
104 |
if(lyr instanceof FLyrPoints) |
105 |
lyrPoints = (FLyrPoints)lyr; |
106 |
if( lyr instanceof FLyrGeoRaster && |
107 |
lyr.getName().startsWith("*") &&
|
108 |
lyr.isActive()) |
109 |
lyrGeoRaster = (FLyrGeoRaster)lyr; |
110 |
} |
111 |
dialog = lyrGeoRaster.getGeoDialog(); |
112 |
} |
113 |
|
114 |
/**
|
115 |
* This method initializes jPanel
|
116 |
*
|
117 |
* @return javax.swing.JPanel
|
118 |
*/
|
119 |
private JPanel getPImage() { |
120 |
if (pImage == null) { |
121 |
pImage = new JPanel(); |
122 |
BorderLayout borderLayout = new BorderLayout(); |
123 |
pImage.setLayout(borderLayout); |
124 |
pImage.setPreferredSize(new java.awt.Dimension(140,140)); |
125 |
pImage.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
126 |
if(canvas == null){ |
127 |
canvas = new CanvasZone(sX, sY, this); |
128 |
canvas.setVisible(false);
|
129 |
canvas.addMouseListener(canvas); |
130 |
canvas.addMouseMotionListener(canvas); |
131 |
} |
132 |
pImage.add(canvas, BorderLayout.CENTER);
|
133 |
|
134 |
} |
135 |
return pImage;
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* This method initializes jPanel1
|
140 |
*
|
141 |
* @return javax.swing.JPanel
|
142 |
*/
|
143 |
private JPanel getPControls() { |
144 |
if (pControls == null) { |
145 |
GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
146 |
gridBagConstraints3.gridx = 0;
|
147 |
gridBagConstraints3.insets = new java.awt.Insets(5,0,0,0); |
148 |
gridBagConstraints3.gridy = 1;
|
149 |
GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
150 |
gridBagConstraints2.gridx = 0;
|
151 |
gridBagConstraints2.gridy = 0;
|
152 |
pControls = new JPanel(); |
153 |
pControls.setLayout(new GridBagLayout()); |
154 |
pControls.setPreferredSize(new java.awt.Dimension(33,140)); |
155 |
pControls.add(getBZoomMas(), gridBagConstraints2); |
156 |
pControls.add(getBZoomMenos(), gridBagConstraints3); |
157 |
} |
158 |
return pControls;
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* This method initializes jButton
|
163 |
*
|
164 |
* @return javax.swing.JButton
|
165 |
*/
|
166 |
private JButton getBZoomMas() { |
167 |
if (bZoomMas == null) { |
168 |
bZoomMas = new JButton(); |
169 |
bZoomMas.setPreferredSize(new java.awt.Dimension(25,25)); |
170 |
bZoomMas.setIcon(new ImageIcon(getClass().getResource("/com/iver/cit/gvsig/gui/Panels/images/aumentar.png"))); |
171 |
bZoomMas.addActionListener(this);
|
172 |
} |
173 |
return bZoomMas;
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* This method initializes jButton
|
178 |
*
|
179 |
* @return javax.swing.JButton
|
180 |
*/
|
181 |
private JButton getBZoomMenos() { |
182 |
if (bZoomMenos == null) { |
183 |
bZoomMenos = new JButton(); |
184 |
bZoomMenos.setPreferredSize(new java.awt.Dimension(25,25)); |
185 |
bZoomMenos.setIcon(new ImageIcon(getClass().getResource("/com/iver/cit/gvsig/gui/Panels/images/disminuir.png"))); |
186 |
bZoomMenos.addActionListener(this);
|
187 |
} |
188 |
return bZoomMenos;
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* @param viewPort The viewPort to set.
|
193 |
*/
|
194 |
public void setViewPort(ViewPort viewPort) { |
195 |
this.viewPort = viewPort;
|
196 |
} |
197 |
|
198 |
/**
|
199 |
*Llama a la funci?n de pintado del canvas con los par?metros que tenga en ese momento
|
200 |
*/
|
201 |
public void draw(){ |
202 |
if(canvas != null){ |
203 |
canvas.setViewPort(this.viewPort);
|
204 |
canvas.repaint();//.update();
|
205 |
} |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Asigna al canvas la propiedad de visibilidad a verdadero o falso.
|
210 |
* @param visible true para mostrar el canvas y false para ocultarlo
|
211 |
*/
|
212 |
public void setVisible(boolean visible){ |
213 |
canvas.setVisible(visible); |
214 |
if(!visible)
|
215 |
canvas.setSize(0,0); |
216 |
else{
|
217 |
canvas.setSize(sX, sY); |
218 |
} |
219 |
canvas.repaint(); |
220 |
} |
221 |
|
222 |
/**
|
223 |
* Asigna el punto sobre el que se centrar? el zoom de la minimagen
|
224 |
* @param pto
|
225 |
*/
|
226 |
public void setCenterPoint(Point2D pto){ |
227 |
canvas.setCenterPoint(pto); |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Asigna el n?mero de punto que se est? tratando
|
232 |
* @param n
|
233 |
*/
|
234 |
public void setNumberPoint(int n){ |
235 |
this.numberPoint = n;
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* Recupera el n?mero de punto que se est? tratando
|
240 |
* @param n
|
241 |
*/
|
242 |
public int getNumberPoint(){ |
243 |
return this.numberPoint; |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Manejo de los controles zoom m?s y zoom menos
|
248 |
*/
|
249 |
public void actionPerformed(ActionEvent e) { |
250 |
if(e.getSource() == bZoomMas){
|
251 |
canvas.calcZoom(0.6);
|
252 |
canvas.repaint();//.update();
|
253 |
} |
254 |
|
255 |
if(e.getSource() == bZoomMenos){
|
256 |
canvas.calcZoom(1.8);
|
257 |
canvas.repaint();//.update();
|
258 |
} |
259 |
|
260 |
} |
261 |
|
262 |
|
263 |
|
264 |
/**
|
265 |
* Control que representa la zona de dibujado de la ventana. Este se encarga de
|
266 |
* repintar el contenido sobre el Graphics2D a partir del viewPort que se le pase
|
267 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
268 |
*/
|
269 |
class CanvasZone extends Canvas implements MouseListener, MouseMotionListener{ |
270 |
private ZoomControlPanel zoomControl = null; |
271 |
private int ancho; |
272 |
private int alto; |
273 |
private FLayers flayers = null; |
274 |
private ViewPort viewPort = null; |
275 |
private ViewPort newViewPort = null; |
276 |
private Point2D centerPoint = null; |
277 |
private int zoom = 20; |
278 |
private boolean dragged = false; |
279 |
private Image dobleBuffer = null; |
280 |
private BufferedImage imgTmp = null; |
281 |
private Graphics dbGraphics = null; |
282 |
|
283 |
/**
|
284 |
* Puntos inicial y final para el desplazamiento del punto de control
|
285 |
*/
|
286 |
private Point2D ptoIni = null, ptoFin = null; |
287 |
private Point2D movDragged = null, initCrux = null; |
288 |
|
289 |
|
290 |
public CanvasZone( int anc,int alt, ZoomControlPanel zc) { |
291 |
zoomControl = zc; |
292 |
ancho = anc; |
293 |
alto = alt; |
294 |
|
295 |
this.setSize(ancho, alto);
|
296 |
this.setBackground(Color.WHITE); |
297 |
|
298 |
imgTmp = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB); |
299 |
dobleBuffer = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB); |
300 |
dbGraphics = dobleBuffer.getGraphics(); |
301 |
} |
302 |
|
303 |
/**
|
304 |
* Asigna el viewPort
|
305 |
* @param vp ViewPort
|
306 |
*/
|
307 |
public void setViewPort(ViewPort vp){ |
308 |
this.viewPort = vp;
|
309 |
this.initViewPort();
|
310 |
} |
311 |
|
312 |
/**
|
313 |
* Calcula el zoom dependiendo del factor de escala pasado por
|
314 |
* par?metro.
|
315 |
*/
|
316 |
public void calcZoom(double factor){ |
317 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
318 |
double nuevoX = centerPoint.getX() -
|
319 |
((newViewPort.getExtent().getWidth() * factor) / 2.0);
|
320 |
double nuevoY = centerPoint.getY() -
|
321 |
((newViewPort.getExtent().getHeight() * factor) / 2.0);
|
322 |
r.x = nuevoX; |
323 |
r.y = nuevoY; |
324 |
r.width = newViewPort.getExtent().getWidth() * factor; |
325 |
r.height = newViewPort.getExtent().getHeight() * factor; |
326 |
newViewPort.setExtent(r); |
327 |
} |
328 |
|
329 |
/**
|
330 |
*Calculamos el viewPort para el zoom de la minimagen
|
331 |
*/
|
332 |
public void initViewPort(){ |
333 |
if(centerPoint != null){ |
334 |
newViewPort = this.viewPort.cloneViewPort();
|
335 |
|
336 |
//Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
|
337 |
double relacionPixelWcWidth = ((viewPort.getExtent().getMaxX() - viewPort.getExtent().getMinX()) / zoom)/this.getWidth(); |
338 |
double relacionPixelWcHeight = ((viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY()) / zoom)/this.getHeight(); |
339 |
|
340 |
double wcOriginX = centerPoint.getX() - ((viewPort.getImageWidth()*relacionPixelWcWidth)/2); |
341 |
double wcOriginY = centerPoint.getY() - ((viewPort.getImageHeight()*relacionPixelWcHeight)/2); |
342 |
|
343 |
double wcDstMinX = wcOriginX;
|
344 |
double wcDstMinY = wcOriginY;
|
345 |
double wcDstMaxX = wcDstMinX + (viewPort.getImageWidth()*relacionPixelWcWidth);
|
346 |
double wcDstMaxY = wcDstMinY + (viewPort.getImageHeight()*relacionPixelWcHeight);
|
347 |
|
348 |
double wcDstWidth = wcDstMaxX - wcDstMinX;
|
349 |
double wcDstHeight = wcDstMaxY - wcDstMinY;
|
350 |
|
351 |
Rectangle2D ext = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight); |
352 |
|
353 |
newViewPort.setExtent(ext); |
354 |
newViewPort.setImageSize(new java.awt.Dimension(this.getWidth(), this.getHeight())); |
355 |
newViewPort.setScale(); |
356 |
} |
357 |
} |
358 |
|
359 |
public void loadBuffer(){ |
360 |
|
361 |
Graphics2D gTmp = (Graphics2D)imgTmp.getGraphics(); |
362 |
|
363 |
if(!dragged){
|
364 |
dbGraphics.setColor( Color.WHITE );
|
365 |
dbGraphics.fillRect( 0,0,ancho,alto ); |
366 |
|
367 |
try{
|
368 |
if(newViewPort != null){ |
369 |
//Si no tenemos las capas las cargamos
|
370 |
if(flayers == null){ |
371 |
View theView = (View) PluginServices.getMDIManager().getActiveView(); |
372 |
flayers = theView.getMapControl().getMapContext().getLayers(); |
373 |
} |
374 |
//dibujamos
|
375 |
flayers.draw(null, (Graphics2D) dbGraphics, newViewPort,new Cancellable() { |
376 |
public boolean isCanceled() { |
377 |
return false; |
378 |
} |
379 |
},flayers.getFMap().getScaleView()); |
380 |
flayers.draw(null, gTmp, newViewPort,new Cancellable() { |
381 |
public boolean isCanceled() { |
382 |
return false; |
383 |
} |
384 |
},flayers.getFMap().getScaleView()); |
385 |
} |
386 |
}catch (DriverException e) {
|
387 |
e.printStackTrace(); |
388 |
} |
389 |
}else{
|
390 |
double x, y;
|
391 |
if(movDragged != null){ |
392 |
x = (movDragged.getX() - ptoIni.getX()) + initCrux.getX(); |
393 |
y = (movDragged.getY() - ptoIni.getY()) + initCrux.getY(); |
394 |
}else{
|
395 |
x = ptoIni.getX() + initCrux.getX(); |
396 |
y = ptoIni.getY() + initCrux.getY(); |
397 |
} |
398 |
|
399 |
dbGraphics.drawImage(imgTmp, 0, 0, null); |
400 |
dbGraphics.setColor( Color.RED );
|
401 |
dbGraphics.drawLine((int)(x - 5), (int)y, (int)(x + 5), (int)y); |
402 |
dbGraphics.drawLine((int)x, (int)(y - 5), (int)x, (int)(y + 5)); |
403 |
} |
404 |
} |
405 |
|
406 |
|
407 |
public void update(Graphics g){ |
408 |
this.paint(g);
|
409 |
} |
410 |
|
411 |
/**
|
412 |
* Dibuja sobre el Graphics2D pasado el viewPort solicitado a
|
413 |
* la lista de capas.
|
414 |
* @param g
|
415 |
*/
|
416 |
public void paint( Graphics g ) { |
417 |
loadBuffer(); |
418 |
g.drawImage(dobleBuffer, 0, 0, null); |
419 |
} |
420 |
|
421 |
/**
|
422 |
* Asigna el punto donde se centrar? la vista de la minimagen
|
423 |
* @param pto Punto central.
|
424 |
*/
|
425 |
public void setCenterPoint(Point2D pto){ |
426 |
this.centerPoint = pto;
|
427 |
} |
428 |
|
429 |
/* (non-Javadoc)
|
430 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
431 |
*/
|
432 |
public void mouseClicked(MouseEvent ev) { |
433 |
|
434 |
} |
435 |
|
436 |
/* (non-Javadoc)
|
437 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
438 |
*/
|
439 |
public void mouseEntered(MouseEvent arg0) { |
440 |
// TODO Auto-generated method stub
|
441 |
|
442 |
} |
443 |
|
444 |
/* (non-Javadoc)
|
445 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
446 |
*/
|
447 |
public void mouseExited(MouseEvent arg0) { |
448 |
// TODO Auto-generated method stub
|
449 |
|
450 |
} |
451 |
|
452 |
/* (non-Javadoc)
|
453 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
454 |
*/
|
455 |
public void mousePressed(MouseEvent ev) { |
456 |
ptoIni = ev.getPoint(); |
457 |
zoomControl.loadLayer(); |
458 |
initCrux = new Point2D.Double(); |
459 |
initCrux.setLocation(this.getWidth() / 2, this.getHeight() / 2);//zoomControl.lyrPoints.getPoint(zoomControl.getNumberPoint() - 1).mapPoint; |
460 |
|
461 |
this.dragged = true; |
462 |
repaint();//update();
|
463 |
} |
464 |
|
465 |
/* (non-Javadoc)
|
466 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
467 |
*/
|
468 |
public void mouseReleased(MouseEvent ev) { |
469 |
View theView = (View) PluginServices.getMDIManager().getActiveView(); |
470 |
ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort(); |
471 |
ptoFin = viewPort.toMapPoint(ev.getPoint()); |
472 |
this.dragged = false; |
473 |
repaint();//update();
|
474 |
} |
475 |
|
476 |
/* (non-Javadoc)
|
477 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
478 |
*/
|
479 |
public void mouseDragged(MouseEvent ev) { |
480 |
this.movDragged = ev.getPoint();
|
481 |
repaint();//update();
|
482 |
|
483 |
} |
484 |
|
485 |
/* (non-Javadoc)
|
486 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
487 |
*/
|
488 |
public void mouseMoved(MouseEvent arg0) { |
489 |
// TODO Auto-generated method stub
|
490 |
|
491 |
} |
492 |
|
493 |
} |
494 |
|
495 |
} |