我正在使用ArrayLists,抽象和多态来为学校创建一个破砖游戏。我创建了一个抽象类DrawableBrick,其中包含一个draw方法。我已经成功创建了填充我的ArrayList的其他子类,游戏运行得很漂亮,但我需要创建一个名为ShavedBrick的新子类,它是一个可以轻松添加到我的ArrayList的多边形。
我对如何为这个类创建参数化构造函数以及将类数据设置为用户传入的参数感到困惑。这是我到目前为止所拥有的。它需要是一个八角形。
import java.awt.*;
import java.util.*;
public class ShavedBrick extends DrawableBrick {
//data
private int [] xArray = new int [8];
private int [] yArray = new int [8];
private int numberOfSides;
//private Color color;
//constructor
public ShavedBrick(int[] x {}, int [] y {}, int numberOfPoints)
{
Random ranGen = new Random();
xArray[0] = x;
yArray[0] = y;
this.numberOfSides = numberOfPoints;
//this.width = width;
//this.height = height;
this.color = new Color(0,(ranGen.nextInt(156)+100),0);
}
//draw - tells the ShavedBrick to draw itself, using the Graphics object received
public void draw(Graphics g)
{
Color prevColor = g.getColor(); //save previous color associated with g
g.setColor(this.color);
g.fillPolygon(xArray, yArray, numberOfSides);
g.setColor(prevColor); //restore previous color
}
以下是在ArrayList
中创建对象的示例//some constants created in the main data
private final int WALLWIDTH = 5; //Walls' width
private final int BRICKSTARTX = WALLWIDTH;
private final int BRICKSTARTY = 100 + WALLWIDTH;
private final int BRICKWIDTH = 150;
private final int BRICKHEIGHT = 75;
//Fill the ArrayList with random DrawableBricks
for (int i = 0; i<10; i++){
Random random = new Random();
int randomBrick = random.nextInt(3);
if (randomBrick == 0){
myBricks.add(i, new RedBrick((BRICKSTARTX + i*BRICKWIDTH),(BRICKSTARTY + BRICKHEIGHT),BRICKWIDTH,BRICKHEIGHT));
myBricks.add(i, new RedBrick((BRICKSTARTX + i*BRICKWIDTH),((BRICKSTARTY+75) + BRICKHEIGHT),BRICKWIDTH,BRICKHEIGHT));
}
else if (randomBrick == 1) {
myBricks.add(i, new BlueBrick((BRICKSTARTX + i*BRICKWIDTH),(BRICKSTARTY + BRICKHEIGHT),BRICKWIDTH, BRICKHEIGHT));
myBricks.add(i, new BlueBrick((BRICKSTARTX + i*BRICKWIDTH),((BRICKSTARTY+75) + BRICKHEIGHT),BRICKWIDTH,BRICKHEIGHT));
}
//else if (randomBrick == 3){
//myBricks.add(new ShavedBrick(0,0,2,6));
//}
else if (randomBrick == 2){
for (int i = 0; i<8; i++){
xValues[] array = new xArray[8];
myBricks.add(new ShavedBrick(int [i] x {BRICKSTARTX,});
}
}
}
答案 0 :(得分:0)
我会建议像这样......
public ShavedBrick(int[] x, int[] y, int numberOfPoints)
{
Random ranGen = new Random();
for(int i = 0; i < 8; i ++) {
xArray[i] = x[i];
yArray[i] = y[i];
}
this.numberOfSides = numberOfPoints;
this.color = new Color(0,(ranGen.nextInt(156)+100),0);
}
基本上,从构造函数签名中删除大括号({
}
)。并使用for循环将输入数组中的8个元素中的每一个添加到类的私有数组中。您可以将类的私有数组直接指向输入数组,但我个人会避免这种情况,只需将值复制到新创建的数组对。
您也可以使用Array.copyOf(int[], int)
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf(int [],int)。
在你的客户端代码中运行一个循环来计算你的坐标,然后将这些坐标传递给构造函数,就像这样......
int[] xArray = new int[8];
int[] yArray = new int[8];
for (int i = 0; i < 8; i ++) {
xArray[i] = //some value for the x co-ordinate of the ith point
yArray[i] = //some value for the y co-ordinate of the ith point
}
ShavedBrick b = new ShavedBrick(xArray, yArray, 8);
myBricks.add(b); //finally add the new shaved brick
答案 1 :(得分:0)
您可以直接分配参数数组,如下所示:
public ShavedBrick(int[] xArray, int[] yArray, int numberOfPoints)
this.xArray = xArray;
this.yArray = yArray;
//...
int[] x {}
实际上创建了一个新的空数组,当然这不是一个有效的参数声明。参数声明应显示为int[] xArray
。
答案 2 :(得分:0)
我不确定你的问题是什么,但是如果你有一个八角形,那么你不需要numberOfPoints
参数:
public ShavedBrick(int[] x, int [] y)
{
Random ranGen = new Random();
this.numberOfSides = 8; //it's always 8
xArray = Arrays.copyOf(x,numberOfSides); //note, this method will pad array with zeros if x.length is less than 8
yArray = Arrays.copyOf(y,numberOfSides);
this.color = new Color(0,(ranGen.nextInt(156)+100),0);
}