在bluej中移动多个对象

时间:2016-04-22 18:52:14

标签: java bluej

我正在使用BLUEJ来创建一些基本对象等。 我有4个对象(3个矩形和一个三角形个体),我想使用“slowMoveHorizo​​ntal”方法将这4个对象向右移动约50px。

到目前为止,一切都有效,但问题在于它们不会同时移动。他们一个接一个地移动。

public class PLFlag
{
    private MyRectangle greenRectangle;
    private MyRectangle whiteRectangle;
    private MyRectangle blackRectangle; 
    private MyRectangle frame;
    private MyRectangle mast; 
    private Triangle redTriangle;


    /**
     * Constructor for objects of class PLFlag
     */
    public PLFlag()
    {
        // nothing to do... instance variables are automatically set to null
    }

    /**
     * Draw this PLFlag.
     */
    public void draw()
    {
        mast = new MyRectangle();
        mast.makeVisible();
        mast.changeSize(400,5);
        mast.changeColor("black");
        mast.moveVertical(-100);
        mast.moveHorizontal(-55);

        frame = new MyRectangle();
        frame.changeColor("black");

        frame.changeSize(122,180);
        frame.slowMoveHorizontal(-50);
        frame.moveVertical(-100);
        frame.makeVisible();

        greenRectangle = new MyRectangle();
        greenRectangle.changeColor("green");

        greenRectangle.changeSize(40,179);
        greenRectangle.moveHorizontal(-50);
        greenRectangle.slowMoveVertical(-19);
        greenRectangle.makeVisible();

        whiteRectangle = new MyRectangle();
        whiteRectangle.changeColor("white");

        whiteRectangle.changeSize(40,179);
        whiteRectangle.moveVertical(-59);
        whiteRectangle.moveHorizontal(-50);     
        whiteRectangle.makeVisible();

        redTriangle = new Triangle();
        redTriangle.changeSize(-90,-120);
        redTriangle.changeColor("red");

        redTriangle.slowMoveHorizontal(210);
        redTriangle.slowMoveVertical(-130);     
        redTriangle.makeVisible();
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码等待slowMove完成,以便继续。 你需要让slowMove在一个单独的Thread中并行运行。

将此代码置于每次 slowMoveXXX()来电。

new Thread()
{
    public void run() {
        //slowMove here. E.g.:
        greenRectangle.slowMoveVertical(-19);
    }
}.start();

此外,您需要将关键字final添加到您在Thread内使用的每个对象。 例如。 private final MyRectangle greenRectangle;如果你需要慢慢移动绿色矩形。

顺便说一句,你可能需要在慢动作之前拨打makeVisible(),否则在移动时你无法看到它。

编辑:未经测试的完整代码:

 public class PLFlag {
 private final MyRectangle greenRectangle;
 private MyRectangle whiteRectangle;
 private MyRectangle blackRectangle;
 private final MyRectangle frame;
 private MyRectangle mast;
 private final Triangle redTriangle;


 /**
  * Constructor for objects of class PLFlag
  */
 public PLFlag() {
  // nothing to do... instance variables are automatically set to null
 }

 /**
  * Draw this PLFlag.
  */
 public void draw() {
  mast = new MyRectangle();
  mast.changeSize(400, 5);
  mast.changeColor("black");
  mast.moveVertical(-100);
  mast.moveHorizontal(-55);
  mast.makeVisible();

  frame = new MyRectangle();
  frame.changeColor("black");
  frame.changeSize(122, 180);
  frame.makeVisible();
  new Thread() {
   public void run() {
    frame.slowMoveHorizontal(-50);
   }
  }.start();
  frame.moveVertical(-100);

  greenRectangle = new MyRectangle();
  greenRectangle.changeColor("green");
  greenRectangle.changeSize(40, 179);
  greenRectangle.moveHorizontal(-50);
  greenRectangle.makeVisible();
  new Thread() {
   public void run() {
    greenRectangle.slowMoveVertical(-19);
   }
  }.start();

  whiteRectangle = new MyRectangle();
  whiteRectangle.changeColor("white");
  whiteRectangle.changeSize(40, 179);
  whiteRectangle.moveVertical(-59);
  whiteRectangle.moveHorizontal(-50);
  whiteRectangle.makeVisible();

  redTriangle = new Triangle();
  redTriangle.changeSize(-90, -120);
  redTriangle.changeColor("red");
  redTriangle.makeVisible();
  new Thread() {
   public void run() {
    redTriangle.slowMoveHorizontal(210);
   }
  }.start();
  new Thread() {
   public void run() {
    redTriangle.slowMoveVertical(-130);
   }
  }.start();
 }
}