Java 一起移动对象

Java 一起移动对象,java,bluej,Java,Bluej,编写一个公共实例方法move(),该方法接受两个整数参数,表示更改实例变量xPos和yPos的值的量。该方法不应返回任何值。它应该利用提供的方法delay()暂停,以便可以看到重复运行该方法的效果,例如,此延迟(20) 暂停执行20毫秒 通过移动StickFigure实例并检查其是否保持对齐来测试代码 我的代码如下,但我似乎不知道如何将所有3个形状移动到一起,只有三角形似乎在移动 public class StickFigure { /*Instance variables*/

编写一个公共实例方法move(),该方法接受两个整数参数,表示更改实例变量xPos和yPos的值的量。该方法不应返回任何值。它应该利用提供的方法delay()暂停,以便可以看到重复运行该方法的效果,例如,此延迟(20)

暂停执行20毫秒

通过移动StickFigure实例并检查其是否保持对齐来测试代码

我的代码如下,但我似乎不知道如何将所有3个形状移动到一起,只有三角形似乎在移动

public class StickFigure
{
   /*Instance variables*/   
   private int xPos;//The horizontal position of a StickFigure
   private int yPos;//The vertical position of a StickFigure
   private Circle head;
   private Triangle body;
   private Rectangle leg;
   //   add your declarations here for part (a)(i)

   /**
    * Constructor for objects of class StickFigure that 
    * provides a default stick figure near the bottom left corner of the graphical display.
    * 
    */
   public StickFigure()
   {
      super();
      this.head = new Circle (30, OUColour.PINK);
      this.body = new Triangle (50, 50, OUColour.RED);
      this.leg = new Rectangle (6, 50, OUColour.PINK);
      this.setXPos(25);  //sets starting position towards bottom left of Shapes window
      this.setYPos(220);
      this.alignAll();
   }   

   /**
    * Sets the xPos of the receiver to the value of the argument.
    */
   public void setXPos(int newPos)
   {
      this.xPos = newPos;
      this.body.setXPos(newPos);
      this.alignAll();
      //part (b)(iii)
   }

   /**
    * Returns the xPos of the receiver.
    */
   public int getXPos()
   {
      return this.xPos; 
   }

   /**
    * Sets the yPos of the receiver to the value of the argument.
    */
   public void setYPos(int newPos)
   {
      this.yPos = newPos;
      this.body.setYPos(newPos);
      this.alignAll();

      //part (b)(iii)
   }

   /**
    * Returns the yPos of the receiver.
    */
   public int getYPos()
   {
      return this.yPos;
   }  

   /**You will need to uncomment these methods when directed to*******/

   /**
    * Returns a reference to the head of the receiver.
    */
   public Circle getHead()
   {
      return this.head;   
   }

   /**
    * Returns a reference to the body of the receiver.
    */
   public Triangle getBody()
   {
      return this.body;   
   }

   /**
    * Returns a reference to the leg of the receiver.
    */
   public Rectangle getLeg()
   {
      return this.leg;   
   }

   /**
    * Aligns the head of the receiver relative to the xPos and yPos of the body.
    */
   public void alignHead()   
   {
      this.head.setXPos(this.body.getXPos() + (this.body.getWidth() - this.head.getDiameter())/2);
      this.head.setYPos(this.body.getYPos() - this.head.getDiameter());
   }  

   /**
    * Aligns the body of the receiver relative to the xPos and yPos of the head.
    */
   public void alignBody()   
   {
      this.body.setXPos(25);
      this.body.setYPos(220);
   } 

   /**
    * Aligns the leg of the receiver relative to the xPos and yPos of the head and body.
    */
   public void alignLeg()   
   {
      this.leg.setXPos(this.body.getXPos() + (this.body.getWidth() - this.leg.getWidth())/2);
      this.leg.setYPos(this.body.getYPos() + this.leg.getHeight());
   } 

   /**
    * Aligns all the body parts of the receiver to form the
    * StickFigure-type figure.
    */

   public void alignAll()
   {
      this.alignBody();
      this.alignHead();      
      this.alignLeg();
   }

   public void move(int xPos, int yPos)
   {
      this.alignAll();
      this.body.setXPos(xPos + xPos);
      this.body.setYPos(yPos + yPos);
      this.delay(20);


   }
您的
move()
不应该设置XPO和YPO。因此:

 this.body.setXPos(xPos + xPos);
 this.body.setYPos(yPos + yPos);
应该是

 this.setXPos(this.xPos + xPos);
 this.setYPos(this.yPos + yPos);
后跟
alignAll()

然后您的
alignBody()
会说:“将接收器的主体与头部的XPO和YPO对齐”,但您的方法不会这样做。。。
头/腿似乎与身体有关。因此,主体X、Y可能应该相对于XPO和YPO?

考虑提供的方法alignHead(),该方法将主体X、Y与棍形主体的XPO和YPO对齐,使其位于主体正上方的中心。确保你了解它是如何工作的。编写alignBody()相当简单,因为主体只是简单地放置在棍子图形本身的坐标上,但是alignLeg()需要更多的考虑。i、 编写一个公共实例方法alignBody(),该方法不接受任何参数,也不返回任何值。它应该将body的xpo和ypo设置为StickFigure的xpo和ypo。那么所有人都应该处理好你说的话。因为头会跟着身体。你的moveAll不应该直接重置身体的XPO和YPO。它应该重置XPO和YPO本身,然后调用alignAll,这将从移动主体开始。。是的,我在这里发表评论,因为我认为这就是这个答案的想法。这里的想法是正确的。但是,除非您将alignBody修复为不硬编码,否则alignAll将把所有内容移回原位。另一方面,对齐方式不应该调用对齐方式。Test.xPoS+XPOS工作,但是调用参数DelCube会更清楚。考虑提供的方法对齐头(),它将头相对于杆图形的XPOS和YPOS对齐,这样它就直接位于身体上方。确保你了解它是如何工作的。编写alignBody()相当简单,因为主体只是简单地放置在棍子图形本身的坐标上,但是alignLeg()需要更多的考虑。i、 编写一个公共实例方法alignBody(),该方法不接受任何参数,也不返回任何值。它应该将主体的XPO和YPO设置为StickFigure的XPO和YPO。