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.
Here is my code:
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);
}
}
}
What you probably do in your main method is this:
Rectangle r = new Rectangle(10,10,10,10);
But in your class only parameter-less constructor is defined, thus the error.
You will have to add another constructor:
Side personal opinion: If I am not mistaken, this is an example from Brucke Eckel's book Thinking in Java. I know that he says to use BlueJ, but in my opinion that IDE is not very good, for your own good have a look at NetBeans, Eclipse or IntelliJ.
I'm quite foggy on what your context is with this, but your
draw()
method uses a constructor to create a new instance ofRecangle
you do not have so far:You could add to your class the needed constructor, something like this:
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?I think your draw method should be rather looking something like this: