2016-06-30 8 views
2

하위 클래스 Flower가있는 기본 클래스 트리를 만들어야하는 과제를 진행하고 있습니다. 하지만 어디서 잘못했는지 모릅니다. 마우스를 누르면 꽃이 나무에 나타나지 않고 나무 만 나타납니다. 여기에 지금까지 내 코드,하위 클래스가 표시되지 않습니다.

Tree tree; 
ArrayList<Tree> treeList = new ArrayList<Tree>(); 

Flower flowers; 
ArrayList <Flower> flowerList = new ArrayList<Flower>(); 

void setup() { 
    size(800, 800); 
    tree = new Tree(mouseX, mouseY, HALF_PI, height/12); 
    flowers = new Flower(mouseX, mouseY, HALF_PI, height/12); 
} 

void draw() { 
    background(255); 

    //current tree 
    for (int j =0; j < treeList.size(); j++) 
    { 
    tree = treeList.get(j); 
    tree.drawTree(tree.xPos, tree.yPos, tree.rotation, tree.tall); 
    } 

    //current flower 
    for(int i = 0; i < flowerList.size(); i ++){ 
    flowers = flowerList.get(i); 
    flowers.drawFlower(); 
    } 
drawMouseTree(mouseX, mouseY, HALF_PI, height/12); 

} 

void mousePressed() { 
    treeList.add(new Tree(mouseX, mouseY, HALF_PI, height/12)); 
    flowerList.add(new Flower(mouseX, mouseY, HALF_PI, height/12)); 
} 

void drawMouseTree(float xPos, float yPos, float rotation, float tall) { 
    //growing branch 
    float endX = xPos - tall * cos(rotation); 
    float endY = yPos - tall * sin(rotation); 

    //draw a tree 
    stroke(0); 
    strokeWeight(2); 
    line(xPos, yPos, endX, endY); 

    //create 2 branches 
    if (tall > 5) { 
    drawMouseTree(endX, endY, rotation - PI/5, tall * 0.7); //left 
    drawMouseTree(endX, endY, rotation + PI/5, tall * 0.7); //right 
    } 

    //create flowers each branch 
    if (tall > 5) { 

    stroke(255,102,178); 
    fill(255, 102, 178); 
    ellipse(endX, endY, 5, 5); 
    } 
} 


class Tree { 
    float xPos, yPos; 
    float rotation; 
    float tall, endX, endY; 

    Tree(float xPos, float yPos, float rotation, float tall) { 
    this.xPos = xPos; 
    this.yPos = yPos; 
    this.rotation = rotation; 
    this.tall = tall; 
    } 

    void drawTree(float xPos, float yPos, float rotation, float tall) { 
    //end of a branch 
    float endX = xPos - tall * cos(rotation); 
    float endY = yPos - tall * sin(rotation); 

    //draw a tree 
    stroke(0); 
    strokeWeight(2); 
    line(xPos, yPos, endX, endY); 

    //create 2 branches 
    if (tall > 5) { 
     drawTree(endX, endY, rotation - PI/5, tall * 0.7); //left 
     drawTree(endX, endY, rotation + PI/5, tall * 0.7); //right 
    } 
    } 
} 

class Flower extends Tree { 

    Flower(float xPos, float yPos, float rotation, float tall) { 
    super(xPos, yPos, rotation, tall); 
    } 


    void drawFlower() { 
    super.drawTree(xPos, yPos, rotation, tall); 

    //create flowers each branch 
    if (tall < 40 && tall > 5) { //so the flowers will appear around top of tree 


     stroke(255, 102, 178); 
     fill(255, 102, 178); 
     ellipse(endX, endY, 5, 5); 
    } 
    } 
} 

내가 여전히 슈퍼 자식 클래스 개념에 새로 온 사람이다. 이 문제를 해결할 수있는 도움을 주시면 매우 감사하겠습니다!

편집 : drawFlower()에서 endX, endY를 사용하는 실수를 확인하십시오.

class Flower extends Tree { 

    Flower(float xPos, float yPos, float rotation, float tall) { 
    super(xPos, yPos, rotation, tall); 
    } 

    void drawFlower() { 
    super.drawTree(xPos, yPos, rotation, tall); 
    //create flowers each branch 

    if (tall > 5) { 
     //draw flower 

     stroke(255, 102, 178); 
     fill(255, 102, 178); 
     ellipse(finX, finY, 5, 5); 

    } 
    } 
} 
class Tree { 
    float xPos, yPos; 
    float rotation; 
    float tall, finX, finY; 

    Tree(float xPos, float yPos, float rotation, float tall) { 
    this.xPos = xPos; 
    this.yPos = yPos; 
    this.rotation = rotation; 
    this.tall = tall; 
    } 

    void drawTree(float xPos, float yPos, float rotation, float tall) { 
    //end of a branch 
    float endX = xPos - tall * cos(rotation); 
    float endY = yPos - tall * sin(rotation); 
    finX = endX; 
    finY = endY; 

    //draw a tree 
    stroke(0); 
    strokeWeight(2); 
    line(xPos, yPos, endX, endY); //branch 

    //create 2 branches 
    if (tall > 5) { 

     drawTree(endX, endY, rotation - PI/5, tall * 0.7); //left 
     drawTree(endX, endY, rotation + PI/5, tall * 0.7); //right 
    } 
    } 
} 

그러나, 결과는 다음과 같습니다 : http://i.imgur.com/gNLeKsB.png각 끝 지점 트리의에 꽃을 가지고 어쨌든 여기에 새로운 코드는? 이처럼 는 : http://i.imgur.com/wz2iNgP.png

답변

0

창문의 height800입니다.

Tree 또는 Flower 생성자로 전달하는 마지막 매개 변수는 height/12입니다.

800/1266.6666...이며, 66으로 잘립니다.

즉, tall 변수의 값은 66입니다. 그런 다음이 if 성명에서 그 변수를 사용 66하지 미만 40 때문에

if (tall < 40 && tall > 5) { 

if 한 Statment는 true로 평가하지 않았다.

당신은 쉽게 간단하게 그 권리를 if 문 앞에 tall의 값을 인쇄하여 자신을 테스트 할 수 있습니다

println(tall); 

당신이 if 문 밖으로, 당신은 여전히 ​​꽃을 볼 수 없습니다 것을 언급하더라도. 다시, 구조에 println() 기능 :

stroke(255, 102, 178); 
    fill(255, 102, 178); 
    println(endX + ", " + endY); //prints 0.0, 0.0 
    ellipse(endX, endY, 50, 50); 

endXendY의 값은 항상 0이 시점에서입니다. 그리고 그것은 당신이 그들을 어떻게 사용하는지에 대해 이해합니다.

나는 당신이 여기서 무엇을할지는 알지만, 코드의이 시점에서 당신은 실제로 가지의 끝 위치에 접근 할 수 없다.

내가 너라면, 훨씬 더 간단하게 시작할 것입니다. 재귀 함수를 사용하여 나무를 그리는 대신 상단에 꽃이있는 한 줄로 시작한 다음 거기에서부터 시작하십시오.

+0

내 질문에 답변 해 주셔서 감사합니다. 필요한 하위 클래스가있는 재귀 트리 여야한다고 언급 했어야합니다. – Tracey

+0

@ Tracey 그것이 당신의 요구 사항이라 할지라도 나는 더 간단하게 시작하는 것이 좋습니다. 문제는 코드를 아직 이해하지 못해서 생기는 것이므로 더 쉽게 시작할 수 있습니다. 곧은 나무의 끝에 꽃을 보여주는보다 기본적인 스케치를 작성한 다음 거기에서부터 작업하십시오. –