2013-11-15 85 views
0

我對Java編程非常陌生。我目前正在嘗試製作一款遊戲,其中白球不斷出現在屏幕上,並且用戶必須單擊並將球拖出到畫布的右側以使其消失。我還沒有完成編寫代碼,但是我的代碼不斷崩潰我的Java程序。任何人都可以告訴我我的代碼有什麼問題,爲什麼我的程序不斷崩潰?謝謝!使對象出現在點擊

public class BubbleGame extends GraphicsProgram 
{ 

//~ Instance/static variables 


private GRect field; 
private GRect goal; 
private GObject gobj;   /* The object being dragged */ 
private GPoint last;   /* The last mouse position */ 

private RandomGenerator rgen = new RandomGenerator(); 

//~ Constructor ........................................................... 

// ---------------------------------------------------------- 
/** 
* Creates a new BubbleGame object. 
*/ 
public void init() 
{ 
    //call method to create regions 
    CreateRegions(); 

    //add mouse listeners 
    addMouseListeners(); 

    //loop to add bubbles 
    while (true) 
    { 
     //create a filled bubble 
     GOval oval = new GOval (100, 100, 50, 50); 
     oval.setFilled(true); 
     oval.setColor(Color.WHITE); 
     add(oval); 

     //randomly generate coordinates within the field 


     //add the bubble and pause 



    } 
} 


//~ Methods ............................................................... 
public void CreateRegions(){ 

    //create and add the field with the size and color of your choice 
    field = new GRect(0, 0, getWidth() * .75, getHeight()); 
    field.setFilled(true); 
    field.setColor(Color.GREEN); 
    add(field); 

    //create and add the adjacent goal with the size and color of your choice 
    goal = new GRect(493, 0, getWidth() * .25, getHeight()); 
    goal.setFilled(true); 
    goal.setColor(Color.BLACK); 
    add(goal); 
    } 



/* Called on mouse press to record the coordinates of the click */ 
public void mousePressed(MouseEvent e) { 
    last = new GPoint(e.getPoint()); 
    gobj = getElementAt(last); 
    //later add check that not dragging field or goal 

} 

/* Called on mouse drag to reposition the object */ 
public void mouseDragged(MouseEvent e) { 
    if (gobj != null) { 
     gobj.move(e.getX() - last.getX(), e.getY() - last.getY()); 
     last = new GPoint(e.getPoint()); 
    }  

} 

/* Called on mouse drag to reposition the object */ 
public void mouseReleased(MouseEvent e) { 
    //if gobj has a value and its coordinates are contained by goal, make it invisible 




} 


} 
+0

「It's crashing」根本不能幫助你知道。什麼是錯誤信息? – Kayaman

+0

線程「main」中的異常java.lang.OutOfMemoryError:Java堆空間 線程「AWT-EventQueue-0」中的異常java.lang.OutOfMemoryError:Java堆空間 –

回答

1

有可能你的while循環是它崩潰的原因。你可以考慮把睡眠這裏,讓您的應用程序將不會使用CPU不斷:

while (true) 
    { 
     //create a filled bubble 
     GOval oval = new GOval (100, 100, 50, 50); 
     oval.setFilled(true); 
     oval.setColor(Color.WHITE); 
     add(oval); 

     //randomly generate coordinates within the field 


     //add the bubble and pause 


     Thread.sleep(100); 
    } 

而且要添加GOval不斷的對象。我不知道GraphicsProgram類,但它也可能導致你的內存填滿某些點。

0

通過執行以下操作,您正在特定位置繪製一個圓。

GOval oval = new GOval (100, 100, 50, 50); 

while (true)將重新執行該代碼,它已經完成後,立即和繼續永遠這樣做。這會填滿你的記憶並最終導致崩潰。

但是,除此之外,它是毫無意義的,因爲用戶甚至不會看到單獨的圈子:它們被繪製在彼此之上。

如果這確實是您正在尋找的,那麼我會建議添加一個Integer來計算當前屏幕中的圓圈數。這樣,你會添加到當前While -loop結束:

while(counter > 2) { 
    Thread.sleep(100); 
} 

這將保持在畫布恰好兩個圓在任何時候,用〜100毫秒的最大延遲。

您的IDE會提醒您應該捕獲的異常。

可能會更有意義什麼都編程和功能,可以是:

  • 添加代碼來繪製一個新圓的舊圈的情況下被刪除,並繪製在程序初始化只有一個圓圈。或者,
  • While -loop更改爲For -loop,例如運行10次。然後添加代碼來隨機化每個圓的位置。

Random randomGenerator = new Random();

for(int i=0;i<10;i++) { 
    int pos1 = randomGenerator.nextInt(100); // random between 0 and 99 
    int pos2 = randomGenerator.nextInt(100); // random between 0 and 99 
    int pos3 = randomGenerator.nextInt(100); // random between 0 and 99 
    int pos4 = randomGenerator.nextInt(100); // random between 0 and 99 
    GOval oval = new GOval (pos1,pos2,pos3,pos4); 
} 
0

把你GOval出while循環。保持它在那裏使它再生,因此崩潰。