Java BlueJ的矩形类
我正在尝试用BlueJ为矩形类编写代码。当我编译代码时,我得到一个错误“类矩形中的构造函数矩形不能应用于给定的类型;必需:没有参数;找到:int,int,int,int;原因:实际参数列表和形式参数列表长度不同”。另外,我对矩形宽度x高度的公式放在哪里感到困惑。我试图创建一个名为rectangle的类,我可以在另一个名为picture的类中使用它。我想用这门课在房子的图片上画一个烟囱 这是我的密码:Java BlueJ的矩形类,java,syntax-error,shapes,rectangles,bluej,Java,Syntax Error,Shapes,Rectangles,Bluej,我正在尝试用BlueJ为矩形类编写代码。当我编译代码时,我得到一个错误“类矩形中的构造函数矩形不能应用于给定的类型;必需:没有参数;找到:int,int,int,int;原因:实际参数列表和形式参数列表长度不同”。另外,我对矩形宽度x高度的公式放在哪里感到困惑。我试图创建一个名为rectangle的类,我可以在另一个名为picture的类中使用它。我想用这门课在房子的图片上画一个烟囱 这是我的密码: import java.awt.*; public class Rectangle {
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);
}
}
import java.awt.*;
公共类矩形
{
私人内部高度;
私有整数宽度;
私密位置;
私人内部职位;
私有字符串颜色;
私有布尔值是可见的;
/**
*使用默认颜色在默认位置创建一个新矩形。
*/
公共矩形()
{
高度=20;
宽度=10;
xPosition=310;
位置=120;
color=“红色”;
isVisible=false;
}
/**
*使此矩形可见。如果它已可见,则不执行任何操作。
*/
public void makeVisible()
{
isVisible=true;
draw();
}
/**
*使此矩形不可见。如果它已不可见,请不要执行任何操作。
*/
公共空间
{
擦除();
isVisible=false;
}
/**
*将矩形向右移动几个像素。
*/
公权
{
水平移动(20);
}
/**
*将矩形向左移动几个像素。
*/
公共空间左移()
{
水平移动(-20);
}
/**
*将矩形向上移动几个像素。
*/
公共空间向上移动()
{
垂直移动(-20);
}
/**
*将矩形向下移动几个像素。
*/
公共无效向下移动()
{
垂直移动(20);
}
/**
*按“距离”像素水平移动矩形。
*/
公共空间水平移动(整数距离)
{
擦除();
xPosition+=距离;
draw();
}
/**
*将矩形垂直移动“距离”像素。
*/
公共空间垂直移动(整数距离)
{
擦除();
位置+=距离;
draw();
}
/**
*将矩形水平移动“距离”像素。
*/
公共空隙慢速移动水平(整数距离)
{
内特三角洲;
如果(距离<0)
{
delta=-1;
距离=-距离;
}
其他的
{
δ=1;
}
对于(int i=0;i
}我不清楚你的背景是什么,但是你的
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);
}
}
这将使有关draw()
方法的错误消失,但我不确定这是否是解决问题所需的全部。你能提供更多的背景吗
我认为你的画法应该是这样的:
public Rectangle(xPos, yPos, width, height)
{
this.height = height;
this.width = width;
this.xPosition = xPos;
this.yPosition = yPos;
color = "red";
isVisible = false;
}
private void draw()
{
if(isVisible)
{
Canvas canvas = Canvas.getCanvas();
canvas.drawRect(color, this.xPosition, this.yPosition, this.width, this.height);
canvas.wait(10);
}
}
您在主要方法中可能做的是:
rectangler=新矩形(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)的《Java思考》一书中的一个例子。我知道他说要用BlueJ,但在我看来IDE不是很好,为了你自己好看看,或者