2016-01-02 27 views
-1

確定,所以我已創建將充當移動傳球,反彈的牆壁和其他NanOrgs碰撞在Java其它目的

一個

NanOrg

對象此程序`公開課NanOrg擴展Ellipse2D.Float {

public int x_speed, y_speed; 
private int d; 
private int width = MainWindow.WIDTH; 
private int height = MainWindow.HEIGHT; 
private ArrayList<NanOrg> nanorgs; 
Rectangle2D r = new Rectangle2D.Float(super.x, super.y, d, d); 

public NanOrg(int diameter, ArrayList<NanOrg> nanorgs){ 
    super((int) (Math.random() * (MainWindow.WIDTH - 20) + 1), (int) (Math.random() * (MainWindow.HEIGHT - 20) + 1), diameter, diameter); 
    this.d = diameter; 

    this.x_speed = (int) (Math.random() * 5 + 1); 
    this.y_speed = (int) (Math.random() * 5 + 1); 
    this.nanorgs = nanorgs; 

} 
public void move(){ 
    for(NanOrg nanorg : nanorgs){ 
     if(nanorg != this && nanorg.intersects(r)){ 
      int tempx = x_speed; 
      int tempy = y_speed; 
      x_speed = nanorg.x_speed; 
      y_speed = nanorg.y_speed; 
      nanorg.x_speed = tempx; 
      nanorg.y_speed = tempy; 
      break; 
     } 
    } 
    if(super.x < 0){ 
     super.x = 0; 
     x_speed = Math.abs(x_speed); 
    } 
    else if(super.x > width - d){ 
     super.x = width - d; 
     x_speed = -Math.abs(x_speed); 
    } 
    if(super.y < 0){ 
     super.y = 0; 
     y_speed = Math.abs(y_speed); 
    } 
    else if(super.y > height - d){ 
     super.y = height - d; 
     y_speed = -Math.abs(y_speed); 
    } 
    super.x += x_speed; 
    super.y += y_speed; 
} 

}`an D,它代表的油動不動液滴的

對象。

public class Oil extends Ellipse2D.Float { 

private int width = MainWindow.WIDTH; 
private int height = MainWindow.HEIGHT; 
private int d; 
private ArrayList<Oil> oils; 
Rectangle2D rect = new Rectangle2D.Float(super.x, super.y, d, d); 

public Oil(int diameter){ 
    super((int) (Math.random() * (MainWindow.WIDTH - 20) + 1), (int) (Math.random() * (MainWindow.HEIGHT - 20) + 1), diameter, diameter); 
    this.d = diameter; 
} 

}

的NanOrgs在隨機位置產卵,並在隨機的方向和速度移動。 NanOrg能夠彼此反彈,但我需要幫助弄清楚如何處理油和NanOrg之間的碰撞,特別是NanOrg可以通過去除油來「吃」油。我有兩個其他類:

主窗口和PaintSurface

MainWindow類創建窗口爲JApplet的。

'公共類主窗口擴展JApplet的{

public static final int WIDTH = 500; 
public static final int HEIGHT = 500; 
private PaintSurface canvas; 

public void init(){ 
    this.setSize(WIDTH, HEIGHT); 
    this.setName("NanOrg Program"); 
    canvas = new PaintSurface(); 
    this.add(canvas, BorderLayout.CENTER); 
    Timer t = new Timer(20, e -> {canvas.repaint();}); 
    t.start(); 
     } 

} The PaintSurface Class "draws" the Oil and NanOrgs公共類PaintSurface擴展JComponent的{

public ArrayList<NanOrg> nanorgs = new ArrayList<NanOrg>(); 
public ArrayList<Oil> oils = new ArrayList<Oil>(); 

public PaintSurface(){ 
    for(int i = 0; i < 5; i++) 
     nanorgs.add(new NanOrg(10, nanorgs)); 

    for(int o = 0; o < 30; o++) 
     oils.add(new Oil(10)); 
} 

public void paint(Graphics g){ 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setColor(Color.MAGENTA); 


    for(NanOrg nanorg : nanorgs){ 
     nanorg.move(); 
     g2.fill(nanorg); 

    } 
    for(Oil oil : oils){ 
     g2.setColor(Color.BLACK); 
     g2.fill(oil); 
    } 

} 

}`

這是我的計劃,所以只需回顧一下:我有創造它,所以NanOrgs互相反彈和牆壁,但我需要幫助我需要編寫的NanOrg「吃」石油(如刪除它)和在哪裏放置刪除石油的代碼。我在NanOrg Class中嘗試過 public void eatOil(){ for(Oil oil : oils){ if(oil.intersects(r)){ oils.remove(oil); } } } ,然後在我的PaintSurface類中調用該方法,但是當我運行它時,出現NullPointerException。然後,我嘗試在Oil和PaintSurface類中執行相同的代碼,但是我無法讓這兩個類中的任何一個使用「r」作爲NanOrg中定義的變量。所以如果你能幫我弄清楚怎麼做會很棒,或者告訴我如果我做錯了什麼,該怎麼做。如果您有任何疑問可以嘗試澄清ypur知識,請詢問我將盡我所能回答。

+1

縮小代碼並更具體 –

+0

在for(E e:list)中調用正在被集成的ArrayList上的remove是不好的。考慮將無糖水滴複製到新的列表中,以取代舊的。 – laune

+0

顯示堆棧跟蹤。 – DV88

回答

0

您必須在NanOrgs中創建一個方法來檢測給定的NanOrg和給定的油是否相互交叉。您需要維護Oil類中的Oil實例列表,就像NanOrg類中的NanOrgs列表一樣。

然後在每次調用NanOrg()的調用時,都必須檢查NanOrg是否與Oil列表中的任何油滴相交。如果發現它們相交,則必須從Oil中的列表中移除Oil。

就是這樣。