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