import java.awt.*; public class ConcentricCircles { 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 concentricCircles( Graphics2D g, int nCircles, int width, int height) throws InterruptedException { int left = 0; int top = 0; int w = width; int h = height; int wAdjust = width/nCircles; int hAdjust = height/nCircles; // top-left offsets are 1/2 the circle size adjustment int leftAdjust = width/(nCircles*2); int topAdjust = height/(nCircles*2); for (int n = 0; n < nCircles; n++) { g.setColor(Color.BLACK); g.drawRect(left, top, w, h); g.setColor(getRandomColor()); g.fillOval(left, top, w, h); Thread.sleep(1000); // Comment out each of the 4 lines below one-at-a-time // to see what each of them does. left += leftAdjust; top += topAdjust; w -= wAdjust; h -= hAdjust; } } public static void main(String[] args) throws InterruptedException { DrawingPanel dp = new DrawingPanel(400, 600); dp.setBackground(Color.WHITE); Graphics2D g = dp.getGraphics(); concentricCircles(g, 10, dp.getWidth(), dp.getHeight()); } }