import java.awt.*; public class CC5 { // It's easier to see the order in which cells draw // if you change the color every time. public static Color getRandomColor() { int r = (int)(Math.random() * 256); int g = (int)(Math.random() * 256); int b = (int)(Math.random() * 256); return new Color(r,g,b); } public static void drawWhateverInsideCell(int left, int top, int szSubFig) { // this is where you draw INSIDE the cell } public static void grid(Graphics2D g, int x, int y, int szSubFig, int rc) throws InterruptedException { int left = x; int top = y; for (int r = 0; r < rc; r++) { for (int c = 0; c < rc; c++) { System.out.println("row="+r+",col="+c + " left="+left + " top=" + top); g.setColor(getRandomColor()); g.fillRect(left, top, szSubFig, szSubFig); drawWhateverInsideCell(left, top, szSubFig); Thread.sleep(1000); left = left + szSubFig; // comment out this line to learn how this works } top = top + szSubFig; // comment out this line to learn how this works left = x; // comment out this line to learn how this works } } public static void frame(Graphics2D g, int x, int y, int szSubFig, int rc) throws InterruptedException { int left = x; int top = y; int wh = szSubFig * rc; g.setColor(Color.RED); g.drawRect(left, top, wh, wh); Thread.sleep(2000); System.out.println(); grid(g, x, y, szSubFig, rc); } public static void main(String[] args) throws InterruptedException { DrawingPanel dp = new DrawingPanel(400,400); dp.setBackground(Color.WHITE); Graphics2D g = dp.getGraphics(); frame(g, 0, 0, 100, 1); frame(g, 10, 120, 24, 5); frame(g, 150, 20, 40, 6); frame(g, 130, 275, 36, 3); } }