未绘制对象的问题 - 处理

时间:2014-05-07 18:28:23

标签: tree processing processing.js

查看文档,看起来像noFill();和noStroke();我应该打电话给“取消”我的对象。现在,点击,我正在显示我的树的子节点,但在另一次点击父节点时,我希望这些孩子崩溃并“取消”。谁能让我知道这里可能出了什么问题?非常感激。在javascript中运行。

MAIN SKETCH:

//SFD Tree Visualization
//Austin Slominski 2014

Node root;
Node current;
XML data;

void setup(){
    root = new Node();
    data = loadXML("nodes.xml");

    size(750,750);
    smooth(true);
    background(255);

    loadChild(root, data, null);
    current = root;
}

void loadChild(Node node, XML data, Node parent) {
    XML title = data.getChild("title");
    String xmlTitle = title.getContent();
    node.title = xmlTitle;

    XML[] XMLchildren = data.getChildren("child");
    node.children = new Node[XMLchildren.length];

    for(int i=0;i<XMLchildren.length;i++){
      node.children[i] = new Node();
    }

    //DEBUG
        println("Current Node: " + node.title);
      if(parent!=null){
        println("Parent: " + parent.title);
        node.parent = parent;
      }
      if(node.children!=null){
          println("Number of Children:" + XMLchildren.length);
      }
        println("");

    for (int i = 0; i < XMLchildren.length; i++) {
      loadChild(node.children[i], XMLchildren[i], node);
    }
}

void draw(){
    current.display();//starts inactive, null collapsed

    if(current.collapsed == false) {
          for (int i = 0; i < current.children.length; i++) {
        current.children[i].display();
    }
    }

}

void mousePressed(){
    current.clickedActive();

    for (int i = 0; i < current.children.length; i++) {
      current.children[i].clickedActive();
    }
}

NODE CLASS:

/* <a rel="nofollow" href="/two/profile/pjs">@pjs</a> font="Junction-light.otf"; */
PFont f;

class Node {

      Node parent;
      Node[] children;
      public String title;

      int x=120;
      int y=140;
      int tWidth;
      int tHeight;
      int titleColor = 0;
      float textSize=70;
      public boolean active = false;
      public boolean collapsed = true;

    Node(){
      f = createFont("Junction-light.otf",100,true);
      textFont(f,textSize);  
    }

    void display(){
      if(parent == null) {
        fill(titleColor);
        text(title,x,y);
        noFill();
      } else {
        if(parent.collapsed == true){
          noFill();
          noStroke();
          text(title,x,y);
        }else{
          fill(titleColor);
          text(title,x,y);
          noFill();
        }
      }


      tHeight = textAscent();
      tWidth  = textWidth(title)+8;
      //rect(x,y-tHeight,tWidth,tHeight);
    }

    void clickedActive(){
      if(mouseX > x && mouseX < x+tWidth && mouseY > y-tHeight && mouseY < y){

         if(active == false){      //if already inactive (default)
           titleColor = 140;       //make gray
           active = true;          //set active
           collapsed = false;
           expandChildren();
         }else if(active == true){ //if already active
           titleColor = 0;         //make black
           active = false;         //set inactive
           collapsed = true;
           collapseChildren();
         }

      }
    }

    void expandChildren(){
       for (int i = 0; i < children.length; i++) {
         //this.children[i].collapsed = false;
         this.children[i].y = y + 150 + 150*i;
         println("expand");
       }
    }

    void collapseChildren(){
       for (int i = 0; i < children.length; i++) {
         //this.children[i].collapsed = true;
         println("collapse");
       }
    }

}

NODES.XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>

    <type></type>
    <title>ROOT</title>
    <description></description>
    <image></image>

    <child>
        <type></type>
        <title>Test Child 1</title>
        <description></description>
        <image></image>

        <child>
            <type></type>
            <title>Test Child 1b</title>
            <description></description>
            <image></image>
        </child>
    </child>

    <child>
        <type></type>
        <title>Test Child 2</title>
        <description></description>
        <image></image>

        <child>
            <type></type>
            <title>Test Child 2b</title>
            <description></description>
            <image></image>
        </child>
    </child>

</root>

1 个答案:

答案 0 :(得分:1)

在绘制任何内容之前,您需要添加background()(或rect()image(),其大小等于窗口的尺寸),以便过去的帧得到& #34;隐藏&#34;新的背后。

像这样:

void draw() {
  background(0); // will set the background to black
  current.display(); //starts inactive, null collapsed
  if (current.collapsed == false) {
    for (int i = 0; i < current.children.length; i++) {
      current.children[i].display();
    }
  }
}