Daniel Dil
Daniel Dil

Reputation: 51

Call paint recursively appending drawings on a java applet

I am attempting to make a graphical representation of my Huffman Code tree. I have the tree's root node and I wrote a printTree() function to recursively call itself and print all the subtrees in the console. I made an applet that draws the root of the tree and it's children but I can't figure out how to draw the rest because I can't call paint recursively with parameters. Does anyone know how to go about painting recursively while keeping the old drawing there so I can draw the rest of the tree.

 System.out.println("Paint");
        g.setColor(Color.black);
        g.drawRect(0, 0, getSize().width -1,getSize().height -1);
        g.setColor(Color.red);
        g.drawString(text, 15, 25);
        g.setColor(Color.white);
        //left line
        g.drawLine(ovalCenterx,ovalCentery,leftOvalx+30,leftOvaly);
        //right line
        g.drawLine(ovalCenterx,ovalCentery,rightOvalx+19,rightOvaly);
        g.setColor(Color.green);
        //draw left circle
        g.fillOval(leftOvalx,leftOvaly, ovalSize, ovalSize);
        //draw right circle
        g.fillOval(rightOvalx,rightOvaly, ovalSize, ovalSize);
        //draw the root
        g.fillOval(ovalX, ovalY, ovalSize, ovalSize);
        g.setColor(Color.white);
        g.setColor(Color.black);
        //draw root label
        g.drawChars(label,0,label.length,ovalX+ovalP,ovalY+ovalP);          
        if(labelleft != null)
        g.drawChars(labelleft,0,labelleft.length,leftOvalx+ovalP,leftOvaly+ovalP);
        if(labelright != null)
        g.drawChars(labelright,0,labelright.length,rightOvalx+ovalP,rightOvaly+ovalP);
        if(left){
            setRootCoord(leftOvalx,leftOvaly);
        }
        if(right){
            setRootCoord(rightOvalx,leftOvaly);

this is my paint function. I just want to call this function with all the children until I reach the end.

Here is the console version.

public void printTree(int counter){
        counter++;
//      System.out.println(this.right + " is this.right");
//      System.out.println(this.left.data.prob);
//      System.out.println(this.right.data.prob);
        if(this.left.data.prob == -1 && this.right.data.prob == -1){
            if(counter > 1)
            return;
            this.left.printTree(counter);
            this.right.printTree(counter);
        }
        if(this.left.data.prob == -1.0 && this.right.data.prob != -1.0){
            System.out.println(this + "my left is -1");
            this.left.printTree(counter);
        }
        if(this.right.data.prob == -1.0 && this.left.data.prob != -1.0){
            System.out.println(this + "my right is -1");
            this.right.printTree(counter);
        }
        if(this.right.data.prob != -1.0 && this.left.data.prob != -1.0){
            System.out.println(this);
        }
    }

Upvotes: 1

Views: 110

Answers (1)

gpasch
gpasch

Reputation: 2682

If your paint is paint you create your own paint2(Tree t, Graphics g).

Then in paint2 you do like in printTree:

if left paint2(left, g);

You have to measure the lengths etc though because now you are drawing on 2D space.

Upvotes: 1

Related Questions