Hey,
Our instructions were rather easy...to make a recursive H tree that when finished, would look like whats in this link.
http://www.cs.usm.maine.edu/~macleod/courses/cos161/Spring2007/assign6.html
After writing a good bit of code of what I thought should work, I ran it and apparently a couple things are messing up but Im not sure what! I cant figure out whats going on. Obviously the math is not getting some of the top H's to print out or something...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tree extends JPanel
{
public Tree()
{
}
public void paintComponent(Graphics g)
{
paintHTree(g,256,256,256);
}
private void paintHTree(Graphics g, int xCent, int yCent, int size)
{
g.drawLine(xCent - size/2,yCent,xCent + size/2,yCent); //middle
g.drawLine(xCent - size/2,yCent - size/2,xCent - size/2,yCent + size/2); //left
g.drawLine(xCent + size/2,yCent - size/2,xCent + size/2,yCent + size/2); //right
int tx = xCent;
int ty = yCent;
int tsize = size;
int blx = xCent;
int bly = yCent;
int blsize = size;
int brx = xCent;
int bry = yCent;
int brsize = size;
if (size > 64)
{
xCent = xCent/2;
yCent = yCent/2;
size = size/2;
paintHTree(g, xCent, yCent, size);
tx = tx + tsize/2;
ty = ty/2;
tsize = tsize/2;
paintHTree(g, tx, ty, tsize);
brx = brx + brsize/2;
bry = bry + brsize/2;
brsize = brsize/2;
paintHTree(g, brx, bry, brsize);
blx = blx - blsize/2;
bly = bly + blsize/2;
blsize = blsize/2;
paintHTree(g, blx, bly, blsize);
}
The first and second step work fine, and I can get those if I change that if statement to either 128 or 256. But when I try to go to 64 in that if statement, not all of the H's show up...It looks like this...
http://i168.photobucket.com/albums/u166/bc2210/untitled.jpg
And then when I go to 32, it just keeps getting worse...But I notice the upper left quadrant of 32 is identical to the entire picture of the 64. Which is weird, idk...
http://i168.photobucket.com/albums/u166/bc2210/untitled1.jpg
Any suggestions? I tried playing with the math formulas but idk what to do next!
This is what my tester looks like:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TreeViewer extends JFrame
{
public TreeViewer()
{
Tree tree = new Tree();
add(tree);
}
public static void main(String[] args)
{
TreeViewer viewer = new TreeViewer();
viewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
viewer.setSize(512,620);
viewer.setTitle("Recursive Trees");
viewer.setVisible(true);
}
}