2016-06-30 5 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文でその変数を使用します。

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

6640未満ないであるため、このifなステートメントは、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それがあなたの要求であっても、私はまだ簡単に始めることを勧めます。あなたの問題は、まだコードを理解していないことに由来しているので、あなたができる最良のことはより簡単に始めることです。まっすぐな木の終わりに花を見せてくれる、より基本的なスケッチを作ってみましょう。 –

関連する問題