// public void savePDF(){ // Rectangle suggestedPageSize = getITextPageSize(page1.getPageSize()); // Rectangle pageSize = rotatePageIfNecessary(suggestedPageSize); // //rotate if we need landscape // Document document = new Document(pageSize); // // PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); // document.open(); // Graphics2D graphics = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight()); // // // call your GTRenderer here // GTRenderer draw = new StreamingRenderer(); // draw.setMapContent(mapContent); // // draw.paint(graphics, outputArea, mapContent.getLayerBounds() ); // // // cleanup // graphics.dispose(); // // //cleanup // document.close(); // writer.close(); // } public void saveImage(final MapContent map, final String file, final int imageWidth) { GTRenderer renderer = new StreamingRenderer(); renderer.setMapContent(map); Rectangle imageBounds = null; ReferencedEnvelope mapBounds = null; try { mapBounds = map.getMaxBounds(); double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0); imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth)); } catch (Exception e) { // failed to access mapContent layers throw new RuntimeException(e); } BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB); Graphics2D gr = image.createGraphics(); gr.setPaint(Color.WHITE); gr.fill(imageBounds); try { renderer.paint(gr, imageBounds, mapBounds); File fileToSave = new File(file); ImageIO.write(image, "jpeg", fileToSave); } catch (Exception e) { e.printStackTrace(); } }
public void saveNewImage(MapContent map, String file) { GTRenderer renderer = new StreamingRenderer(); renderer.setMapContent(map); System.out.println("line 139"); Rectangle imageBounds = null; try { ReferencedEnvelope mapBounds = map.getMaxBounds(); double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0); int imageWidth = 600; imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth)); } catch (Exception e) { } System.out.println("line 151"); // Rectangle imageSize = new Rectangle(600,600); BufferedImage image = new BufferedImage( imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB); // darker red fill Graphics2D gr = image.createGraphics(); gr.setPaint(Color.WHITE); gr.fill(imageBounds); try { System.out.println("line 161"); renderer.paint(gr, imageBounds, map.getMaxBounds()); System.out.println("line 163"); File fileToSave = new File(file); System.out.println("line 166"); ImageIO.write(image, "jpeg", fileToSave); System.out.println("line 168"); } catch (IOException e) { } }
/** * It zooms in and out as the user uses the mousewheel. The mouse stays pointing in the same map * coordinates * * @param ev */ private void handleMouseWheelEvent(MouseWheelEvent ev) { if (this.IsRendering()) { return; } double zoomFactor = 0.5; int clicks = ev.getWheelRotation(); // -ve means wheel moved up, +ve means down int sign = (clicks > 0 ? -1 : 1); zoomFactor = sign * zoomFactor / 2; Point2D mouseMapPointPos = this.getPointInMap(ev.getPoint()); ReferencedEnvelope env = this.getDisplayArea(); double width = env.getSpan(0); double height = env.getSpan(1); double newWidth = width - (width * zoomFactor); double newHeight = height - (height * zoomFactor); double centerX = env.getMedian(0); double centerY = env.getMedian(1); double distanceMouseCenterAlongX = mouseMapPointPos.getX() - centerX; double distanceMouseCenterAlongY = mouseMapPointPos.getY() - centerY; centerX += distanceMouseCenterAlongX * zoomFactor; centerY += distanceMouseCenterAlongY * zoomFactor; double newMinX = centerX - newWidth / 2; double newMinY = centerY - newHeight / 2; double newMaxX = centerX + newWidth / 2; double newMaxY = centerY + newHeight / 2; env = new ReferencedEnvelope( newMinX, newMaxX, newMinY, newMaxY, env.getCoordinateReferenceSystem()); this.setDisplayArea(env); // this.refresh(); }
/** * It updates the pixel resolution of the map for each change of the DisplayArea. * * @param ev */ public void handleOnDisplayAreaChanged(MapPaneEvent ev) { ReferencedEnvelope newDisplayArea = this.getDisplayArea(); double widthInMap = newDisplayArea.getSpan(0); double widthInPixels = this.getSize().getWidth(); this.pixelResolution = widthInMap / widthInPixels; }