I'm doing this University practice and I'm all stuck. I'm supposed to make a program to draw circles and lines. It has 3 packages, Evaluar(with the main class), Dibujo(with the drawing class) and Forma (with 4 class, The FATHER, forma, formabasica which inherits from Forma and Circulo&Linea which inherits from formaBasica.
Here is the main class:
package PaqueteAplicacion;
import PaqueteFormas.Forma;
import java.util.Scanner;
import PaqueteFormas.Circulo;
import PaqueteFormas.Linea;
public abstract class Evaluar {
public static void main (String[] args) {
//Creamos atributos para lo que graficaremos
Forma linea1;
Forma linea2;
linea1 = new Linea(); //PROBLEM HERE
linea2 = new Linea(1,1);//PROBLEM HERE
}
Eclipse's error "Cannot instantiate the type Linea", and I don`t know why its giving me this problem.
This is my Linea class, I'm having the same problem with Circulo.
package PaqueteFormas;
import java.util.Scanner;
import PaqueteGraficos.Dibujo;
public class Linea extends FormaBasica {
//Atributos
int numSegmentos;
public Linea(){
}
public Linea(int ejeX, int ejeY){
super(ejeX,ejeY);
}
public void cambiarTamano(int incrementoSegmentos){
numSegmentos = incrementoSegmentos;
}
public void paintComponent() {
Dibujo.dibujarLinea(ejeX, ejeY, numSegmentos);//More Problem ejeX cannot be resolved as a variable
}
}
If you need more information please let me know, Thank you!
Sorry I'm new in StackOverflow so if my post is not correct please tell me.
I added the rest of the Java classes from my project: This is the FATHER: Forma's class:
package PaqueteFormas;
public abstract class Forma {
//Añadimos atributos
private int x,y;
public Forma()
{
this.x = 1;
this.y = 1;
}
public Forma( int ejeX, int ejeY) {
this.x = ejeX;
this.y = ejeY;
}
//Establece la edición atributo
public void setX(int ejeX) {
x = ejeX;
}
public void setY(int ejeY) {
y = ejeY;
}
public int getX() {
return x;
};
public int getY() {
return y;
};
public void moverEjeX(int variacionEnX) {
x += variacionEnX;
}
public void moverEjeY(int variacionEnY) {
y += variacionEnY;
}
public abstract void paintComponent();
public abstract void cambiarTamano (int variacionTamano);
}
Heritage's class from Forma:
package PaqueteFormas;
public abstract class FormaBasica extends Forma {
public FormaBasica(){//Constructor por defecto
super(1,1);
}
public FormaBasica(int ejeX, int ejeY){//Constructor pasando los atributos
super(ejeX,ejeY);
}
}
The other class' heritage from FormaBasica
package PaqueteFormas;
import java.util.Scanner;
import PaqueteGraficos.Dibujo;
public class Linea extends FormaBasica {
int numSegmentos;
public Linea(){
}
public Linea(int ejeX, int ejeY){
super(ejeX,ejeY);
}
public void cambiarTamano(int incrementoSegmentos){
numSegmentos = incrementoSegmentos;
}
public void paintComponent() {
Dibujo.dibujarLinea(ejeX, ejeY, numSegmentos);
}
}
The root of your problems is in this line in Linea
:
Dibujo.dibujarLinea(ejeX, ejeY, numSegmentos);//More Problem ejeX cannot be resolved as a variable
The error message makes sense: There is no variable ejeX
in the paintComponent
method, where this line stands, and also no field (instance or class variable) ejeX
in the Linea
class. There is a parameter ejeX
in one of the constructors. But here a constructor is just like a method, and you cannot access parameters or variables in another method because it would not make sense to do. Methods are called at different times, so the variables of one method usually don’t exist when another method is called and is executing. Same for ejeY
, of course.
I assume you meant to use the values of ejeX
and ejeY
that were passed to the Linea
constructor when the object was created. These have been stored in the Forma
superclass, and you can get them through the getX
and getY
methods of that class. So the fix is to change the line to this:
Dibujo.dibujarLinea(getX(), getY(), numSegmentos);
For your other problems: The error addressed above prevented the Linea
class from being compiled. When it couldn’t be compiled, it also could not be instantiated. So I hope that’s fixed now too.
If new error messages turn up as you get rid of the old errors, don’t despair, though, this is normal.
And it’s not to be unfriendly, the contrary: See if you can find a local who will help you. Maybe a fellow student or a teaching assistant? The turnaround time on Stack Overflow is too long, so your progress will be slow as long as you rely on us. Even more so when you post so much code because many users will give up on reading through it all.