2014-11-21 18 views
0

我想完成一個小行星項目。我有四個班。小行星,多邊形,遊戲和Asteriod。 我正在製作一艘新船,它是船的一個子類。我是新來的Java。我試圖製作一個新的貨物對象,但是我無法讓它出現在屏幕上。 https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html 但是,在船級中,我現在使用了fillPolygon,它現在顯示在屏幕上。但我知道寫的方式。我怎樣才能使船舶使用船對象,並使其出現。我正在下面提供我的課程。如何在我的小行星程序中顯示我的飛船?

package asteroids; 

/* 
CLASS: Asteroids 
DESCRIPTION: Extending Game, Asteroids is all in the paint method. 
NOTE: This class is the metaphorical "main method" of your program, 
    it is your control center. 
Original code by Dan Leyzberg and Art Simon 
*/ 
import java.awt.*; 
import java.awt.event.*; 

class Asteroids extends Game { 
    static int counter = 0; 

public Asteroids() { 
    super("Asteroids!",800,600); 
    this.setFocusable(true); 
    this.requestFocus(); 
} 

public void paint(Graphics brush) { 
    brush.setColor(Color.black); 
    brush.fillRect(0,0,width,height); 

    // sample code for printing message for debugging 
    // counter is incremented and this message printed 
    // each time the canvas is repainted 
    counter++; 
    brush.setColor(Color.white); 
    brush.drawString("Counter is " + counter,10,10); 
    ship p = new ship(
      new Point[] {new Point(1,1),new Point(3,1),new Point(1.5,2)}, 
      new Point(3,3), 
      0); 


    brush.setColor(Color.red); 
    p.paint(brush); 

} 

public static void main (String[] args) { 
    Asteroids a = new Asteroids(); 

    a.repaint(); 










    } 
} 

多邊形類

enter code herepackage asteroids; 

/* 

CLASS:多邊形 描述:多邊形是由一組這樣 點,偏移量,和旋轉限定在空間中的點的序列。偏移量是原點與形狀中心之間的距離。 旋轉以度數0-360度量。 用法:您打算使用一組點永久定義它的形狀來實例化此類,然後通過重新定位並旋轉該形狀對其進行修改。在定義形狀時,使用了您提供的點的相對位置 ,換句話說:{(0,1),(1,1),(1,0)} 與{(9, 10),(10,10),(10,9)}。

class Polygon { 
    private Point[] shape; // An array of points. 
    public Point position; // The offset mentioned above. 
    public double rotation; // Zero degrees is due east. 

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


    // "getPoints" applies the rotation and offset to the shape of the polygon. 
    public Point[] getPoints() { 
    Point center = findCenter(); 
    Point[] points = new Point[shape.length]; 
    for (int i = 0; i < shape.length; i++) { 
    for (Point p : shape) { 
     Point p = shape[i]; 
     double x = ((p.x-center.x) * Math.cos(Math.toRadians(rotation))) 
      - ((p.y-center.y) * Math.sin(Math.toRadians(rotation))) 
      + center.x/2 + position.x; 
    double y = ((p.x-center.x) * Math.sin(Math.toRadians(rotation))) 
      + ((p.y-center.y) * Math.cos(Math.toRadians(rotation))) 
      + center.y/2 + position.y; 
    points[i] = new Point(x,y); 
    } 
    return points; 
} 

    // "contains" implements some magical math (i.e. the ray-casting algorithm). 
public boolean contains(Point point) { 
    Point[] points = getPoints(); 
    double crossingNumber = 0; 
    for (int i = 0, j = 1; i < shape.length; i++, j=(j+1)%shape.length) { 
    if ((((points[i].x < point.x) && (point.x <= points[j].x)) || 
     ((points[j].x < point.x) && (point.x <= points[i].x))) && 
     (point.y > points[i].y + (points[j].y-points[i].y)/ 
     (points[j].x - points[i].x) * (point.x - points[i].x))) { 
    crossingNumber++; 
    } 
} 
return crossingNumber%2 == 1; 
} 

public void rotate(int degrees) {rotation = (rotation+degrees)%360;} 

/* 
The following methods are private access restricted because, as this access 
level always implies, they are intended for use only as helpers of the 
methods in this class that are not private. They can't be used anywhere else. 
*/ 

// "findArea" implements some more magic math. 
private double findArea() { 
    double sum = 0; 
    for (int i = 0, j = 1; i < shape.length; i++, j=(j+1)%shape.length) { 
    sum += shape[i].x*shape[j].y-shape[j].x*shape[i].y; 
    } 
    return Math.abs(sum/2); 
    } 

// "findCenter" implements another bit of math. 
private Point findCenter() { 
    Point sum = new Point(0,0); 
    for (int i = 0, j = 1; i < shape.length; i++, j=(j+1)%shape.length) { 
    sum.x += (shape[i].x + shape[j].x) 
      * (shape[i].x * shape[j].y - shape[j].x * shape[i].y); 
    sum.y += (shape[i].y + shape[j].y) 
      * (shape[i].x * shape[j].y - shape[j].x * shape[i].y); 
    } 
    double area = findArea(); 
    return new Point(Math.abs(sum.x/(6*area)),Math.abs(sum.y/(6*area))); 
} 

} 類船 包小行星;

import java.awt.Color; 
import java.awt.Graphics; 

public class ship extends Polygon{ 
    static int counter=0; 

    public ship(Point[] inShape, Point inPosition, double inRotation) { 
     super(inShape, inPosition, inRotation); 
     // TODO Auto-generated constructor stub 
    } 

    public void paint(Graphics brush) { 
     brush.fillPolygon(new int []{400,380,380,420,420,400},new int[] {300,320,360,360,320,300},6); 

} 


public void drawPolygon(Point[] points, Point point, int i) { 
    // TODO Auto-generated method stub 

} 

}

Any help will be appreciated ,thanks 

回答

0
小行星

應直接或間接地延伸的JFrame(通過遊戲)。

一旦你實例化你的類小行星,應延長JFrame的,這樣做:

asteroid.setVisible(true); 

的新JPanel就會出現,你的paint方法將油漆JPanel的畫布上。

相關問題