【问题标题】:Rectangle Class for BlueJBlueJ 的矩形类
【发布时间】:2014-03-22 02:56:19
【问题描述】:

我正在尝试为 BlueJ 中的矩形类编写代码。当我编译我的代码时,我收到错误“矩形类中的构造函数矩形不能应用于给定类型;必需:无参数;找到:int、int、int、int;原因:实际和形式参数列表的长度不同”。另外,我对在哪里放置矩形的公式 - 宽度 x 高度感到困惑。我正在尝试创建一个名为矩形的类,我可以在另一个名为图片的类中使用它。我正在尝试使用此类在房屋图片上制作烟囱。

这是我的代码:

import java.awt.*;

public class Rectangle
{
    private int height;
    private int width;
    private int xPosition;
    private int yPosition;
    private String color;
    private boolean isVisible;

/**
 * Create a new rectangle at default position with default color.
 */
    public Rectangle()
    {
        height = 20;
        width = 10;
        xPosition = 310;
        yPosition = 120;
        color = "red";
        isVisible = false;
    }

/**
 * Make this rectangle visible. If it was already visible, do nothing.
 */
    public void makeVisible()
    {
        isVisible = true;
        draw();
    }

/**
 * Make this rectangle invisible. If it was already invisible, do nothing.
 */
    public void makeInvisible()
    {
        erase();
        isVisible = false;
    }

/**
 * Move the rectangle a few pixels to the right.
 */
    public void moveRight()
    {
        moveHorizontal(20);
    }

/**
 * Move the rectangle a few pixels to the left.
 */
    public void moveLeft()
    {
        moveHorizontal(-20);
    }

/**
 * Move the rectangle a few pixels up.
 */
    public void moveUp()
    {
        moveVertical(-20);
    }

/**
 * Move the rectangle a few pixels down.
 */
    public void moveDown()
    {
        moveVertical(20);
    }

/**
 * Move the rectangle horizontally by 'distance' pixels.
 */
    public void moveHorizontal(int distance)
    {
        erase();
        xPosition += distance;
        draw();
    }

/**
 * Move the rectangle vertically by 'distance' pixels.
 */
    public void moveVertical(int distance)
    {
        erase();
        yPosition += distance;
        draw();
    }

/**
 * Slowly move the rectangle horizontally by 'distance' pixels.
 */
    public void slowMoveHorizontal(int distance)
    {
        int delta;

        if(distance < 0) 
    {
            delta = -1;
            distance = -distance;
    }
        else 
    {
            delta = 1;
    }

        for(int i = 0; i < distance; i++)
    {
            xPosition += delta;
            draw();
    }
}

/**
 * Slowly move the rectangle vertically by 'distance' pixels.
 */
    public void slowMoveVertical(int distance)
{
        int delta;

        if(distance < 0) 
    {
            delta = -1;
            distance = -distance;
    }
        else 
    {
            delta = 1;
    }

        for(int i = 0; i < distance; i++)
    {
            yPosition += delta;
            draw();
    }
}

/**
 * Change the size to the new size (in pixels). Size must be >= 0.
 */
    public void changeSize(int newWidth, int newHeight)
{
        erase();
        height = newHeight;
        width = newWidth;
        draw();
}

/**
 * Change the color. Valid colors are "red", "yellow", "blue", "green",
 * "magenta" and "black".
 */
    public void changeColor(String newColor)
{
        color = newColor;
        draw();
}

/**
 * Draw the rectangle with current specifications on screen.
 */
    private void draw()
{
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.draw(this, color,
                    **new Rectangle(xPosition, yPosition, width, height));**
            canvas.wait(10);
    }
}

/**
 * Erase the rectangle on screen.
 */
    private void erase()
{
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.erase(this);
    }
}

}

【问题讨论】:

    标签: java syntax-error shapes rectangles bluej


    【解决方案1】:

    我对你的上下文是什么感到很模糊,但你的 draw() 方法使用构造函数来创建一个新的 Recangle 实例,你目前还没有:

    private void draw()
    {
        if(isVisible) 
        {
            Canvas canvas = Canvas.getCanvas();
            canvas.draw(this, color,
                    new Rectangle(xPosition, yPosition, width, height)); <-- Problem
            canvas.wait(10);
        }
    }
    

    您可以将所需的构造函数添加到您的类中,如下所示:

    public Rectangle(xPos, yPos, width, height)
    {
        this.height = height;
        this.width = width;
        this.xPosition = xPos;
        this.yPosition = yPos;
        color = "red";
        isVisible = false;
    }
    

    这将使您的draw() 方法的错误消失,但我不确定这是否是您解决问题所需的全部。能否提供更多背景信息?

    我认为你的 draw 方法应该看起来像这样:

    private void draw()
    {
        if(isVisible) 
        {
            Canvas canvas = Canvas.getCanvas();
            canvas.drawRect(color, this.xPosition, this.yPosition, this.width, this.height); 
            canvas.wait(10);
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可能在你的 main 方法中做的是这样的:

      Rectangle r = new Rectangle(10,10,10,10);

      但是在您的类中只定义了无参数构造函数,因此出现错误。

      你将不得不添加另一个构造函数:

          public Rectangle(int height, int width, int xPosition, int yPosition) {
              this.height = height ;
              this.width = width ;
              this.xPosition = xPosition ;
              this.yPosition = yPosition = 120;;
              color = "red";
              isVisible = false;
          }
      

      个人观点:如果我没记错的话,这是Brucke EckelThinking in Java 一书中的一个例子。我知道他说要使用BlueJ,但我认为IDE不是很好,为了您自己的利益,请查看NetBeansEclipseIntelliJ

      【讨论】:

        猜你喜欢
        • 2013-10-30
        • 1970-01-01
        • 2020-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        相关资源
        最近更新 更多