import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.util.ArrayList; import javax.swing.JFrame; /** * A fractal like printing of the letter H **/ public class FH extends JFrame { /** * Private class holding the location and size of the H **/ private class H { final int cx; final int cy; final int siz; public H(int ccx, int ccy, int siz) { this.cx=ccx; this.cy=ccy; this.siz=siz; } } /** * ArrayList holding the Hs to be printed **/ private ArrayList hs; public FH(){ hs = new ArrayList<>(); setSize(820, 820); setTitle("JFrame"); setBackground(Color.WHITE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } /** * Recursive method adding Hs **/ public void addH(int cx, int cy, int siz) { if (siz<1) { return; } try { Thread.sleep(10); } catch (Exception e) {} hs.add(new H(cx, cy, siz)); repaint(); addH(cx-siz,cy+siz, siz/2); addH(cx-siz,cy-siz, siz/2); addH(cx+siz,cy+siz, siz/2); addH(cx+siz,cy-siz, siz/2); //With this line in the func shows printing H as a depth-first line traversal // rather than screen filliing //hs.remove(hs.size()-1); } public String toString() { return ""; } public static void main(String[] args) { // TODO Auto-generated method stub FH m = new FH(); m.addH(410,400, 200); System.out.println(m); } /** * Does the actual rendering **/ public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.clearRect(0, 0, 800, 800); g2.setStroke(new BasicStroke(1)); for (H h:hs) { g2.draw(new Line2D.Float(h.cx-h.siz, h.cy, h.cx+h.siz, h.cy)); g2.draw(new Line2D.Float(h.cx-h.siz, h.cy-h.siz, h.cx-h.siz, h.cy+h.siz)); g2.draw(new Line2D.Float(h.cx+h.siz, h.cy-h.siz, h.cx+h.siz, h.cy+h.siz)); } } }