package com.github.davidmoten.rtree;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.google.common.base.Optional;

public class Visualizer {

	private final RTree tree;
	private final int width;
	private final int height;

	public Visualizer(RTree tree, int width, int height, Rectangle view) {
		this.tree = tree;
		this.width = width;
		this.height = height;
	}

	public BufferedImage create() {
		final BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_ARGB);
		final Graphics2D g = (Graphics2D) image.getGraphics();
		if (tree.root().isPresent())
			drawNode(image, tree.root(), 0);
		return image;
	}

	private void drawNode(BufferedImage image, Optional<Node> node, int depth) {

	}

	public void save(File file, String imageFormat) {
		try {
			ImageIO.write(create(), imageFormat, file);
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}

}

@@ -31,16 +31,39 @@
 rtree;%0A%0A
+import java.awt.Color;%0A
 import j
@@ -204,49 +204,8 @@
 O;%0A%0A
-import com.google.common.base.Optional;%0A%0A
 publ
@@ -306,16 +306,47 @@
  height;
+%0A%09private final Rectangle view;
 %0A%0A%09publi
@@ -477,16 +477,36 @@
 height;%0A
+%09%09this.view = view;%0A
 %09%7D%0A%0A%09pub
@@ -731,21 +731,48 @@
 rawNode(
-image
+(Graphics2D) image.getGraphics()
 , tree.r
@@ -776,16 +776,22 @@
 e.root()
+.get()
 , 0);%0A%09%09
@@ -835,63 +835,783 @@
 ode(
-BufferedImage image, Optional%3CNode%3E node, int depth) %7B%0A
+Graphics2D g, Node node, int depth) %7B%0A%09%09Color color = Color.getHSBColor(depth / 10f, 1f, 1f);%0A%09%09g.setColor(color);%0A%09%09Rectangle r = node.mbr();%0A%09%09drawRectangle(g, r);%0A%09%09if (node instanceof Leaf) %7B%0A%09%09%09g.setColor(Color.black);%0A%09%09%09Leaf leaf = (Leaf) node;%0A%09%09%09for (Entry entry : leaf.entries()) %7B%0A%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%7D%0A%0A%09private void drawRectangle(Graphics2D g, Rectangle r) %7B%0A%09%09double x1 = (r.x1() - view.x1()) / (view.x2() - view.x1()) * width;%0A%09%09double y1 = (r.y1() - view.y1()) / (view.y2() - view.y1()) * height;%0A%09%09double x2 = (r.x2() - view.x1()) / (view.x2() - view.x1()) * width;%0A%09%09double y2 = (r.y2() - view.y1()) / (view.y2() - view.y1()) * height;%0A%09%09g.drawRect(rnd(x1), rnd(y1), rnd(x2 - x1), rnd(y2 - y1));%0A%09%7D%0A%0A%09private static int rnd(double d) %7B%0A%09%09return (int) Math.round(d);
 %0A%09%7D%0A

use rx