调用绘制在Java小程序上递归附加绘图

时间:2016-04-09 04:26:49

标签: java swing recursion applet repaint

我正在尝试对我的霍夫曼代码树进行图形表示。我有树的根节点,我写了一个printTree()函数来递归调用自身并在控制台中打印所有子树。 我创建了一个小程序,它绘制了树的根和它的孩子,但我无法弄清楚如何绘制其余部分,因为我无法通过参数递归调用绘制。有没有人知道如何递归绘画,同时保持旧绘图,所以我可以绘制树的其余部分。

 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);

这是我的绘画功能。我只想和所有孩子一起调用这个函数,直到我结束。

这是控制台版本。

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);
        }
    }

1 个答案:

答案 0 :(得分:1)

如果您的油漆是油漆,您可以创建自己的油漆paint2(Tree t, Graphics g)

然后在paint2 printTree中,if left paint2(left, g);

// app/models/category.js
import DS from 'ember-data';
export default DS.Model.extend({
    name: DS.attr('string'),
    parent: DS.belongsTo('category', { inverse: 'children' }),
    children: DS.hasMany('category', { embedded: 'always', async: false, inverse: 'parent' })
});

你必须测量长度等,因为现在你正在绘制2D空间。