import java.awt.Color; import java.awt.Graphics2D; public class Grid { public static void drawWhateverInsideCell(Graphics2D g, 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) { int left = x; int top = y; for (int r = 0; r < rc; r++) { for (int c = 0; c < rc; c++) { g.setColor(Color.BLACK); g.drawRect(left, top, szSubFig, szSubFig); drawWhateverInsideCell(g, left, top, szSubFig); 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) { int left = x; int top = y; int wh = szSubFig * rc; grid(g, x, y, szSubFig, rc); g.setColor(Color.RED); g.drawRect(left, top, wh, wh); } public static void main(String[] args) { 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); } }