I am creating a program for a school project where the user can create a geometrical shape (which is supposed to look like a horse) using buttons which control the size and color of: One Ellipse, one Circle, two Rectangles and one String (as a title for the figure). To save the figure I want an ArrayList where all of these are included, so I created a class which looks like this.
我正在为学校项目创建一个程序,用户可以使用按钮来创建几何形状(看起来像马),控制尺寸和颜色:一个椭圆,一个圆,两个矩形和一个字符串(如该图的标题)。为了保存图形,我想要一个包含所有这些的ArrayList,所以我创建了一个类似于此的类。
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
public class WiLi_Horse{
Ellipse body;
Rectangle leg1;
Rectangle leg2;
Circle head;
String name;
public WiLi_Horse(Ellipse bdy,
Rectangle lg1, Rectangle lg2,
Circle hd, String nme) {
body = bdy;
leg1 = lg1;
leg2 = lg2;
head = hd;
name = nme;
}
}
Now, when I create the ArrayList I write the following (in another class):
现在,当我创建ArrayList时,我编写了以下内容(在另一个类中):
ArrayList<WiLi_Horse> horseList = new ArrayList<>();
I then want to add a 'horse' in the ArrayList, so I begin with typing:
然后我想在ArrayList中添加一个'horse',所以我先输入:
horseList.add(new WiLi_Horse(XXX);
Where it says 'XXX' is the problem in hand. I don't know what to write from here on. Eclipse's quick-fix is to add null, null, null, null, null, but that is ofcourse not what I want.
它说'XXX'就是问题所在。我不知道从这里写什么。 Eclipse的快速修复是添加null,null,null,null,null,但这不是我想要的。
Thank you in advance.
先感谢您。
1 个解决方案
#1
May be you can try a List
of Shape
which will accept all of them:
也许你可以尝试接受所有这些的形状列表:
List<Shape> shapes = new ArrayList<Shape>();
Now you can add whatever shape you want to add:
现在您可以添加要添加的任何形状:
shapes.add(body);
shapes.add(leg1);
shapes.add(leg2);
etc. Here is the complete example which you can test:
以下是您可以测试的完整示例:
public class WiLi_Horse {
Ellipse body;
Rectangle leg1;
Rectangle leg2;
Circle head;
String name;
public WiLi_Horse(Ellipse body, Rectangle leg1, Rectangle leg2, Circle head, String name) {
this.body = body;
this.leg1 = leg1;
this.leg2 = leg2;
this.head = head;
this.name = name;
}
public List<Shape> getHorse()
{
List<Shape> shapes = new ArrayList<Shape>();
shapes.add(body);
shapes.add(leg1);
shapes.add(leg2);
shapes.add(head);
return shapes;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "WiLi_Horse{" +
"body=" + body +
", leg1=" + leg1 +
", leg2=" + leg2 +
", head=" + head +
", name='" + name + '\'' +
'}';
}
}
Now you can test it as:
现在你可以测试它:
public static void main(String[] args) {
Ellipse ellipse = new Ellipse(10,20);
Rectangle rect1 = new Rectangle(40,20);
Rectangle rect2 = new Rectangle(40,20);
Circle circle = new Circle(5);
String name = "Horse";
WiLi_Horse horse1 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
WiLi_Horse horse2 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
WiLi_Horse horse3 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
WiLi_Horse horse4 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
List<WiLi_Horse> horseList = new ArrayList<>();
horseList.add(horse1);
horseList.add(horse2);
horseList.add(horse3);
horseList.add(horse4);
System.out.println(horseList);
}
#1
May be you can try a List
of Shape
which will accept all of them:
也许你可以尝试接受所有这些的形状列表:
List<Shape> shapes = new ArrayList<Shape>();
Now you can add whatever shape you want to add:
现在您可以添加要添加的任何形状:
shapes.add(body);
shapes.add(leg1);
shapes.add(leg2);
etc. Here is the complete example which you can test:
以下是您可以测试的完整示例:
public class WiLi_Horse {
Ellipse body;
Rectangle leg1;
Rectangle leg2;
Circle head;
String name;
public WiLi_Horse(Ellipse body, Rectangle leg1, Rectangle leg2, Circle head, String name) {
this.body = body;
this.leg1 = leg1;
this.leg2 = leg2;
this.head = head;
this.name = name;
}
public List<Shape> getHorse()
{
List<Shape> shapes = new ArrayList<Shape>();
shapes.add(body);
shapes.add(leg1);
shapes.add(leg2);
shapes.add(head);
return shapes;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "WiLi_Horse{" +
"body=" + body +
", leg1=" + leg1 +
", leg2=" + leg2 +
", head=" + head +
", name='" + name + '\'' +
'}';
}
}
Now you can test it as:
现在你可以测试它:
public static void main(String[] args) {
Ellipse ellipse = new Ellipse(10,20);
Rectangle rect1 = new Rectangle(40,20);
Rectangle rect2 = new Rectangle(40,20);
Circle circle = new Circle(5);
String name = "Horse";
WiLi_Horse horse1 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
WiLi_Horse horse2 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
WiLi_Horse horse3 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
WiLi_Horse horse4 = new WiLi_Horse(ellipse,rect1,rect2,circle,name);
List<WiLi_Horse> horseList = new ArrayList<>();
horseList.add(horse1);
horseList.add(horse2);
horseList.add(horse3);
horseList.add(horse4);
System.out.println(horseList);
}