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.Transparency;
21 import java.awt.geom.Rectangle2D;
22 import java.awt.image.BufferedImage;
24 import java.net.URISyntaxException;
26 import org.simantics.maps.ProvisionException;
27 import org.simantics.maps.internal.ImageUtil;
28 import org.simantics.maps.tile.ITileProvider;
29 import org.simantics.maps.tile.TileKey;
32 * @author Tuukka Lehtonen
34 public class DebugTileProvider implements ITileProvider {
36 private final Rectangle2D EXTENT = new Rectangle2D.Double(-180, -90, 360, 180);
42 public DebugTileProvider(int tileSize) {
43 this.tileSize = tileSize;
46 this.source = new URI("debug://localhost");
47 } catch (URISyntaxException e) {
48 throw new Error("Should never happen", e);
53 public URI getSource() {
58 public Rectangle2D getExtent() {
59 return (Rectangle2D) EXTENT.clone();
63 public Image get(TileKey key) throws ProvisionException {
64 int level = key.getLevel();
68 double xTiles = Math.pow(2, level + 1);
69 double yTiles = Math.pow(2, level);
72 throw new IllegalArgumentException("invalid tile level " + level + " (tile=" + key +")");
73 if (x < 0 || x >= (int) xTiles)
74 throw new IllegalArgumentException("tile x out of bounds " + x + " (tile=" + key +")");
75 if (y < 0 || y >= (int) yTiles)
76 throw new IllegalArgumentException("tile y out of bounds " + y + " (tile=" + key +")");
78 Rectangle2D r = new Rectangle2D.Double();
85 double xdelta = w / xTiles;
86 double ydelta = h / yTiles;
88 double yy = yTiles - y - 1;
96 // System.out.println(yTiles + ", " + key.y + " => " + r);
98 BufferedImage image = ImageUtil.createScreenCompatibleImage(tileSize, tileSize, Transparency.OPAQUE);
99 Graphics2D g = image.createGraphics();
101 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
102 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
104 g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
106 g.setColor(Color.BLACK);
107 g.fillRect(0, 0, tileSize, tileSize);
108 g.setColor(Color.LIGHT_GRAY);
109 g.fillRect(1, 1, tileSize - 1, tileSize - 1);
110 g.setColor(Color.BLACK);
112 drawCenteredString(g, tileSize + "x" + tileSize, tileSize, tileSize / 2 - 50);
113 drawCenteredString(g, "(" + x + ", " + y + ")(" + level + ")", tileSize, tileSize / 2 + 50);
115 drawCenteredString(g, r.getMaxY() + "", tileSize, 50);
116 drawCenteredString(g, r.getMinY() + "", tileSize, 500);
117 g.drawString(r.getMinX() + "", 20, tileSize / 2);
118 g.drawString(r.getMaxX() + "", 412, tileSize / 2);
120 // Simulate network delays
121 try { Thread.sleep(100); } catch (InterruptedException e) {}
129 private void drawCenteredString(Graphics2D g, String str, int width, int y) {
130 FontMetrics metrics = g.getFontMetrics();
131 Rectangle2D r2d = metrics.getStringBounds(str, g);
132 g.drawString(str, (int)(width / 2 - r2d.getWidth() / 2.0), y);