Need help drawing rectangles inside rectangles
<p>I have (yet) another question to make. I'm not sure I referred to my problem correctly on the title but my problem is:</p>

<p>I am trying to create a program that for now will generate some components inside others. My idea is to create a rectangle that will serve as background and then create a function that will allow to create other small rectanges inside that first rectangle (and later on I will try to animate them and move alongside that rectangle, removing and creating more rectangles).</p>

<p>I want to create a class Peca in which I can freely create smaller rectangles to be drawn inside the background rectangle (done with Fundo). My question is how shall I use the Peca class I've created so that it is added to the containment hierarchy and further display those rectangles being created? I've tried adding it to the Container but if I do so, it will override was was generated by Fundo, so how shall I call the class so that it overlaps the previous drawings?</p>

<p>Here is my java code:</p>

<p>Simulator.java</p>

<pre><code>import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;


public class Simulator extends JFrame {

private final static int dimH = 1000;
private final static int dimV = 500;
private final static int startx = 10;
private final static int starty = 10;
private final static String title = "Test";
public JFrame frame = new JFrame();
Container pane = frame.getContentPane();

public Simulator() {
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    initcomponents();
}

public static void main (String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Simulator();
        }
    }); 
}

public void initcomponents() {
    //JFrame frame = new JFrame();
    frame.setSize(dimH, dimV);
    frame.setTitle(title);
    frame.setResizable(false);
    Dimension appletSize = frame.getSize();
    int appletHeight = appletSize.height;
    int appletWidth = appletSize.width; 

    Fundo f = new Fundo(startx,starty,appletWidth - 25,appletHeight - 80,Color.blue);
    pane.add(f);
    Peca p = new Peca(10,10,10,10,Color.red);

//pane.add(p);
    pane.add(new Botoes(f), "South");
   // frame.addWindowListener(new GereJanela());
    frame.setVisible( true );
    frame.setLocationRelativeTo(null);
    pack();
}
}
</code></pre>

<p>Fundo.java</p>

<pre><code>import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Fundo  extends JPanel {
    int cx,cy,comp,larg;
    Color cor;

    public Fundo (int cx, int cy, int comp, int larg, Color cor) {
        System.out.println("Fundo");
        this.cx = cx;
        this.cy = cy;
        this.comp = comp;
        this.larg = larg;
        this.cor = cor; 
    }
        public void paintComponent( Graphics g ) {
            System.out.println("Fundopaint");
            super.paintComponent(g);
            g.setColor(cor);
            g.fillRect(cx,cy,comp,larg);
            g.setColor(Color.red);
            g.drawRect(25,25,50,50);
            Peca p = new Peca(50,50,50,50,Color.orange);
        }

        public void escuro() {
            cor = cor.darker();
            repaint();
        }

        public void claro() {
            cor = cor.brighter();
            repaint();
        }
}
</code></pre>

<p>Botoes.java</p>

<pre><code>import javax.swing.JButton;
import javax.swing.JPanel;

public class Botoes extends JPanel {
    public Botoes (Fundo f) {
        System.out.println("Botoes");
        JButton start = new JButton("Claro");
        //start.addActionListener(new GereEventos(1,p));
        this.add(start);
        JButton reset = new JButton("Escuro");
        //reset.addActionListener(new GereEventos(2,p));
        this.add(reset);
        JButton sair = new JButton("Exit");
        //sair.addActionListener(new GereEventos(3,p));
        this.add(sair);
    }
}
</code></pre>

<p>Peca.java</p>

<pre><code>import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JPanel;

public class Peca  extends JPanel {
    int cx,cy,comp,larg;
    Color cor;

    public Peca (int cx, int cy, int comp, int larg, Color cor) {
        System.err.println("Peca");
        this.cx = cx;
        this.cy = cy;
        this.comp = comp;
        this.larg = larg;
        this.cor = cor; 
    }
     //      frame.add( new JPanel() {
        public void paintComponent( Graphics g ) {
            System.err.println("Pecapaint");
            super.paintComponent(g);
            g.setColor(Color.orange);
            g.drawRect(200, 200, 50, 50);
        }

}
</code></pre>

<p>My problem here is how to call Peca to draw new rectangles inside the other one whenever I need!</p>

<p>Thanks in advance for those who bother to take a look through this so I can understand what I am doing wrong and how it should be done correctly!</p>
