子类不显示处理
Subclass doesn't display Processing
我正在做一项作业,需要创建一棵基础 class 树和子 class 花。
但我不知道我在哪里弄错了,按下鼠标时树上没有出现花,只有树出现了。到目前为止,这是我的代码,
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);
}
}
}
我对 super-child class 概念还是陌生的。非常感谢任何解决此问题的帮助!
编辑:
找出我在 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
你的window的height
是800
。
您传递给 Tree
或 Flower
构造函数的最后一个参数是 height/12
.
800/12
是 66.6666...
,它被截断为 66
。
换句话说,tall
变量保存值 66
。然后,您在此 if
语句中使用该变量:
if (tall < 40 && tall > 5) {
此 if
语句永远不会计算为真,因为 66
不 小于 40
.
您可以轻松地自行测试,只需在 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);
此时endX
和endY
的值总是0
。这对您如何使用它们很有意义。
我明白你的意思了,但此时在代码中你实际上无法访问分支的结束位置。
如果我是你,我会从简单的开始。不要使用递归函数来绘制树,而是从顶部有一朵花的单行开始,然后从那里开始。
我正在做一项作业,需要创建一棵基础 class 树和子 class 花。 但我不知道我在哪里弄错了,按下鼠标时树上没有出现花,只有树出现了。到目前为止,这是我的代码,
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);
}
}
}
我对 super-child class 概念还是陌生的。非常感谢任何解决此问题的帮助!
编辑: 找出我在 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
你的window的height
是800
。
您传递给 Tree
或 Flower
构造函数的最后一个参数是 height/12
.
800/12
是 66.6666...
,它被截断为 66
。
换句话说,tall
变量保存值 66
。然后,您在此 if
语句中使用该变量:
if (tall < 40 && tall > 5) {
此 if
语句永远不会计算为真,因为 66
不 小于 40
.
您可以轻松地自行测试,只需在 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);
此时endX
和endY
的值总是0
。这对您如何使用它们很有意义。
我明白你的意思了,但此时在代码中你实际上无法访问分支的结束位置。
如果我是你,我会从简单的开始。不要使用递归函数来绘制树,而是从顶部有一朵花的单行开始,然后从那里开始。