1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.maps.debug;
14 import java.awt.Color;
16 import java.awt.FontMetrics;
17 import java.awt.Graphics2D;
18 import java.awt.Image;
19 import java.awt.RenderingHints;
20 import java.awt.geom.Rectangle2D;
21 import java.awt.image.BufferedImage;
23 import java.net.URISyntaxException;
25 import org.simantics.maps.ProvisionException;
26 import org.simantics.maps.tile.ITileProvider;
27 import org.simantics.maps.tile.TileKey;
30 * @author Tuukka Lehtonen
32 public class DebugTileProvider implements ITileProvider {
34 private final Rectangle2D EXTENT = new Rectangle2D.Double(-180, -90, 360, 180);
40 public DebugTileProvider(int tileSize) {
41 this.tileSize = tileSize;
44 this.source = new URI("debug://localhost");
45 } catch (URISyntaxException e) {
46 throw new Error("Should never happen", e);
51 public URI getSource() {
56 public Rectangle2D getExtent() {
57 return (Rectangle2D) EXTENT.clone();
61 public Image get(TileKey key) throws ProvisionException {
62 int level = key.getLevel();
66 double xTiles = Math.pow(2, level + 1);
67 double yTiles = Math.pow(2, level);
70 throw new IllegalArgumentException("invalid tile level " + level + " (tile=" + key +")");
71 if (x < 0 || x >= (int) xTiles)
72 throw new IllegalArgumentException("tile x out of bounds " + x + " (tile=" + key +")");
73 if (y < 0 || y >= (int) yTiles)
74 throw new IllegalArgumentException("tile y out of bounds " + y + " (tile=" + key +")");
76 Rectangle2D r = new Rectangle2D.Double();
83 double xdelta = w / xTiles;
84 double ydelta = h / yTiles;
86 double yy = yTiles - y - 1;
94 // System.out.println(yTiles + ", " + key.y + " => " + r);
96 BufferedImage image = new BufferedImage(tileSize, tileSize, BufferedImage.TYPE_3BYTE_BGR);
97 Graphics2D g = image.createGraphics();
99 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
100 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
102 g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 32));
104 g.setColor(Color.BLACK);
105 g.fillRect(0, 0, tileSize, tileSize);
106 g.setColor(Color.LIGHT_GRAY);
107 g.fillRect(1, 1, tileSize - 1, tileSize - 1);
108 g.setColor(Color.BLACK);
110 drawCenteredString(g, tileSize + "x" + tileSize, tileSize, tileSize / 2 - 50);
111 drawCenteredString(g, "(" + x + ", " + y + ")(" + level + ")", tileSize, tileSize / 2 + 50);
113 drawCenteredString(g, r.getMaxY() + "", tileSize, 50);
114 drawCenteredString(g, r.getMinY() + "", tileSize, 500);
115 g.drawString(r.getMinX() + "", 20, tileSize / 2);
116 g.drawString(r.getMaxX() + "", 412, tileSize / 2);
118 // Simulate network delays
119 try { Thread.sleep(100); } catch (InterruptedException e) {}
127 private void drawCenteredString(Graphics2D g, String str, int width, int y) {
128 FontMetrics metrics = g.getFontMetrics();
129 Rectangle2D r2d = metrics.getStringBounds(str, g);
130 g.drawString(str, (int)(width / 2 - r2d.getWidth() / 2.0), y);