import java.awt.*; // used for Graphics import javax.swing.*; // used for JFrame, JPanel public class RecursiveDrawer { public static void main(String[] args) { JFrame frame=new JFrame(""); frame.setSize(600,600); DrawPanel p=new DrawPanel(); frame.add(p); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static class DrawPanel extends JPanel { public void paintComponent(Graphics g) { draw(g, 300,300,150,0); // start recursive call using 300,300 as starting point for the 3 lines, each line size is 150 } public void draw(Graphics g, int x, int y, int size, int c) // c is a counter, once it is too big, we won't have enough resolution to continue to recurse { if(c<8) // recurse only 7 times { g.drawLine(x-size/2,y,x,y); // draw 3 lines, left of x,y, right of x,y, up from x,y g.drawLine(x,y,x+size/2,y); g.drawLine(x,y-size/2,x,y); draw(g,x-size/2,y,size/2,c+1); // recurse with three new midpoints, one to the left of x,y, one to the right of x,y, one above x,y draw(g,x+size/2,y,size/2,c+1); draw(g,x,y-size/2,size/2,c+1); } } } }