首页 > 解决方案 > 我需要在它们的 Y 轴上移动两个形状,我该怎么做?

问题描述

我的作业中的问题需要一个从原始状态向上移动 50 像素的箭头。它需要一个“moveUp”方法,我需要使用setYInt()andgetYInt()方法来做到这一点。

这是我当前的代码:

import java.awt.*;
public class Arrow {
   private Canvas canvas;
   private Triangle head = new Triangle(120, 500, 50, 50, true);
   private Rectangle shaft = new Rectangle(140, 550, 10, 100);

   public Arrow(Canvas _canvas) {
      canvas = _canvas;
      Triangle head = new Triangle(120, 500, 50, 50, true);
      Rectangle shaft = new Rectangle(140, 550, 10, 100);
   }

   public void draw() {
      canvas.draw(head);
      canvas.draw(shaft);
   }

   public void erase() {
      canvas.erase(head);
      canvas.erase(shaft);
   }

   public void moveUp() {
      head.getYInt();
      shaft.getYInt();
      head.setYInt(head.getYInt - 50);
   }
}

我在使用此代码时遇到的错误:

Arrow.java:32: error: cannot find symbol
      head.setYInt(head.getYInt - 50);
                       ^
  symbol:   variable getYInt
  location: variable head of type Triangle
1 error

我不明白我应该如何获取 Y 整数并将其设置为不同的整数。任何帮助表示赞赏。

标签: javaawt

解决方案


head.setYInt(head.getYInt - 50);

您正在使用head.getYIntwhich 不是功能。试试.getYInt()吧。


推荐阅读