Skip to content

Commit

Permalink
Made example modular
Browse files Browse the repository at this point in the history
  • Loading branch information
Jotschi committed May 8, 2011
1 parent 604522f commit ec0475b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 29 deletions.
15 changes: 13 additions & 2 deletions src/main/java/at/jotschi/quadtree/BoundsQuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
public class BoundsQuadTree<T> extends QuadTree<T> {

protected BoundsNode<T> rootNode;

/**
* Create a new QuadTree with the give start coordinates and size
*
Expand Down Expand Up @@ -67,9 +69,18 @@ public void insert(Point point, Dimension elementSize, T element) {
+ "] / [" + point.x + elementSize.height + "]");
}

this.rootNode.insert(new BoundsNodeElement<T>(point, elementSize, element));
this.rootNode.insert(new BoundsNodeElement<T>(point, elementSize,
element));

}

/**
* Returns the rootNode of this tree
*
* @return
*/
public BoundsNode<T> getRootNode() {
return this.rootNode;
}


}
20 changes: 9 additions & 11 deletions src/test/java/at/jotschi/quadtree/QuadTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,23 @@ public void testApp() {
tree.insert(552, 555, "6");
tree.insert(551, 555, "7");

Vector<NodeElement<String>> elements = (Vector<NodeElement<String>>) tree.getElements(new Point(500,
550));
Vector<NodeElement<String>> elements = (Vector<NodeElement<String>>) tree
.getElements(new Point(500, 550));

for (NodeElement<String> element : elements) {
System.out.println("[" + element.getX() + "-" + element.getY()
+ "] " + element.getElement());
}
}

@Test
public void quadTreeBoundsTest() {
BoundsQuadTree<String> tree = new BoundsQuadTree<String>(new Point(0, 0),
new Dimension(600, 600));
tree.insert(new Point(11, 3),new Dimension(10,10), "1");
tree.insert(new Point(22, 3),new Dimension(30,10), "2");
tree.insert(new Point(33, 3),new Dimension(40,10), "3");

BoundsQuadTree<String> tree = new BoundsQuadTree<String>(
new Point(0, 0), new Dimension(600, 600));
tree.insert(new Point(11, 3), new Dimension(10, 10), "1");
tree.insert(new Point(22, 3), new Dimension(30, 10), "2");
tree.insert(new Point(33, 3), new Dimension(40, 10), "3");


}

}
42 changes: 36 additions & 6 deletions src/test/java/at/jotschi/quadtree/RenderBoundsQuadTree.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package at.jotschi.quadtree;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Vector;

import at.jotschi.quadtree.gui.QuadTreePanel;

@SuppressWarnings("serial")
public class RenderBoundsQuadTree extends QuadTreePanel {

protected BoundsQuadTree<String> tree;

public static void main(String[] args) {
new RenderBoundsQuadTree();
}
Expand All @@ -17,19 +23,20 @@ protected RenderBoundsQuadTree() {
tree = createQuadTree();
setupGui();
}

/**
* Create the QuadTree and add some random points
*
* @return
*/
private QuadTree<String> createQuadTree() {
BoundsQuadTree<String> tree = new BoundsQuadTree<String>(new Point(100,
100), new Dimension(400, 400));
private BoundsQuadTree<String> createQuadTree() {
Point startCoordinates = new Point(100, 100);
BoundsQuadTree<String> tree = new BoundsQuadTree<String>(
startCoordinates, new Dimension(400, 400));

for (int i = 0; i < 200; i++) {
int x = (int) (Math.random() * 1000 * 0.4) + 100;
int y = (int) (Math.random() * 1000 * 0.4) + 100;
int x = (int) (Math.random() * 1000 * 0.3) + startCoordinates.x;
int y = (int) (Math.random() * 1000 * 0.3) + startCoordinates.y;
Point point = new Point(x, y);
Dimension dimension = new Dimension(50, 50);
tree.insert(point, dimension, "test");
Expand All @@ -38,4 +45,27 @@ private QuadTree<String> createQuadTree() {
return tree;
}

@Override
protected void paintComponent(Graphics g) {
BoundsNode<String> rootNode = tree.getRootNode();
drawCells(rootNode, g);
}


/**
* Draw all elements of the node
*
* @param g
*/
@Override
public void drawElements(Node node, Graphics g) {

Vector<NodeElement<String>> elements = (Vector<NodeElement<String>>) node
.getElements();
for (NodeElement<String> element : elements) {
g.drawRect((int) element.getX(), (int) element.getY(),
node.getBounds().width, node.getBounds().height);
}
}

}
3 changes: 3 additions & 0 deletions src/test/java/at/jotschi/quadtree/RenderQuadTree.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package at.jotschi.quadtree;

import at.jotschi.quadtree.gui.QuadTreePanel;



@SuppressWarnings("serial")
public class RenderQuadTree extends QuadTreePanel {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package at.jotschi.quadtree;
package at.jotschi.quadtree.gui;

import java.awt.Color;
import java.awt.Dimension;
Expand All @@ -16,12 +16,16 @@

import org.apache.log4j.Logger;

import at.jotschi.quadtree.Node;
import at.jotschi.quadtree.NodeElement;
import at.jotschi.quadtree.QuadTree;
import at.jotschi.quadtree.Node.Cell;

@SuppressWarnings("serial")
public class QuadTreePanel extends JPanel implements KeyListener, MouseListener {

protected QuadTree<String> tree;

protected Vector<NodeElement<String>> selectedElements = new Vector<NodeElement<String>>();

protected static Logger log = Logger.getLogger(QuadTreePanel.class);
Expand All @@ -41,10 +45,8 @@ protected void setupGui() {
f.addMouseListener(this);
f.add(this);
f.setLocationRelativeTo(null);

f.setSize(600, 600);
f.setVisible(true);

}

/**
Expand Down Expand Up @@ -78,7 +80,7 @@ protected void paintComponent(Graphics g) {
* @param node
* @param g
*/
private void drawCells(Node<String> node, Graphics g) {
protected void drawCells(Node<String> node, Graphics g) {

Dimension bounds = node.getBounds();
Point startCoordinates = node.getStartCoordinates();
Expand All @@ -93,20 +95,32 @@ private void drawCells(Node<String> node, Graphics g) {
}

// Draw points of this node
Vector<NodeElement<String>> elements = (Vector<NodeElement<String>>) node
.getElements();
for (NodeElement<String> element : elements) {
g.drawOval((int) element.getX(), (int) element.getY(), 4, 4);
}
drawElements(node, g);

// Draw selected elements
g.setColor(Color.RED);
drawSelectedElements(g);
}

public void drawSelectedElements(Graphics g) {
g.setColor(Color.RED);
for (NodeElement<String> element : selectedElements) {
g.drawOval((int) element.getX(), (int) element.getY(), 4, 4);
}
g.setColor(Color.BLACK);
}

/**
* Draw all elements of the node
*
* @param g
*/
public void drawElements(Node node, Graphics g) {

Vector<NodeElement<String>> elements = (Vector<NodeElement<String>>) node
.getElements();
for (NodeElement<String> element : elements) {
g.drawOval((int) element.getX(), (int) element.getY(), 4, 4);
}
}

public void keyTyped(KeyEvent e) {
Expand Down

0 comments on commit ec0475b

Please sign in to comment.