我正在尝试在 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);
}
}
}
最佳答案
我对你的背景很模糊,但是你的
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);
}
}
关于java - BlueJ 的矩形类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920112/