1

I tried making two rectangles using the class below: DrawRect but when I create a new DrawRect object it replaces old one's width and height.

package MemDiagramVisualizer;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;

public class DrawRect extends JPanel {
   private static int RECT_X;
   private static int RECT_Y;
   private static int RECT_WIDTH;
   private static int RECT_HEIGHT;

   
   public DrawRect(int w, int h) {
       RECT_X = 20;
       RECT_Y = 20;
       RECT_WIDTH = w;
       RECT_HEIGHT = h;
   }
   
   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
   }

}
JPanel visDisplay = new JPanel();
visDisplay.setLayout(new GridLayout(1,3));
DrawRect rec2 = new DrawRect(40,60);
visDisplay.add(rec2);
DrawRect rec = new DrawRect(100,600);
visDisplay.add(rec);

The code above when added to frame content pane creates two rectangles with 100,600 dimensions

KGajjala
  • 15
  • 4
  • 1
    What is `drawrec2`? For better help sooner post a proper [mre] that demonstrates the issue – Frakcool Jul 25 '20 at 00:16
  • it's literally a copy of DrawRect, it is my current workaround the problem: to have a separate class that has the same code but different name. I edited the code to adress my initial problem – KGajjala Jul 25 '20 at 00:29
  • Welcome to Stack Overflow. Please take the [tour](https://stackoverflow.com/tour), read about [what's on-topic](https://stackoverflow.com/help/on-topic), and read [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask) as well as [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). The later (as @Frakcool) already pointed out allows others to reproduce your problem, and so help you more quickly and effectively: _Help others to help you_. – Ivo Mori Jul 25 '20 at 00:29

1 Answers1

1

You have a couple of issues in your program:

  1. All these 4 variables:

    private static int RECT_X;
    private static int RECT_Y;
    private static int RECT_WIDTH;
    private static int RECT_HEIGHT;
    

    They all are static, but you're trying to change them inside the program, this will apply for all the instances of your program and they all are gonna share the value. In this case I'd suggest you to remove that and you should be good. This is the reason why when you create another class with the same code, it worked.

  2. RECT_X = 20; and RECT_Y = 20; inside the constructor, if these are constants, then initialize them at the top and don't set them in every instance of the class.

  3. Not an error, but depending on your requirements, you might want to stop using multiple JPanels and instead use the Shape API as shown in this answer to create an Array of Shapes that you can draw in a single JPanel. Again, this all depends on your needs.

After removing the static keyword from those constants above, we have this:

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89