/******************************************************************************* * Copyright (c) 2012 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.maps.debug; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.net.URI; import java.net.URISyntaxException; import org.simantics.maps.ProvisionException; import org.simantics.maps.tile.ITileProvider; import org.simantics.maps.tile.TileKey; /** * @author Tuukka Lehtonen */ public class DebugTileProvider implements ITileProvider { private final Rectangle2D EXTENT = new Rectangle2D.Double(-180, -90, 360, 180); private int tileSize; private URI source; public DebugTileProvider(int tileSize) { this.tileSize = tileSize; try { this.source = new URI("debug://localhost"); } catch (URISyntaxException e) { throw new Error("Should never happen", e); } } @Override public URI getSource() { return source; } @Override public Rectangle2D getExtent() { return (Rectangle2D) EXTENT.clone(); } @Override public Image get(TileKey key) throws ProvisionException { int level = key.getLevel(); int x = key.getX(); int y = key.getY(); double xTiles = Math.pow(2, level + 1); double yTiles = Math.pow(2, level); if (level < 0) throw new IllegalArgumentException("invalid tile level " + level + " (tile=" + key +")"); if (x < 0 || x >= (int) xTiles) throw new IllegalArgumentException("tile x out of bounds " + x + " (tile=" + key +")"); if (y < 0 || y >= (int) yTiles) throw new IllegalArgumentException("tile y out of bounds " + y + " (tile=" + key +")"); Rectangle2D r = new Rectangle2D.Double(); double minx = -180; double miny = -90; double w = 360; double h = 180; double xdelta = w / xTiles; double ydelta = h / yTiles; double xx = x; double yy = yTiles - y - 1; r.setFrame( minx + xdelta*xx, miny + ydelta*yy, xdelta, ydelta ); // System.out.println(yTiles + ", " + key.y + " => " + r); BufferedImage image = new BufferedImage(tileSize, tileSize, BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = image.createGraphics(); try { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 32)); g.setColor(Color.BLACK); g.fillRect(0, 0, tileSize, tileSize); g.setColor(Color.LIGHT_GRAY); g.fillRect(1, 1, tileSize - 1, tileSize - 1); g.setColor(Color.BLACK); drawCenteredString(g, tileSize + "x" + tileSize, tileSize, tileSize / 2 - 50); drawCenteredString(g, "(" + x + ", " + y + ")(" + level + ")", tileSize, tileSize / 2 + 50); drawCenteredString(g, r.getMaxY() + "", tileSize, 50); drawCenteredString(g, r.getMinY() + "", tileSize, 500); g.drawString(r.getMinX() + "", 20, tileSize / 2); g.drawString(r.getMaxX() + "", 412, tileSize / 2); // Simulate network delays try { Thread.sleep(100); } catch (InterruptedException e) {} return image; } finally { g.dispose(); } } private void drawCenteredString(Graphics2D g, String str, int width, int y) { FontMetrics metrics = g.getFontMetrics(); Rectangle2D r2d = metrics.getStringBounds(str, g); g.drawString(str, (int)(width / 2 - r2d.getWidth() / 2.0), y); } }