import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JPanel; public class RecursiveLinesExample extends JPanel { public final static int WIDTH = 640; public final static int HEIGHT = 480; public final static int FAN_OUT = 10; public final static int MAX_LENGTH = 100; public final static int LENGTH_DECREMENT = 20; public final static int TOTAL_DEGREES = 360; public static void main(String[] args) { JFrame frame = new JFrame("Recursive Lines Example"); FileChooserExample panel = new FileChooserExample(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(WIDTH, HEIGHT); frame.setLocationRelativeTo(null); frame.setContentPane(new RecursiveLinesExample()); frame.setVisible(true); frame.repaint(); } public void paint(Graphics g) { super.paint(g); drawLines(g, WIDTH / 2, HEIGHT / 2, MAX_LENGTH); } public void drawLines(Graphics g, int x, int y, int length) { if (length == 0) { return; } for (int angle = 0; angle <= TOTAL_DEGREES; angle += TOTAL_DEGREES / FAN_OUT) { int x2 = (int)(x + length * Math.cos(Math.toRadians(angle))); int y2 = (int)(y + length * Math.sin(Math.toRadians(angle))); g.setColor(new Color((float)Math.random(), (float)Math.random(), (float)Math.random())); g.drawLine(x, y, x2, y2); drawLines(g, x2, y2, length - LENGTH_DECREMENT); } } }