How to draw shapens inside shapes? [SOLVED] [NEW] How to create ArrayList to use Colors?
<p>I am trying to build a an application where I intend to create some basic animations. My goal is to have a panel on top where the graphics will be painted and a panel below with regular buttons and labels.</p>

<p>I have done the code presented below but I am failing to draw rectangles inside previously drawn rectangles. I believe my problem is that I am calling a class (Rectangle) to draw the rectangles which extends a JPanel, meaning that everytime I try to draw a new rectangle a new panel will be creating not overlapping the previous one!</p>

<p><em><strong>EDIT</em></strong></p>

<p>I've looked up at some more examples, and finally could come up with a code to generate shapes (rectangles this case) as I need them on the same panel. I've made some changes to the code, below is the new code. </p>

<p>L.java</p>

<pre><code>import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class L extends JFrame {

public static final int ww = 1000;
public static final int wh = 600;
public static final String wt = "Teste";
Container pane = getContentPane();
DrawRectangle rectangle = new DrawRectangle();

public L() {
    setSize(ww,wh);
    this.setTitle(wt);
    Sim();
    pane.add(rectangle);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public void addRectangle(int px, int py, int pwh, Color pc) {
    this.rectangle.addRectangle( px, py, pwh, pc);
}

public void Sim() {
    addRectangle(100,100,250,Color.red);
    addRectangle(200,200,50,Color.green);
}

public static void main(String[] args) {

    L l = new L();

    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }

    //} 

}

}  
</code></pre>

<p>DrawRectangle.java</p>

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

class DrawRectangle extends JPanel {
private java.util.List&lt;Rectangle2D&gt; squares;
private java.util.List&lt;Color&gt; colors;
//private int a, startX, startY;
public DrawRectangle(){
    squares = new ArrayList&lt;Rectangle2D&gt;();
    colors = new ArrayList&lt;Color&gt;();
}

public void addRectangle(int px, int py, int pwh, Color pc)  { // square
    squares.add( new Rectangle2D.Double(px, py, pwh, pwh) ) ;
    colors.add(pc);
    //this.a = a;
    //this.startX = startX;
    //this.startY = startY;
}
public void paintComponent(Graphics g) {
    Graphics2D g1 = (Graphics2D) g;
    for( Rectangle2D rect : squares ) {
        System.out.println(colors);
        g1.setColor(colors);
        g1.draw(rect);
    }
}
}
</code></pre>

<p>A new problem has now arose! What am I doing wrong that I can't use the Color I am passing to the method to paint the shapes with different colors?</p>
