简体   繁体   English

多边形的参数化构造函数

[英]Parameterized Constructor for Polygon

I am creating a brick breaker game for school using ArrayLists, abstraction, and polymorphism. 我正在使用ArrayLists,抽象和多态性为学校创建一个打砖块游戏。 I have created an abstract class DrawableBrick that includes a draw method. 我创建了一个抽象类DrawableBrick,其中包含一个draw方法。 I have already successfully created the other subclasses that fill my ArrayList and the game works beautifully, but I need to create a new subclass called ShavedBrick that is a polygon that can be easily added to my ArrayList. 我已经成功创建了其他填充了ArrayList的子类,并且游戏运行良好,但是我需要创建一个名为ShavedBrick的新子类,该子类可以轻松添加到ArrayList中。

I am a little stuck as to how to create the parameterized constructor for this class and to set the class data to the arguments passed in by the user. 对于如何为此类创建参数化构造函数以及如何将类数据设置为用户传递的参数,我有些困惑。 Here is what I have so far. 这是我到目前为止所拥有的。 It needs to be an octagon. 它必须是一个八边形。

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
         }

Here is an example of creating the object in the ArrayList 这是在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,});
            }

        }
    }

i would suggest something like this... 我会建议这样的事情...

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);

        }

Basically, remove the braces ( { } ) from the constructor signature. 基本上,从构造函数签名中删除大括号( { } )。 And add each of the 8 elements in the input arrays to the private arrays of the class using a for loop. 并使用for循环将输入数组中的8个元素中的每一个添加到类的私有数组中。 You could point the private arrays of the class directly to the input arrays, but i would personally avoid that and simply copy the values to a newly created pair of arrays. 您可以将类的私有数组直接指向输入数组,但是我个人会避免这种情况,只需将值复制到一对新创建的数组中即可。

You could also use the Array.copyOf(int[], int) http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf(int[], int) as well. 您还可以使用Array.copyOf(int[], int) http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf(int [],int)作为好。

In your client code just run a loop to compute your co-ordinates and then pass those co-ordinates along to the constructor like this ... 在您的客户端代码中,只需运行一个循环来计算您的坐标,然后将这些坐标传递给这样的构造函数...

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

You can directly assign the argument arrays as follows: 您可以直接分配参数数组,如下所示:

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

int[] x {} actually creates a new empty array which of course is not a valid parameter declaration. int[] x {}实际上创建了一个新的空数组,该数组当然不是有效的参数声明。 Parameter declaration should look as int[] xArray . 参数声明应类似于int[] xArray

I'm not sure what your problem is, but if you have an octagon then you don't need a numberOfPoints argument: 我不确定您的问题是什么,但是如果您有一个八边形,则不需要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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM