/** * Calculate the affine transforms used to convert between world and pixel coordinates. The * calculations here are very basic and assume a cartesian reference system. * * <p>Tne transform is calculated such that {@code envelope} will be centred in the display * * @param envelope the current map extent (world coordinates) * @param paintArea the current map pane extent (screen units) */ private void setTransforms(final Envelope envelope, final Rectangle paintArea) { ReferencedEnvelope refEnv = null; if (envelope != null) { refEnv = new ReferencedEnvelope(envelope); } else { refEnv = worldEnvelope(); // FIXME content.setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84); } java.awt.Rectangle awtPaintArea = Utils.toAwtRectangle(paintArea); double xscale = awtPaintArea.getWidth() / refEnv.getWidth(); double yscale = awtPaintArea.getHeight() / refEnv.getHeight(); double scale = Math.min(xscale, yscale); double xoff = refEnv.getMedian(0) * scale - awtPaintArea.getCenterX(); double yoff = refEnv.getMedian(1) * scale + awtPaintArea.getCenterY(); worldToScreen = new AffineTransform(scale, 0, 0, -scale, -xoff, yoff); try { screenToWorld = worldToScreen.createInverse(); } catch (NoninvertibleTransformException ex) { ex.printStackTrace(); } }
@SuppressWarnings("unchecked") private void paintMe(PaintEvent e) { int width = e.gc.getClipping().width; int height = e.gc.getClipping().height; if (map != null) { // sort out the transforms org.eclipse.swt.graphics.Rectangle paintArea = new org.eclipse.swt.graphics.Rectangle(0, 0, width, height); ReferencedEnvelope mapArea = map.getViewport().getBounds(); setTransforms(mapArea, paintArea); StreamingRenderer renderer = new StreamingRenderer(); renderer.setMapContent(map); RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderer.setJava2DHints(hints); @SuppressWarnings("rawtypes") Map rendererParams = new HashMap(); rendererParams.put("optimizedDataLoadingEnabled", new Boolean(true)); renderer.setRendererHints(rendererParams); org.eclipse.swt.graphics.Rectangle curPaintArea = e.gc.getClipping(); BufferedImage baseImage = new BufferedImage( curPaintArea.width + 1, curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = baseImage.createGraphics(); g2d.fillRect(0, 0, curPaintArea.width + 1, curPaintArea.height + 1); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // renderer.setContext(context); java.awt.Rectangle awtRectangle = Utils.toAwtRectangle(curPaintArea); final ReferencedEnvelope mapAOI = map.getViewport().getBounds(); renderer.paint(g2d, awtRectangle, mapAOI, getWorldToScreenTransform()); // swtImage.dispose(); if (swtImage != null && !swtImage.isDisposed()) { swtImage.dispose(); swtImage = null; } swtImage = new Image( canvas.getDisplay(), awtToSwt(baseImage, curPaintArea.width + 1, curPaintArea.height + 1)); // org.eclipse.swt.graphics.Image image = new // org.eclipse.swt.graphics.Image( // e.display, convertToSWT(tmpImage)); e.gc.drawImage(swtImage, 0, 0); } double y2 = Math.random() * 120d; e.gc.drawLine(20, 40, 80, (int) y2); };
/** * Return a (copy of) the currently displayed map area. * * <p>Note, this will not always be the same as the envelope returned by {@code * MapContext.getAreaOfInterest()}. For example, when the map is displayed at the full extent of * all layers {@code MapContext.getAreaOfInterest()} will return the union of the layer bounds * while this method will return an evnelope that can included extra space beyond the bounds of * the layers. * * @return the display area in world coordinates as a new {@code ReferencedEnvelope} */ public ReferencedEnvelope getDisplayArea() { ReferencedEnvelope aoi = null; if (curPaintArea != null && screenToWorld != null) { Rectangle2D awtRectangle = Utils.toAwtRectangle(curPaintArea); Point2D p0 = new Point2D.Double(awtRectangle.getMinX(), awtRectangle.getMinY()); Point2D p1 = new Point2D.Double(awtRectangle.getMaxX(), awtRectangle.getMaxY()); screenToWorld.transform(p0, p0); screenToWorld.transform(p1, p1); aoi = new ReferencedEnvelope( Math.min(p0.getX(), p1.getX()), Math.max(p0.getX(), p1.getX()), Math.min(p0.getY(), p1.getY()), Math.max(p0.getY(), p1.getY()), content.getCoordinateReferenceSystem()); } return aoi; }
@SuppressWarnings("deprecation") public void handleEvent(Event event) { curPaintArea = getVisibleRect(); // System.out.println("event: " + event.type); if (event.type == SWT.MouseDown) { startX = event.x; startY = event.y; // start mouse activity mouseDown = true; } else if (event.type == SWT.MouseUp) { endX = event.x; endY = event.y; boolean mouseWasMoved = startX != endX || startY != endY; if (toolCanMove && mouseWasMoved) { // if the tool is able to move draw the moved image afterImageMove(); } // stop mouse activity mouseDown = false; isDragging = false; } else if (event.type == SWT.Paint) { // System.out.println("PAINT CALLED (DOESN'T MEAN I'M DRAWING)"); if (acceptRepaintRequests) { gc = event.gc; // System.out.println(toolCanDraw + "/" + toolCanMove + "/" + isDragging + "/" + // redrawBaseImage); /* * if the mouse is dragging and the current tool can * move the map we just draw what we already have * on white background. At the end of the moving * we will take care of adding the missing pieces. */ if (toolCanMove && isDragging) { // System.out.println("toolCanMove && isDragging"); if (gc != null && !gc.isDisposed() && swtImage != null) { /* * double buffer necessary, since the SWT.NO_BACKGROUND * needed by the canvas to properly draw background, doesn't * clean the parts outside the bounds of the moving panned image, * giving a spilling image effect. */ Image tmpImage = new Image(getDisplay(), curPaintArea.width, curPaintArea.height); GC tmpGc = new GC(tmpImage); tmpGc.setBackground(white); tmpGc.fillRectangle(0, 0, curPaintArea.width, curPaintArea.height); tmpGc.drawImage(swtImage, imageOrigin.x, imageOrigin.y); gc.drawImage(tmpImage, 0, 0); tmpImage.dispose(); } return; } /* * if the mouse is dragging and the current tool can * draw a boundingbox while dragging, we draw the box * keeping the current drawn image */ if (toolCanDraw && toolManager.getCursorTool().isDrawing() && isDragging) { // System.out.println("draw box: " + startX + "/" + startY + "/" + endX + // "/" + endY); if (swtImage != null) { drawFinalImage(swtImage); } gc.setXORMode(true); org.eclipse.swt.graphics.Color fC = gc.getForeground(); gc.setLineStyle(cursorToolLineStyle); gc.setLineWidth(cursorToolLineWidth); gc.setForeground(cursorToolColor); gc.drawRectangle(startX, startY, endX - startX, endY - startY); gc.setForeground(fC); gc.setXORMode(false); return; } if (!toolCanDraw && !toolCanMove && isDragging) { return; } if (curPaintArea == null || content == null || renderer == null) { return; } if (content.layers().size() == 0) { // if no layers available, return only if there are also no overlays gc.setForeground(yellow); gc.fillRectangle(0, 0, curPaintArea.width + 1, curPaintArea.height + 1); if (overlayImage == null) return; } final ReferencedEnvelope mapAOI = content.getViewport().getBounds(); if (mapAOI == null) { return; } if (redrawBaseImage) { MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STARTED); publishEvent(ev); baseImage = new BufferedImage( curPaintArea.width + 1, curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = baseImage.createGraphics(); g2d.fillRect(0, 0, curPaintArea.width + 1, curPaintArea.height + 1); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // renderer.setContext(context); java.awt.Rectangle awtRectangle = Utils.toAwtRectangle(curPaintArea); renderer.paint(g2d, awtRectangle, mapAOI, getWorldToScreenTransform()); // swtImage.dispose(); if (swtImage != null && !swtImage.isDisposed()) { swtImage.dispose(); swtImage = null; } // System.out.println("READRAWBASEIMAGE"); swtImage = new Image( getDisplay(), awtToSwt(baseImage, curPaintArea.width + 1, curPaintArea.height + 1)); } if (swtImage != null) { drawFinalImage(swtImage); } MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STOPPED); publishEvent(ev); clearLabelCache = true; onRenderingCompleted(); redrawBaseImage = false; } } }