zdravím...neviete prečo, a ako odstrániť chybu ktorú mi vypisuje program?
"cannot reference x before supertype constructor has been called"
Abstraknú triedu shapes zkompiluje v pohode...ale pri kompilácii Rectangle to vyhodí. Keď však do dám konkrétne parametre napr. super(5,5,Color.RED) tak to ide...keď x,y,color tak nie.
Zdrojáky
Abstract class Shapes
Kód:
abstract class Shapes
{
protected Color color;
protected int x;
protected int y;
protected boolean visible;
public Shapes(int x, int y) {
this.x=x;
this.y=y;
visible = false;
color = Color.RED;
}
public Shapes(int x, int y, Color color) {
this.x=x;
this.y=y;
this.color = color;
visible = false;
color = Color.RED;
}
protected void show() {
visible = true;
Canvas.setColor(color);
draw( color);
}
protected void hide() {
visible = true;
Canvas.setColor(Canvas.getBackgroundColor());
draw(color);
}
protected void draw(Color color) {
}
void moveRight() {
hide();
x +=1;
show();
}
void moveLeft() {
hide();
x -= 1;
show();
}
void moveUp() {
hide();
y -= 1;
show();
}
void moveDown() {
hide();
y += 1;
show();
}
int getX() {
return x;
}
void setX(int x) {
hide();
this.x = x;
show();
}
int getY() {
return y;
}
void setY(int y) {
hide();
this.y = y;
show();
}
}
class Rectangle
Kód:
class Rectangle extends Shapes {
protected int width;
protected int height;
protected int step;
public Rectangle( int width, int height, boolean visible) {
super(x,y,color);
this.width = width;
this.height = height;
this.visible = visible;
if (visible) {
show();
}
}
private void draw() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Canvas.drawPixel(x + i, y + j);
}
}
}
int getWidth() {
return width;
}
void setWidth(int width) {
hide();
this.width = width;
show();
}
int getHeight() {
return height;
}
void setHeight(int height) {
hide();
this.height = height;
show();
}
}
Ďakujem za pomoc...