I am trying to create a custom component using Java AWT or Swing which will be a rectangle with a number of components inside of it, including other rectangles. Something like this:
╔══════╗
║ ┌┐ ║
║ ├┘ ║
║ ║
╚══════╝
And this needs to be a component that I can preferably draw with one instruction. Something like myFrame.add(new MyComponent()).
What would you say is the best way to do this? Is there a way I can do this using Rectangle, or should I go with JPanel or something from Swing?
I would recomend extending a
JPaneland overriding it’spaintComponent()method. See another answer of mine for some help on that.Basically, when a rectangle is ‘drawn’ on your panel, you will want to save it as a member of the
Jpanel. Then, in thepaintComponentmethod, you will just draw all of the rectangles you have saved in yourJPanel.This is how I would implement a ‘draw’ method:
And, the paint component will look similar to: