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  




}


}
4

3 に答える 3

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 クラスについてはわかりませんが、ある時点でメモリがいっぱいになる可能性もあります。

于 2013-11-15T07:13:55.523 に答える
0

以下を実行すると、特定の場所に円が描画されます。

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

しかしwhile (true)、終了直後にこのコードを再実行し、これを永遠に続けます。これによりメモリがいっぱいになり、最終的にクラッシュが発生します。

しかし、それ以外に、ユーザーは別々の円を見ることさえできないので無意味です: それらは互いの上に描かれています.

それが実際に探しているものである場合は、Integer現在画面にある円の数をカウントする を追加することをお勧めします。While次に、現在のループの最後に追加します。

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

これにより、キャンバス内に常に正確に 2 つの円が保持され、最大遅延は ~100 ミリ秒になります。

IDE は、おそらくキャッチする必要がある例外を通知します。

プログラム的にも機能的にもより理にかなっているのは、次のいずれかです。

  • 古い円が削除されるイベントに新しい円を描画するコードを追加し、プログラムの初期化時に円を 1 つだけ描画します。または、
  • While-loop を -loop に変更しForます。たとえば、おそらく 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);
}
于 2013-11-15T08:03:29.730 に答える
0

while ループから GOval を取り出します。そこに保持すると、再生成されるため、クラッシュします。

于 2013-11-15T08:46:31.727 に答える