0

我正在使用 ArrayLists、抽象和多态性为学校创建一个破砖游戏。我创建了一个包含 draw 方法的抽象类 DrawableBrick。我已经成功地创建了填充我的 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,});
            }

        }
    }
4

3 回答 3

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
于 2013-03-04T04:26:30.870 回答
0

您可以直接分配参数数组,如下所示:

public ShavedBrick(int[] xArray, int[] yArray, int numberOfPoints)
    this.xArray = xArray;
    this.yArray = yArray;
    //...

int[] x {}实际上创建了一个新的空数组,这当然不是有效的参数声明。参数声明应如下所示int[] xArray

于 2013-03-04T04:27:42.650 回答
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);
    }
于 2013-03-04T04:29:50.473 回答