Java中方法执行的延迟

时间:2016-09-09 14:07:07

标签: java loops methods awt

我在for循环中执行方法时遇到问题。我希望我的程序执行programmedMoving()方法5次。

programmedMoving()方法包含两种方法:

第一个(chooseTheDirection())执行一些算法并返回对象应移动到的Point2D;

第二个(moveToThePoint())应该得到这一点并移动对象。

public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_H) {
            for(int i=0; i<5; i++{
               programmedMoving();
            }
        }
}

//////////////////////////////////////////////////////////////////

private void programmedMoving(){
        chooseTheDirection();  //returns the Point2D
        moveToThePoint();//according to the direction starts moving the point
    }

问题是它执行chooseTheDirection()方法5次而不等待moveToThePoint()方法完成它的执行。因此,当JPanel上的对象开始实际移动时,chooseTheDirection()方法已经提供了5个Point2D点,而我需要它只提供一个,并等待结束下一个方法。

有谁能告诉我,我做错了什么?谢谢。

enter image description here

此外:

private Direction chooseDirection(){
    final List<Direction> directionList = Collections.unmodifiableList(Arrays.asList(Direction.values()));

    int pick = random.nextInt(directionList.size());
    dir = directionList.get(pick);
    directionsArchive.add(dir);
    System.out.println("dir " + dir + " " + directionsArchive);
    if(directionsArchive.size() == 1){
        dir = directionsArchive.get(0);
        System.out.println("equal to one taken " + dir + " size of dir " + directionsArchive.size());
        directionsArchive.add(dir);
    }
    if(directionsArchive.size() > 1){
        int last = directionsArchive.size()-1;
        System.out.println("index of last " + last);
        if(directionsArchive.get(last).equals(dir)){
            pick = random.nextInt(directionList.size());
            dir = directionList.get(pick);
            directionsArchive.add(dir);
        }
        System.out.println("more than one taken " + dir + " size of dir " + directionsArchive.size());
        directionsArchive.add(dir);
    }
    else{
    directionsArchive.add(dir);
    System.out.println(" size of dir " + directionsArchive.size());}
    return dir;
}


private void moveToThePoint(){
    if(dir.equals(Direction.NORTH)){
        this.robot.turnUp();
        this.robot.go();
    }
    if(dir.equals(Direction.SOUTH)){
        this.robot.turnDown();
        this.robot.go();            
    }
    if(dir.equals(Direction.EAST)){
        this.robot.turnRight();
        this.robot.go();
    }
    if(dir.equals(Direction.WEST)){
        this.robot.turnLeft();
        this.robot.go();
    }
}

// SOME EXAMPLES TO THE MOVING METHODS. I PROVIDED ONLY ONE, CAUSE THEY ARE PRETTY SIMILAR
    public void turnDown()
    {
        //System.out.println("Robot - Turn Down!");
        this.m_nXDir = 0;
        this.m_nYDir = 1;
    }

    public void go()
    {
        this.m_nStep = 1;
        //System.out.println("Robot - Go!");
    }

public void move(int d, int e)
{
    //from start to finish
    int x = d + this.m_nStep * this.m_nXDir;
    int y = e + this.m_nStep * this.m_nYDir;

    int w = getWidth();
    int h = getHeight();

    setBounds(x, y, w, h);
}

1 个答案:

答案 0 :(得分:0)

moveToThePoint实际上并不移动对象,它只是将步长设置为1.我假设在计时器上调用实际的move()函数。

以下是有关如何修复它的2条建议:

为每一步重新运行决策算法(programmedMoving)。

排队未来的举动。您存储对象的未来移动队列,并且每次将其安排到move(),您将从队列中删除下一个移动并执行它。