[Schmitzm-commits] r505 - in branches/1.0-gt2-2.6/src: gtmig/org/geotools/swing schmitzm/geotools/gui

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Oct 31 11:53:44 CET 2009


Author: alfonx
Date: 2009-10-31 11:53:43 +0100 (Sat, 31 Oct 2009)
New Revision: 505

Modified:
   branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
   branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java
Log:
* Added fast scaled previews to JMapPane rendering. Works for ZoomIn and ZoomOut. 
* Added setMapbackground and getMapBackground to JMapPane


Modified: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java	2009-10-27 12:57:58 UTC (rev 504)
+++ branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java	2009-10-31 10:53:43 UTC (rev 505)
@@ -43,10 +43,12 @@
 import java.awt.Graphics2D;
 import java.awt.LayoutManager;
 import java.awt.Rectangle;
+import java.awt.RenderingHints;
 import java.awt.event.InputEvent;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
+import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
@@ -176,6 +178,11 @@
 	 */
 	private boolean panning_started = false;
 
+	/**
+	 * This color is used as the default background color when painting a map.
+	 */
+	private Color mapBackgroundColor = Color.WHITE;
+
 	public JMapPane() {
 		this(null, true, null, null);
 	}
@@ -339,17 +346,24 @@
 
 		if (changed) { /* if the map changed then redraw */
 			changed = false;
+			
+			// The baseImage should not have alpha
 			baseImage = new BufferedImage(dr.width, dr.height,
-					BufferedImage.TYPE_INT_ARGB);
+					BufferedImage.TYPE_INT_RGB); 
 
 			final Graphics2D ig = baseImage.createGraphics();
-			/* System.out.println("rendering"); */
+			ig.setBackground(getMapBackgroundColor());
+			ig.fillRect(0, 0, dr.width, dr.height);
+			
+//			/* System.out.println("rendering"); */
 			if (renderer.getContext() != null)
 				renderer.setContext(context);
-			labelCache.clear(); // work around anoying labelcache bug
 
+			// TODO is this clearing still needed? We already replace all that stuff whenever we change the style. Deactivated 31.10.09.. let's see 
+//			labelCache.clear(); // work around anoying labelcache bug
+
 			// draw the map
-			renderer.paint((Graphics2D) ig, dr, mapArea);
+			renderer.paint(ig, dr, mapArea);
 
 			// TODO nur machen, wenn panning beginnt
 			panningImage = new BufferedImage(dr.width, dr.height,
@@ -483,11 +497,7 @@
 				final int dy = lastY - startY;
 				// System.out.println("translate "+dx+","+dy);
 				final Graphics2D g2 = panningImage.createGraphics();
-				g2.setBackground(new Color(240, 240, 240)); // TODO richtige
-				// farbe? am besten
-				// vom L&F die
-				// hintergrundfarbe
-				// auslesen...
+				g2.setBackground(getMapBackgroundColor()); 
 
 				g2.clearRect(0, 0, this.getWidth(), this.getHeight());
 				g2.drawImage(baseImage, dx, dy, this);
@@ -644,18 +654,29 @@
 			setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
 
 			// sk.sc
-			// {
+			 {
 			// // SK tries to paint a preview of the zoom ;-9 aha.... well
-			// Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
-			// graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
-			// RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
-			// graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-			// RenderingHints.VALUE_ANTIALIAS_OFF);
-			// graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
-			// RenderingHints.VALUE_RENDER_SPEED);
-			// graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
-			// JMapPane.this.getHeight(), x1, y1, x2, y2, null);
-			// }
+//				 
+//				 double reductionFactor = 1/8.;
+////				 
+////			 BufferedImage reducedBaseImage = new BufferedImage((int)(baseImage.getWidth()*reductionFactor), (int)(baseImage.getHeight()*reductionFactor), BufferedImage.TYPE_INT_RGB);
+////			 Graphics2D reducedG = (Graphics2D) reducedBaseImage.getGraphics();
+//			 AffineTransform reductionTransform =new AffineTransform();
+//			 reductionTransform.scale(reductionFactor, reductionFactor);
+////			reducedG.drawImage(baseImage, reductionTransform, null);
+//			 
+//			 Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
+////			 graphics.getTransform().scale(reductionFactor, reductionFactor);
+//			 
+//			 graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+//			 RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
+//			 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+//			 RenderingHints.VALUE_ANTIALIAS_OFF);
+//			 graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
+//			 RenderingHints.VALUE_RENDER_SPEED);
+//			 graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
+//			 JMapPane.this.getHeight(), x1, y1, x2, y2, null);
+			 }
 			// xulu.ec
 		} else if (state == JMapPane.ZoomOut) {
 			drawRectangle(this.getGraphics());
@@ -1103,4 +1124,20 @@
 		return maxExtend;
 	}
 
+	/**
+	 * Set the background color of the map. 
+	 * @param if <code>null</code>, white is used.
+	 */
+	public void setMapBackgroundColor(Color bgColor) {
+		if (bgColor == null) bgColor = Color.WHITE;
+		this.mapBackgroundColor = bgColor;
+	}
+
+	/**
+	 * Returns the background {@link Color} of the map pane. Default is white.
+	**/
+	public Color getMapBackgroundColor() {
+		return mapBackgroundColor;
+	}
+
 }

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java	2009-10-27 12:57:58 UTC (rev 504)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java	2009-10-31 10:53:43 UTC (rev 505)
@@ -34,6 +34,7 @@
 import java.awt.Component;
 import java.awt.Cursor;
 import java.awt.Graphics;
+import java.awt.Graphics2D;
 import java.awt.LayoutManager;
 import java.awt.Point;
 import java.awt.RenderingHints;
@@ -41,8 +42,10 @@
 import java.awt.event.MouseWheelEvent;
 import java.awt.event.MouseWheelListener;
 import java.awt.geom.AffineTransform;
+import java.awt.geom.NoninvertibleTransformException;
 import java.awt.geom.Point2D;
 import java.awt.image.BufferedImage;
+import java.awt.image.ImageObserver;
 import java.io.IOException;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -52,6 +55,7 @@
 import java.util.Vector;
 
 import javax.swing.JList;
+import javax.swing.SwingUtilities;
 import javax.swing.event.MouseInputAdapter;
 
 import org.apache.log4j.Logger;
@@ -177,11 +181,49 @@
 		// Links-Drag erfolgen!!
 		// Ausnahme: Bei Rechts-Drag (Button 3) soll die Methode
 		// trotzdem aufgerufen werden fuer Pan!
+		
 		if (getState() == ZOOM_IN && getWindowSelectionState() != ZOOM_IN
 				&& e.getButton() != 3)
 			return;
+		
+
+		/** We store the old transform for a moment to use it for the
+		 "quick scaled preview" in case of ZoomIn **/
+		AffineTransform oldTransform = getTransform();
+		/** We store the old mapArea for a moment to use it for the
+		 "quick scaled preview" in case of ZoomOut **/
+		Envelope oldMapArea = getMapArea();
+
 		super.processDrag(x1, y1, x2, y2, e);
+		
+		if (getState() == JMapPane.ZOOM_IN) {
+			// Draw a quick scaled preview image, so the user is not do bored
 
+			drawScaledPreviewImage_ZoomIn(oldTransform);
+			
+		} else if (getState() == JMapPane.ZOOM_OUT) {
+			drawScaledPreviewImage_ZoomOut(oldMapArea);
+//			// Draw a quick scaled preview image, so the user is not do bored
+//
+//			Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
+//			Envelope gg = tranformGeoToWindow(mapArea.getMinX(), mapArea.getMinY(), mapArea.getMaxX(), mapArea.getMaxY(), oldTransform);
+//
+//			graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+//					RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
+//			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+//					RenderingHints.VALUE_ANTIALIAS_OFF);
+//			graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
+//					RenderingHints.VALUE_RENDER_SPEED);
+//			
+//			int xx1 = (int)gg.getMinX();
+//			int yy1 = (int)gg.getMinY(); 
+//			int xx2 = (int)gg.getMaxX();
+//			int yy2 = (int)gg.getMaxY();
+//			
+//			graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(), JMapPane.this.getHeight(), xx1,yy1,xx2,yy2,
+//					 null);
+		}
+
 		// SK, 19.6.2009:
 		// Ein MapAreaChangedEvent soll nur geworfen werden, wenn auch gezoomt
 		// wurde! Irgendwie wird das auch aufgerufen, wenn man mit InfoClick
@@ -191,6 +233,73 @@
 			fireMapPaneEvent(new MapAreaChangedEvent(this, oldMapArea, mapArea));
 	}
 
+	/**
+	 * Diretly paints scaled preview into the {@link JMapPane}. Used to give the
+	 * user something to look at while we are rendering. Method should be called
+	 * after {@link #setMapArea(Envelope)} has been set to the new mapArea and
+	 * transform has been reset.
+	 * 
+	 * @param oldTransform
+	 *            The old transform has to be passed as parameter.
+	 */
+	private void drawScaledPreviewImage_ZoomIn(AffineTransform oldTransform) {
+		
+		
+			Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
+			Envelope gg = tranformGeoToWindow(mapArea.getMinX(), mapArea.getMinY(),
+					mapArea.getMaxX(), mapArea.getMaxY(), oldTransform);
+			
+			graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+					RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
+			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+					RenderingHints.VALUE_ANTIALIAS_OFF);
+			graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
+					RenderingHints.VALUE_RENDER_SPEED);
+			
+			int xx1 = (int) gg.getMinX();
+			int yy1 = (int) gg.getMinY();
+			int xx2 = (int) gg.getMaxX();
+			int yy2 = (int) gg.getMaxY();
+
+			graphics.setBackground(getMapBackgroundColor());
+			graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
+					JMapPane.this.getHeight(), xx1, yy1, xx2, yy2, null);
+	}
+	
+	/**
+	 * Diretly paints scaled preview into the {@link JMapPane}. Used to give the
+	 * user something to look at while we are rendering. Method should be called
+	 * after {@link #setMapArea(Envelope)} has been set to the new mapArea and
+	 * transform has been reset.
+	 * 
+	 * @param oldMapArea
+	 *            The old mapArea has to be passed as parameter.
+	 */
+	private void drawScaledPreviewImage_ZoomOut(Envelope oldMapArea) {
+		
+		Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
+		Envelope gg = tranformGeoToWindow(oldMapArea.getMinX(), oldMapArea.getMinY(),
+				oldMapArea.getMaxX(), oldMapArea.getMaxY(), null);
+		
+		graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+				RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
+		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+				RenderingHints.VALUE_ANTIALIAS_OFF);
+		graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
+				RenderingHints.VALUE_RENDER_SPEED);
+		
+		int xx1 = (int) gg.getMinX();
+		int yy1 = (int) gg.getMinY();
+		int xx2 = (int) gg.getMaxX();
+		int yy2 = (int) gg.getMaxY();
+		
+		graphics.setBackground(getMapBackgroundColor());
+		graphics.clearRect(0, 0,  JMapPane.this.getWidth(),
+				JMapPane.this.getHeight());
+			graphics.drawImage(baseImage, xx1, yy1, xx2, yy2, 0, 0, JMapPane.this.getWidth(),
+					JMapPane.this.getHeight(), null);
+	}
+
 	private static final Cursor WAIT_CURSOR = Cursor
 			.getPredefinedCursor(Cursor.WAIT_CURSOR);
 
@@ -1151,6 +1260,32 @@
 		Point2D geoP = at.transform(new Point2D.Double(px, py), null);
 		return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY());
 	}
+	
+	/**
+	 * Transformiert einen Geo-Koordinaten-Bereich in Fenster-Koordinaten.
+	 * 
+	 * @param ox
+	 *            X-Koordinate der VON-Position
+	 * @param oy
+	 *            Y-Koordinate der VON-Position
+	 * @param px
+	 *            X-Koordinate der BIS-Position
+	 * @param py
+	 *            Y-Koordinate der BIS-Position
+	 * @param winToGeotransform Eine Window to Geo transform. If <code>null</code>, {@link #getTransform()} is used.
+	 */
+	public Envelope tranformGeoToWindow(double ox, double oy, double px, double py, AffineTransform winToGeotransform) {
+		AffineTransform at = winToGeotransform == null ? getTransform() : winToGeotransform;
+		Point2D geoO;
+		try {
+			geoO = at.inverseTransform(new Point2D.Double(ox, oy), null);
+			Point2D geoP = at.inverseTransform(new Point2D.Double(px, py), null);
+			return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY());
+		} catch (NoninvertibleTransformException e) {
+			LOGGER.error(e);
+			return new Envelope(ox,oy,px,py);
+		}
+	}
 
 	/**
 	 * Transformiert eine Fenster-Koordinate in eine Geo-Koordinate.
@@ -1234,6 +1369,13 @@
 	 */
 	@Override
 	public void setMapArea(Envelope env) {
+		
+		if (env != null && env.equals(mapArea)) {
+			// Just return if nothing chnaged 
+			return;
+		}
+		
+		
 		// **********************************************************************
 		// Ueber die Funktionen setMaxZoomScale und setMinZoomScale kann der
 		// geotools JMapPane ein gueltiger Massstabsbereich gesetzt werden.
@@ -1278,6 +1420,13 @@
 		// double oldScale = getScale();
 		// Envelope oldEnv = getMapArea();
 
+		/** We store the old transform for a moment to use it for the
+		 "quick scaled preview" in case of ZoomIn **/
+		AffineTransform oldTransform = getTransform();
+		/** We store the old mapArea for a moment to use it for the
+		 "quick scaled preview" in case of ZoomOut **/
+		Envelope oldMapArea = getMapArea();
+
 		switch (e.getButton()) {
 		// Linksklick --> Eingestellte Funktion
 		case MouseEvent.BUTTON1: // SimpleFeature-Auswahl nicht ueber die
@@ -1383,7 +1532,7 @@
 									}
 
 								}
-								
+
 							} finally {
 								fc.close(fcIt);
 							}
@@ -1415,10 +1564,10 @@
 				break;
 			}
 			super.mouseClicked(e);
-			return; // was: breka, aber wir brauchen ja nicht das
-			// setMapArea(mapArea)
 
-			// Rechtsklick --> Zoom-Out
+			break;
+
+		// Rechtsklick --> Zoom-Out
 		case MouseEvent.BUTTON3:
 			int oldState = getState();
 			// MS: ist doch eleganter, wenn IMMER mit Rechtsklick raus-gezoomt
@@ -1430,6 +1579,7 @@
 			super.mouseClicked(e);
 			setState(oldState);
 			break;
+
 		// Sonst nix
 		default:
 			return;
@@ -1452,6 +1602,14 @@
 		// if ( oldEnv == null && newEnv != null || oldEnv != null &&
 		// !oldEnv.equals( newEnv ) )
 		// fireMapPaneEvent( new MapAreaChangedEvent(this,oldEnv,newEnv) );
+
+		if (getState() == ZOOM_IN && e.getButton() == MouseEvent.BUTTON1)
+			drawScaledPreviewImage_ZoomIn(oldTransform);
+		else if (getState() == ZOOM_IN && e.getButton() == MouseEvent.BUTTON3)
+			drawScaledPreviewImage_ZoomOut(oldMapArea);
+		else if (getState() == ZOOM_OUT&& e.getButton() == MouseEvent.BUTTON3)
+			drawScaledPreviewImage_ZoomOut(oldMapArea);
+
 	}
 
 	/**



More information about the Schmitzm-commits mailing list