import java.awt.*; // Graphics import javax.swing.*; // JFrame, JPanel public class RecursiveI { public static void main(String[] args) { JFrame frame=new JFrame(""); frame.setSize(600,800); 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) { int x1=300, y1=200, x2=300, y2=500; // we will start with an I and then with the top and bottom end of the I, we will recursively draw Is there create(g, x1,y1, x2, y2, 0, 300); // our initial size of our line is 300, we halve it with each recursive call } public void create(Graphics g, int x1, int y1, int x2, int y2, int c, int size) // if c is 0, draw the initial I from x1,y1 to x2,y2 and recurse at both top and bottom of the I { if(c==0) { g.drawLine(x1,y1,x2,y2); create(g,x1-size/2,y1,x2+size/2,y1,c+1,size/2); create(g,x1-size/2,y2,x2+size/2,y2,c+1,size/2); } else if(x1!=x2||y1!=y2) // our base case is when x1==x2 and y1==y2 (1 pixel long line) { if(c%2==0) // if c is even, we are drawing a vertical line and then we recurse by drawing lines at the top and bottom of this line { g.drawLine(x1,y1,x2,y2); create(g,x1-size/2,y1,x1+size/2,y1,c+1,size/2); create(g,x2-size/2,y2,x2+size/2,y2,c+1,size/2); } else // otherwise c is odd, we are drawing a horizontal line and then we recurse by drawing lines at the left and right of this line { g.drawLine(x1,y1,x2,y2); create(g,x1,y1-size/2,x1,y1+size/2,c+1,size/2); create(g,x2,y1-size/2,x2,y1+size/2,c+1,size/2); } } } } }