nothing happens when i execute the code. Got my methods right? [closed]

If you ever actually try to construct an instance of Rektanglar, you’ll get a stack overflow due to this:

public class Rektanglar extends JPanel {
   Rektanglar r1 = new Rektanglar ();
   ...
}

That code says that in order to create one instance, you need to create another instance… which will create yet another instance, etc.

It’s not at all clear why you’ve got r1 at all, but I strongly suggest you get rid of it…

I also suspect this:

public void Rectangle(int x, int y, int w, int h) {
    X = x;
    Y = y;
    width = w;
    height = h;
}

… is meant to be a constructor, in which case you’d have to write it as:

public Rektanglar(int x, int y, int w, int h) {
    X = x;
    Y = y;
    width = w;
    height = h;
}

Note that the name has to match the name of the class and you don’t specify a return type.

Additionally, I’d suggest that:

  • You make all fields private
  • You compute the perimeter and area from the width and height, rather than keeping them as fields
  • You follow Java naming conventions for your variables (so x and y instead of X and Y).

Leave a Comment