svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / imagenavigator / ImageNavigator.java @ 21183
History | View | Annotate | Download (20.5 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 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.gui.beans.imagenavigator; |
20 |
|
21 |
import java.awt.AlphaComposite; |
22 |
import java.awt.Color; |
23 |
import java.awt.Cursor; |
24 |
import java.awt.GradientPaint; |
25 |
import java.awt.Graphics; |
26 |
import java.awt.Graphics2D; |
27 |
import java.awt.Image; |
28 |
import java.awt.Point; |
29 |
import java.awt.RenderingHints; |
30 |
import java.awt.color.ColorSpace; |
31 |
import java.awt.event.KeyEvent; |
32 |
import java.awt.event.KeyListener; |
33 |
import java.awt.event.MouseEvent; |
34 |
import java.awt.event.MouseListener; |
35 |
import java.awt.event.MouseMotionListener; |
36 |
import java.awt.event.MouseWheelEvent; |
37 |
import java.awt.event.MouseWheelListener; |
38 |
import java.awt.image.BufferedImage; |
39 |
import java.awt.image.ColorConvertOp; |
40 |
|
41 |
import javax.swing.ImageIcon; |
42 |
import javax.swing.JComponent; |
43 |
|
44 |
import org.gvsig.gui.beans.Messages; |
45 |
/**
|
46 |
* <code>ImageNavigator</code> es un componente que representa un manejador
|
47 |
* de im?genes. En ?l se puede desplazar, hacer un zoom out o un zoom in a una
|
48 |
* imagen virtual. El componente no trata la imagen en si, solo lanza los
|
49 |
* eventos indicando la nueva posici?n y zoom de la imagen, luego es el usuario
|
50 |
* el que se encargar? de dibujar esa imagen en la posici?n correspondiente.
|
51 |
*
|
52 |
* El modo de uso es el siguiente:
|
53 |
* - Se puede desplazar una imagen con el bot?n izquierdo del rat?n.
|
54 |
* - Se puede hacer zoom in/out con las teclas +/- del teclado.
|
55 |
* - Se puede hacer zoom in/out con la rueda del rat?n teniendo en cuenta la
|
56 |
* posici?n del mismo.
|
57 |
* - Se puede resetear los valores con las teclas 'Espacio' o 0;
|
58 |
* - Las teclas 1, 2, 3, 4 y 5 equivalen a zoom 1, 2, 4, 8 y 16 respectivamente.
|
59 |
* - La tecla C sirve para centrar la imagen.
|
60 |
* - La tecla B sirve para mostrar u ocultar los cuadros del fondo. ?til para
|
61 |
* mostrar imagenes con transparencia.
|
62 |
* - La tecla H muestra la ayuda.
|
63 |
*
|
64 |
* @version 04/05/2007
|
65 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
66 |
*/
|
67 |
public class ImageNavigator extends JComponent implements KeyListener, MouseMotionListener, MouseListener, MouseWheelListener { |
68 |
private static final long serialVersionUID = 1164788214432359272L; |
69 |
private IClientImageNavigator iClient = null; |
70 |
|
71 |
private Image image = null; |
72 |
private Graphics2D widgetGraphics = null; |
73 |
private Image imageCache = null; |
74 |
private Graphics2D cacheGraphics = null; |
75 |
|
76 |
private double zoom = 1.0; |
77 |
private double x1 = 0.0; |
78 |
private double y1 = 0.0; |
79 |
private boolean yInverted = false; |
80 |
private boolean xInverted = false; |
81 |
private int width = 0; |
82 |
private int height = 0; |
83 |
private boolean showHelp = false; |
84 |
private boolean showBackground = false; |
85 |
private Color backgroundColor = new Color(224, 224, 224); |
86 |
ImageIcon imageIconClose;
|
87 |
ImageIcon imageIconHelp;
|
88 |
|
89 |
/**
|
90 |
* Crea un <code>ImageNavigator</code> especificandole quien pintara el
|
91 |
* componente
|
92 |
* @param iClient
|
93 |
*/
|
94 |
public ImageNavigator(IClientImageNavigator iClient) {
|
95 |
this.iClient = iClient;
|
96 |
|
97 |
this.setFocusable(true); |
98 |
this.addKeyListener(this); |
99 |
this.addMouseMotionListener(this); |
100 |
this.addMouseListener(this); |
101 |
this.addMouseWheelListener(this); |
102 |
} |
103 |
|
104 |
double initX1 = 0.0; |
105 |
double initY1 = 0.0; |
106 |
double initX2 = 100.0; |
107 |
double initY2 = 100.0; |
108 |
double initZoom = 1.0; |
109 |
boolean autoAdjusted = true; |
110 |
|
111 |
/**
|
112 |
* Actualiza las dimensiones para ajustar la imagen a los bordes especificados
|
113 |
* con setViewDimensions.
|
114 |
*/
|
115 |
private void updateDimensions() { |
116 |
double factor = this.getWidth() / (initX2 - initX1); |
117 |
if (factor > (this.getHeight() / (initY2 - initY1))) |
118 |
factor = this.getHeight() / (initY2 - initY1);
|
119 |
zoom = factor; |
120 |
imageCenter(); |
121 |
} |
122 |
|
123 |
/**
|
124 |
* Centra la imagen
|
125 |
*/
|
126 |
public void imageCenter() { |
127 |
x1 = initX1; |
128 |
y1 = initY1; |
129 |
|
130 |
if (isXInverted())
|
131 |
x1 -= ((initX2 - initX1) - this.getWidth() / zoom) / 2.0; |
132 |
else
|
133 |
x1 += ((initX2 - initX1) - this.getWidth() / zoom) / 2.0; |
134 |
if (isYInverted())
|
135 |
y1 -= ((initY2 - initY1) - this.getHeight() / zoom) / 2.0; |
136 |
else
|
137 |
y1 += ((initY2 - initY1) - this.getHeight() / zoom) / 2.0; |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Especifica el rectangulo de la imagen a visualizar, pudiendo tener
|
142 |
* cualquiera de los ejes X e Y invertidos
|
143 |
*
|
144 |
* @param x1 Coordenada izquierda
|
145 |
* @param y1 Coordenada superior
|
146 |
* @param x2 Coordenada derecha
|
147 |
* @param y2 Coordenada inferior
|
148 |
*/
|
149 |
public void setViewDimensions(double x1, double y1, double x2, double y2) { |
150 |
this.initX1 = x1;
|
151 |
this.initX2 = x2;
|
152 |
this.initY1 = y1;
|
153 |
this.initY2 = y2;
|
154 |
|
155 |
yInverted = (y2 < y1); |
156 |
if (yInverted) {
|
157 |
this.initY1 = y2;
|
158 |
this.initY2 = y1;
|
159 |
} |
160 |
|
161 |
xInverted = (x2 < x1); |
162 |
if (xInverted) {
|
163 |
this.initX1 = x2;
|
164 |
this.initX2 = x1;
|
165 |
} |
166 |
|
167 |
this.updateDimensions();
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* Hace un forzado de pintado del buffer temporal de la imagen. Este m?todo
|
172 |
* forzar? una llamada a la funci?n de pintado del cliente.
|
173 |
*/
|
174 |
public void updateBuffer() { |
175 |
updateImageCache(true);
|
176 |
refreshImage(0, 0); |
177 |
} |
178 |
|
179 |
/**
|
180 |
* Especifica el zoom que usar? por defecto el componente.
|
181 |
* @param zoom
|
182 |
*/
|
183 |
public void setZoom(double zoom) { |
184 |
initZoom = zoom; |
185 |
this.zoom = initZoom;
|
186 |
autoAdjusted = false;
|
187 |
imageCenter(); |
188 |
} |
189 |
|
190 |
/**
|
191 |
* Especifica el zoom que usar? por defecto el componente.
|
192 |
* @param zoom
|
193 |
*/
|
194 |
public void setAutoAdjusted() { |
195 |
autoAdjusted = true;
|
196 |
updateDimensions(); |
197 |
updateImageCache(true);
|
198 |
refreshImage(0, 0); |
199 |
} |
200 |
|
201 |
/*
|
202 |
* (non-Javadoc)
|
203 |
* @see javax.swing.JComponent#addNotify()
|
204 |
*/
|
205 |
public void addNotify() { |
206 |
super.addNotify();
|
207 |
|
208 |
updateImageCache(true);
|
209 |
refreshImage(0, 0); |
210 |
} |
211 |
|
212 |
/**
|
213 |
* Hace un zoom de aumento en las coordenadas especificadas
|
214 |
* @param x
|
215 |
* @param y
|
216 |
*/
|
217 |
private void ZoomIn(double x, double y) { |
218 |
double xcent = (x / zoom);
|
219 |
double ycent = (y / zoom);
|
220 |
if (isXInverted())
|
221 |
x1 -= xcent; |
222 |
else
|
223 |
x1 += xcent; |
224 |
if (isYInverted())
|
225 |
y1 -= ycent; |
226 |
else
|
227 |
y1 += ycent; |
228 |
zoom = zoom * 2.0;
|
229 |
xcent = (x / zoom); |
230 |
ycent = (y / zoom); |
231 |
if (isXInverted())
|
232 |
x1 += xcent; |
233 |
else
|
234 |
x1 -= xcent; |
235 |
if (isYInverted())
|
236 |
y1 += ycent; |
237 |
else
|
238 |
y1 -= ycent; |
239 |
updateImageCache(true);
|
240 |
refreshImage(0, 0); |
241 |
} |
242 |
|
243 |
/**
|
244 |
* Hace un zoom hacia afuera en las coordenadas especificadas
|
245 |
* @param x
|
246 |
* @param y
|
247 |
*/
|
248 |
private void ZoomOut(double x, double y) { |
249 |
double xcent = (x / zoom);
|
250 |
double ycent = (y / zoom);
|
251 |
if (isXInverted())
|
252 |
x1 -= xcent; |
253 |
else
|
254 |
x1 += xcent; |
255 |
if (isYInverted())
|
256 |
y1 -= ycent; |
257 |
else
|
258 |
y1 += ycent; |
259 |
zoom = zoom / 2.0;
|
260 |
xcent = (x / zoom); |
261 |
ycent = (y / zoom); |
262 |
if (isXInverted())
|
263 |
x1 += xcent; |
264 |
else
|
265 |
x1 -= xcent; |
266 |
if (isYInverted())
|
267 |
y1 += ycent; |
268 |
else
|
269 |
y1 -= ycent; |
270 |
updateImageCache(true);
|
271 |
refreshImage(0, 0); |
272 |
} |
273 |
|
274 |
/**
|
275 |
* Mostrar o ocultar la ayuda
|
276 |
*
|
277 |
*/
|
278 |
private void callShowHelp() { |
279 |
showHelp = !showHelp; |
280 |
updateImageCache(true);
|
281 |
refreshImage(0, 0); |
282 |
} |
283 |
|
284 |
/*
|
285 |
* (non-Javadoc)
|
286 |
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
|
287 |
*/
|
288 |
public void keyPressed(KeyEvent e) { |
289 |
switch (e.getKeyChar()) {
|
290 |
case 'h': |
291 |
case 'H': |
292 |
callShowHelp(); |
293 |
break;
|
294 |
case '+': |
295 |
ZoomIn(width / 2.0, height / 2.0); |
296 |
autoAdjusted = false;
|
297 |
break;
|
298 |
case '-': |
299 |
ZoomOut(width / 2.0, height / 2.0); |
300 |
autoAdjusted = false;
|
301 |
break;
|
302 |
case '1': |
303 |
autoAdjusted = false;
|
304 |
this.zoom = initZoom;
|
305 |
imageCenter(); |
306 |
updateImageCache(true);
|
307 |
refreshImage(0, 0); |
308 |
break;
|
309 |
case '2': |
310 |
autoAdjusted = false;
|
311 |
this.zoom = initZoom * 2.0; |
312 |
imageCenter(); |
313 |
updateImageCache(true);
|
314 |
refreshImage(0, 0); |
315 |
break;
|
316 |
case '3': |
317 |
autoAdjusted = false;
|
318 |
this.zoom = initZoom * 4.0; |
319 |
imageCenter(); |
320 |
updateImageCache(true);
|
321 |
refreshImage(0, 0); |
322 |
break;
|
323 |
case '4': |
324 |
autoAdjusted = false;
|
325 |
this.zoom = initZoom * 8.0; |
326 |
imageCenter(); |
327 |
updateImageCache(true);
|
328 |
refreshImage(0, 0); |
329 |
break;
|
330 |
case '5': |
331 |
autoAdjusted = false;
|
332 |
this.zoom = initZoom * 16.0; |
333 |
imageCenter(); |
334 |
updateImageCache(true);
|
335 |
refreshImage(0, 0); |
336 |
break;
|
337 |
case 'c': |
338 |
case 'C': |
339 |
imageCenter(); |
340 |
updateImageCache(true);
|
341 |
refreshImage(0, 0); |
342 |
break;
|
343 |
case 'b': |
344 |
case 'B': |
345 |
setShowBackground(!isShowBackground()); |
346 |
break;
|
347 |
case '0': |
348 |
case ' ': |
349 |
setAutoAdjusted(); |
350 |
break;
|
351 |
} |
352 |
} |
353 |
|
354 |
double updateWidth = 0; |
355 |
double updateHeight = 0; |
356 |
/**
|
357 |
* M?todo que hara la invocaci?n al cliente del pintado del trozo de imagen a
|
358 |
* visualizar
|
359 |
* @param forceUpdate
|
360 |
*/
|
361 |
private void updateImageCache(boolean forceUpdate) { |
362 |
if (getWidgetImage() == null || |
363 |
(updateWidth == getWidgetImage().getWidth(this) &&
|
364 |
updateHeight == getWidgetImage().getHeight(this) &&
|
365 |
!forceUpdate)) |
366 |
return;
|
367 |
updateWidth = getWidgetImage().getWidth(this);
|
368 |
updateHeight = getWidgetImage().getHeight(this);
|
369 |
|
370 |
if (showBackground) {
|
371 |
for (int i = 0; (i * 4) <= width; i++) { |
372 |
for (int j = 0; (j * 4) <= height; j++) { |
373 |
if ((i + j) % 2 == 0) |
374 |
getCacheGraphics().setColor(Color.white);
|
375 |
else
|
376 |
getCacheGraphics().setColor(getBackgroundColor()); |
377 |
getCacheGraphics().fillRect(i * 4, j * 4, 4, 4); |
378 |
} |
379 |
} |
380 |
} else {
|
381 |
getCacheGraphics().setColor(Color.white);
|
382 |
getCacheGraphics().fillRect(0, 0, width, height); |
383 |
} |
384 |
|
385 |
double newY1 = 0.0; |
386 |
double newY2 = 0.0; |
387 |
double newX1 = 0.0; |
388 |
double newX2 = 0.0; |
389 |
if (isYInverted()) {
|
390 |
newY1 = y1 + this.getHeight() / zoom - ((y1 - initY1) * 2.0); |
391 |
newY2 = newY1 - this.getHeight() / zoom;
|
392 |
} else {
|
393 |
newY1 = y1; |
394 |
newY2 = y1 + this.getHeight() / zoom;
|
395 |
} |
396 |
if (isXInverted()) {
|
397 |
newX1 = x1 + this.getWidth() / zoom - ((x1 - initX1) * 2.0); |
398 |
newX2 = newX1 - this.getWidth() / zoom;
|
399 |
} else {
|
400 |
newX1 = x1; |
401 |
newX2 = x1 + this.getWidth() / zoom;
|
402 |
} |
403 |
if ((Double.isNaN(newX1)) || (Double.isNaN(newY1))) |
404 |
return;
|
405 |
iClient.drawImage(getCacheGraphics(), newX1, newY1, newX2, newY2, zoom, this.getWidth(), this.getHeight()); |
406 |
} |
407 |
|
408 |
private Image getWidgetImage() { |
409 |
int width2 = getBounds().width;
|
410 |
int height2 = getBounds().height;
|
411 |
if (width2 <= 0) |
412 |
width2 = 1;
|
413 |
if (height2 <= 0) |
414 |
height2=1;
|
415 |
|
416 |
if ((width != width2) || (height != height2)) {
|
417 |
image = createImage(width2, height2); |
418 |
imageCache = createImage(width2, height2); |
419 |
if (image == null) |
420 |
return null; |
421 |
widgetGraphics = (Graphics2D) image.getGraphics();
|
422 |
cacheGraphics = (Graphics2D) imageCache.getGraphics();
|
423 |
|
424 |
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
425 |
hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); |
426 |
cacheGraphics.setRenderingHints(hints); |
427 |
} |
428 |
|
429 |
width = width2; |
430 |
height = height2; |
431 |
return image;
|
432 |
} |
433 |
|
434 |
private Graphics2D getWidgetGraphics() { |
435 |
getWidgetImage(); |
436 |
return widgetGraphics;
|
437 |
} |
438 |
|
439 |
private Graphics2D getCacheGraphics() { |
440 |
getWidgetImage(); |
441 |
return cacheGraphics;
|
442 |
} |
443 |
|
444 |
/**
|
445 |
* Redibujar el componente en el graphics temporal
|
446 |
*/
|
447 |
private void redrawBuffer(int x, int y) { |
448 |
if (showBackground) {
|
449 |
for (int i = -2; ((i - 2) * 4) <= width; i++) { |
450 |
for (int j = -2; ((j - 2) * 4) <= height; j++) { |
451 |
if ((i + j) % 2 == 0) |
452 |
getWidgetGraphics().setColor(Color.white);
|
453 |
else
|
454 |
getWidgetGraphics().setColor(getBackgroundColor()); |
455 |
getWidgetGraphics().fillRect((i * 4) + (x % 8), (j * 4) + (y % 8), 4, 4); |
456 |
} |
457 |
} |
458 |
} else {
|
459 |
getWidgetGraphics().setColor(Color.white);
|
460 |
getWidgetGraphics().fillRect(0, 0, width, height); |
461 |
} |
462 |
|
463 |
getWidgetGraphics().drawImage(imageCache, x, y, null);
|
464 |
|
465 |
if (showHelp)
|
466 |
paintHelp((Graphics2D) getWidgetGraphics());
|
467 |
else
|
468 |
getWidgetGraphics().drawImage(getIconHelp().getImage(), width - getIconHelp().getIconWidth() - 4, 3, null); |
469 |
} |
470 |
|
471 |
/*
|
472 |
* (non-Javadoc)
|
473 |
* @see javax.swing.JComponent#paint(java.awt.Graphics)
|
474 |
*/
|
475 |
public void paint(Graphics g) { |
476 |
if (autoAdjusted) updateDimensions();
|
477 |
Graphics2D g2d = (Graphics2D) g; |
478 |
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
479 |
hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); |
480 |
g2d.setRenderingHints(hints); |
481 |
|
482 |
updateImageCache(false);
|
483 |
|
484 |
redrawBuffer(0, 0); |
485 |
|
486 |
if (image != null) { |
487 |
if (isEnabled()) {
|
488 |
g.drawImage(image, 0, 0, this); |
489 |
} else {
|
490 |
// Dibujar en escala de grises y aclarado para cuando esta inactivo
|
491 |
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
492 |
|
493 |
Graphics big = bi.createGraphics();
|
494 |
big.drawImage(image, 0, 0, this); |
495 |
|
496 |
ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); |
497 |
colorConvert.filter(bi, bi); |
498 |
|
499 |
big.setColor(new Color(255, 255, 255, 164)); |
500 |
big.fillRect(0, 0, width, height); |
501 |
|
502 |
g.drawImage(bi, getVisibleRect().x, getVisibleRect().y, this);
|
503 |
} |
504 |
} |
505 |
} |
506 |
|
507 |
/**
|
508 |
* Redibujar el componente en el graphics temporal y representarlo en el
|
509 |
* componente
|
510 |
*/
|
511 |
private void refreshImage(int x, int y) { |
512 |
Graphics2D g2d = (Graphics2D) getGraphics(); |
513 |
if (g2d == null) |
514 |
return;
|
515 |
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
516 |
hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); |
517 |
g2d.setRenderingHints(hints); |
518 |
redrawBuffer(x, y); |
519 |
|
520 |
if (image != null) |
521 |
getGraphics().drawImage(image, 0, 0, this); |
522 |
} |
523 |
|
524 |
/**
|
525 |
* Devuelve el icono de cerrar
|
526 |
* @return
|
527 |
*/
|
528 |
private ImageIcon getIconClose() { |
529 |
if (imageIconClose == null) { |
530 |
imageIconClose = new ImageIcon(getClass().getResource("images/close.png")); |
531 |
} |
532 |
return imageIconClose;
|
533 |
} |
534 |
|
535 |
/**
|
536 |
* Devuelve el icono ayuda
|
537 |
* @return
|
538 |
*/
|
539 |
private ImageIcon getIconHelp() { |
540 |
if (imageIconHelp == null) { |
541 |
imageIconHelp = new ImageIcon(getClass().getResource("images/help.png")); |
542 |
} |
543 |
return imageIconHelp;
|
544 |
} |
545 |
|
546 |
private void paintHelp(Graphics2D g) { |
547 |
int sep = 13; |
548 |
int pos = sep + 1; |
549 |
|
550 |
int alto = sep * 8 + 6; |
551 |
|
552 |
Image image2 = createImage(width, alto);
|
553 |
Graphics2D graphics2 = (Graphics2D) image2.getGraphics(); |
554 |
|
555 |
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
556 |
hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); |
557 |
graphics2.setRenderingHints(hints); |
558 |
|
559 |
alto--; |
560 |
|
561 |
Color color1 = new Color(255, 255, 178); |
562 |
Color color2 = new Color(255, 255, 74); |
563 |
graphics2.setPaint(new GradientPaint(0, 0, color1, 0, alto, color2, false)); |
564 |
graphics2.fillRect(0, 0, width, alto); |
565 |
|
566 |
graphics2.setColor(new Color(0, 0, 0)); |
567 |
|
568 |
graphics2.setFont(new java.awt.Font("Tahoma", 1, sep - 2)); |
569 |
|
570 |
graphics2.drawString(Messages.getText("teclas") + ":", 10, pos); |
571 |
|
572 |
graphics2.setFont(new java.awt.Font("Tahoma", 0, sep - 2)); |
573 |
pos += sep; |
574 |
graphics2.drawString(Messages.getText("ayuda_c"), 20, pos); |
575 |
pos += sep; |
576 |
graphics2.drawString(Messages.getText("ayuda_0"), 20, pos); |
577 |
pos += sep; |
578 |
graphics2.drawString(Messages.getText("ayuda_1_5"), 20, pos); |
579 |
pos += sep; |
580 |
graphics2.drawString(Messages.getText("ayuda_more_less"), 20, pos); |
581 |
pos += sep; |
582 |
graphics2.drawString(Messages.getText("ayuda_wheel"), 20, pos); |
583 |
pos += sep; |
584 |
graphics2.drawString(Messages.getText("ayuda_background"), 20, pos); |
585 |
pos += sep; |
586 |
graphics2.drawString(Messages.getText("ayuda_h"), 20, pos); |
587 |
|
588 |
graphics2.setColor(new Color(185, 185, 185)); |
589 |
graphics2.drawLine(0, alto, width, alto);
|
590 |
|
591 |
graphics2.drawImage(getIconClose().getImage(), width - getIconClose().getIconWidth() - 4, 3, null); |
592 |
|
593 |
AlphaComposite myAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f); |
594 |
g.setComposite(myAlpha); |
595 |
|
596 |
g.drawImage(image2, 0, 0, this); |
597 |
|
598 |
myAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f); |
599 |
g.setComposite(myAlpha); |
600 |
} |
601 |
|
602 |
Point mouse = null; |
603 |
/*
|
604 |
* (non-Javadoc)
|
605 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
606 |
*/
|
607 |
public void mousePressed(MouseEvent e) { |
608 |
if (!isEnabled())
|
609 |
return;
|
610 |
requestFocus(); |
611 |
|
612 |
if ((e.getX() > (width - 20)) && (e.getY() < 20)) |
613 |
return;
|
614 |
|
615 |
if ((e.getButton() != MouseEvent.BUTTON1) && (e.getButton() != MouseEvent.BUTTON2)) |
616 |
return;
|
617 |
|
618 |
// Oscurece la imagen cuando se mueve
|
619 |
Color gris = new Color(0, 0, 0, 16); |
620 |
getCacheGraphics().setColor(gris); |
621 |
getCacheGraphics().fillRect(0, 0, width-1, height-1); |
622 |
|
623 |
// Pone un borde a la imagen cuando se mueve
|
624 |
getCacheGraphics().setColor(Color.gray);
|
625 |
getCacheGraphics().drawRect(0, 0, width-1, height-1); |
626 |
|
627 |
mouse = new Point(e.getX(), e.getY()); |
628 |
changePos(e.getX(), e.getY()); |
629 |
autoAdjusted = false;
|
630 |
} |
631 |
|
632 |
/*
|
633 |
* (non-Javadoc)
|
634 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
635 |
*/
|
636 |
public void mouseDragged(MouseEvent e) { |
637 |
if (!isEnabled())
|
638 |
return;
|
639 |
changePos(e.getX(), e.getY()); |
640 |
} |
641 |
|
642 |
/*
|
643 |
* (non-Javadoc)
|
644 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
645 |
*/
|
646 |
public void mouseReleased(MouseEvent e) { |
647 |
if (!isEnabled())
|
648 |
return;
|
649 |
if (mouse != null) { |
650 |
x1 = x1 - ((e.getX() - mouse.getX())/zoom); |
651 |
y1 = y1 - ((e.getY() - mouse.getY())/zoom); |
652 |
updateImageCache(true);
|
653 |
refreshImage(0, 0); |
654 |
} |
655 |
mouse = null;
|
656 |
} |
657 |
|
658 |
private void changePos(int x, int y) { |
659 |
if (mouse != null) |
660 |
refreshImage((int) (x - mouse.getX()), (int) (y - mouse.getY())); |
661 |
} |
662 |
|
663 |
/**
|
664 |
* Evento de la rueda del rat?n para hacer Zoom In o Zoom Out en la posici?n
|
665 |
* del puntero.
|
666 |
*/
|
667 |
public void mouseWheelMoved(MouseWheelEvent e) { |
668 |
if (!isEnabled())
|
669 |
return;
|
670 |
|
671 |
if (e.getWheelRotation() > 0) { |
672 |
ZoomOut(isXInverted() ? this.getWidth() - e.getX() : e.getX(), isYInverted() ? this.getHeight() - e.getY() : e.getY()); |
673 |
autoAdjusted = false;
|
674 |
} |
675 |
if (e.getWheelRotation() < 0) { |
676 |
ZoomIn(isXInverted() ? this.getWidth() - e.getX() : e.getX(), isYInverted() ? this.getHeight() - e.getY() : e.getY()); |
677 |
autoAdjusted = false;
|
678 |
} |
679 |
} |
680 |
|
681 |
/**
|
682 |
* Obtener si el eje de las Y esta invertido
|
683 |
* @return
|
684 |
*/
|
685 |
private boolean isYInverted() { |
686 |
return yInverted;
|
687 |
} |
688 |
|
689 |
/**
|
690 |
* Obtener si el eje de las X esta invertido
|
691 |
* @return
|
692 |
*/
|
693 |
private boolean isXInverted() { |
694 |
return xInverted;
|
695 |
} |
696 |
|
697 |
/*
|
698 |
* (non-Javadoc)
|
699 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
700 |
*/
|
701 |
public void mouseMoved(MouseEvent e) { |
702 |
if (!isEnabled())
|
703 |
return;
|
704 |
if ((e.getX() > (width - 20)) && (e.getY() < 20)) { |
705 |
setCursor(new Cursor(Cursor.HAND_CURSOR)); |
706 |
if (showHelp)
|
707 |
setToolTipText(Messages.getText("cerrar"));
|
708 |
else
|
709 |
setToolTipText(Messages.getText("ayuda"));
|
710 |
} else {
|
711 |
setCursor(new Cursor(Cursor.MOVE_CURSOR)); |
712 |
setToolTipText(null);
|
713 |
} |
714 |
} |
715 |
|
716 |
/*
|
717 |
* (non-Javadoc)
|
718 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
719 |
*/
|
720 |
public void mouseClicked(MouseEvent e) { |
721 |
if (!isEnabled())
|
722 |
return;
|
723 |
if ((e.getX() > (width - 20)) && (e.getY() < 20)) |
724 |
callShowHelp(); |
725 |
} |
726 |
|
727 |
public void keyReleased(KeyEvent e) {} |
728 |
public void keyTyped(KeyEvent e) {} |
729 |
public void mouseEntered(MouseEvent e) {} |
730 |
public void mouseExited(MouseEvent e) {} |
731 |
|
732 |
/**
|
733 |
* Define el color de los recuadros del fondo
|
734 |
*
|
735 |
* @param backgroundColor the backgroundColor to set
|
736 |
*/
|
737 |
public void setBackgroundColor(Color backgroundColor) { |
738 |
this.backgroundColor = backgroundColor;
|
739 |
} |
740 |
|
741 |
/**
|
742 |
* Devuelve el color de los recuadros del fondo
|
743 |
* @return the backgroundColor
|
744 |
*/
|
745 |
public Color getBackgroundColor() { |
746 |
return backgroundColor;
|
747 |
} |
748 |
|
749 |
/**
|
750 |
* Devuelve si se esta mostrando o no la cuadricula de fondo.
|
751 |
* @return the showBackground
|
752 |
*/
|
753 |
public boolean isShowBackground() { |
754 |
return showBackground;
|
755 |
} |
756 |
|
757 |
/**
|
758 |
* Define si se muestra o no la cuadricula de fondo. Util para imagenes
|
759 |
* transparentes
|
760 |
* @param showBackground the showBackground to set
|
761 |
*/
|
762 |
public void setShowBackground(boolean showBackground) { |
763 |
this.showBackground = showBackground;
|
764 |
updateImageCache(true);
|
765 |
refreshImage(0, 0); |
766 |
} |
767 |
|
768 |
/* (non-Javadoc)
|
769 |
* @see javax.swing.JComponent#setEnabled(boolean)
|
770 |
*/
|
771 |
public void setEnabled(boolean enabled) { |
772 |
super.setEnabled(enabled);
|
773 |
updateImageCache(true);
|
774 |
refreshImage(0, 0); |
775 |
} |
776 |
} |