[英]How can I make the following object?
我是Java新手。 因此,我正在嘗試制作小行星游戲項目。
class Point implements Cloneable {
double x,y;
public Point(double inX, double inY) { x = inX; y = inY; }
public Point clone() {
return new Point(x, y);
}
}
以及一個包含點,點和整數數組的面類。 類Polygon {private Point [] shape; //點數組。 公共點位置; //上面提到的偏移量。 公眾雙重輪換; //零度應歸東。
public Polygon(Point[] inShape, Point inPosition, double inRotation) {
shape = inShape;
position = inPosition;
rotation = inRotation;
// First, we find the shape's top-most left-most boundary, its origin.
Point origin = shape[0].clone();
for (Point p : shape) {
if (p.x < origin.x) origin.x = p.x;
if (p.y < origin.y) origin.y = p.y;
}
// Then, we orient all of its points relative to the real origin.
for (Point p : shape) {
p.x -= origin.x;
p.y -= origin.y;
}
}
在小行星的主要類中........... 我想制作一個多邊形,這就是我的船。 我如何在這里放置參數?
public static void main (String[] args) {
Asteroids a = new Asteroids();
a.repaint();
我想創建一個我嘗試過的多邊形對象
Polygon p=new polygon({(0,2),(2,3),(3,1)},(2,3),3);
p.repaint();
我沒有正確使用參數。 任何幫助將不勝感激。
在構造函數中,您需要創建一個Point
類型的數組來初始化新的Point
對象,如下所示:
new Point[] {new Point(0,2),new Point(2,3),new Point(3,1)}
然后定義一個新的Point
:
new Point(2,3)
因此,您的構造函數調用將如下所示:
Polygon p = new Polygon(
new Point[] {new Point(0,2),new Point(2,3),new Point(3,1)},
new Point(2,3),
3);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.