我正在尝试为BlueJ中的矩形类编写代码。当我编译我的代码时,我得到错误“类Rectangle中的构造函数Rectangle不能应用于给定的类型; required:无参数; found:int,int,int,int; reason:actual和formal参数列表的长度不同”。另外,我很困惑在哪里把公式放在一个矩形 - 宽x高。我正在尝试创建一个名为rectangle的类,我可以在另一个名为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);
}
}
}
答案 0 :(得分: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()
方法的错误消失,但我不确定这是解决问题所需的全部内容。你能给出更多背景吗?
我认为你的绘制方法应该是这样的:
private void draw()
{
if(isVisible)
{
Canvas canvas = Canvas.getCanvas();
canvas.drawRect(color, this.xPosition, this.yPosition, this.width, this.height);
canvas.wait(10);
}
}
答案 1 :(得分:0)
你在主要方法中可能做的是:
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 Eckel 的书 Thinking in Java 中的一个例子。我知道他说要使用BlueJ,但在我看来IDE不是很好,为了你自己的好处,请查看NetBeans,Eclipse或IntelliJ。