单击时显示对象

时间:2013-11-15 07:04:31

标签: java gobject

我是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  




}


}

3 个答案:

答案 0 :(得分: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类,但它也可能导致你的记忆在某些时候填满。

答案 1 :(得分:0)

通过执行以下操作,您将在特定位置绘制一个圆圈。

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

while (true)会在完成后立即重新执行此代码,并继续执行此操作。这将填满你的记忆并最终导致崩溃。

但除此之外,它毫无意义,因为用户甚至看不到单独的圆圈:它们被绘制在彼此之上。

如果确实如此,那么我建议添加Integer来计算当前屏幕中的圈数。然后,您将添加到当前While - 循环的末尾:

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

这将始终在画布中保留两个圆圈,最大延迟为~100 ms。

您的IDE会提醒您应该抓住的例外情况。

在编程和功能上更有意义的是:

  • 添加代码以在删除旧圆圈的事件中绘制新圆圈,并在程序初始化时仅绘制一个圆圈。或者,
  • While - 循环更改为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);
}

答案 2 :(得分:0)

将你的GOval从while循环中取出。保持它在那里使它再生,因此崩溃。