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