BlueJ的矩形类 [英] Rectangle Class for BlueJ

查看:142
本文介绍了BlueJ的矩形类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为BlueJ中的矩形类编写代码.编译代码时,出现错误无法将Rectangle类中的构造函数Rectangle应用于给定类型;必需:无参数;找到:int,int,int,int;原因:实际参数和形式参数列表的长度不同".另外,我对将矩形的公式放在何处感到困惑-宽x高.我正在尝试创建一个名为矩形"的类,该类可以在另一个名为图片"的类中使用.我正在尝试使用此类在房屋图片上制作烟囱.

I am trying to write code for a rectangle class in BlueJ. When I compile my code I get the error "constructor Rectangle in class Rectangle cannot be applied to given types; required: no arguments; found: int, int, int, int; reason: actual and formal argument list differ in length". Also, I am confused on where to put the formula for a rectangle - width x height. I am trying to create class called rectangle that I could use in another class called picture. I am trying to use this class to make a chimney on the house picture.

这是我的代码:

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);
    }
}

}

推荐答案

对于您的上下文,我很迷惑,但是您的 draw()方法使用构造函数创建您目前尚没有的Recangle的新实例:

I'm quite foggy on what your context is with this, but your draw() method uses a constructor to create a new instance of Recangle you do not have so far:

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

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

You could add to your class the needed constructor, something like this:

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

这会使与您的draw()方法有关的错误消失,但是我不确定这是否是解决问题所需的全部.您可以提供更多背景信息吗?

This would make the error concerning your draw() method go away, but i'm not sure this is all you need to fix your problem. Can you give more context?

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

I think your draw method should be rather looking something like this:

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

这篇关于BlueJ的矩形类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆