import java.applet.Applet; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; public class FractalApplet extends Applet { // instance variables - replace the example below with your own private ArrayList current = new ArrayList(); private int iterations = 0; private final double XMAX = 5.0; private final double XMIN = -5.0; private final double YMAX = 5.0; private final double YMIN = -5.0; private final double INITIAL_SIZE = 5.0; private int numIterations = 6; private JRadioButton plus = new JRadioButton("Plus"); private JRadioButton circle = new JRadioButton("Circle"); public FractalApplet() { ButtonGroup group = new ButtonGroup(); group.add(plus); group.add(circle); JPanel radioPanel = new JPanel(); radioPanel.add(plus); radioPanel.add(circle); final JTextField parameter = new JTextField(5); final JTextField iter = new JTextField(5); JButton reset = new JButton("Reset"); JButton next = new JButton("Next"); JButton prev = new JButton("Previous"); class optionFieldListener implements ActionListener { private int alterIter = 0; public optionFieldListener(int alterIter_) { alterIter = alterIter_; } public void actionPerformed(ActionEvent event) { if(current.size() != 0) { ((FractalObject)current.get(0)).setParameter(Double.parseDouble(parameter.getText())); } if(alterIter == 0) { numIterations = Integer.parseInt(iter.getText()); } else { numIterations += alterIter; } if(numIterations < 0) numIterations = 0; reset(); repaint(); } } ActionListener resetListener = new optionFieldListener(0); reset.addActionListener(resetListener); ActionListener nextListener = new optionFieldListener(1); next.addActionListener(nextListener); ActionListener prevListener = new optionFieldListener(-1); prev.addActionListener(prevListener); JLabel afLabel = new JLabel("Parameter: "); JLabel iLabel = new JLabel("Iterations: "); JPanel panel = new JPanel(); panel.add(afLabel); panel.add(parameter); panel.add(iLabel); panel.add(iter); panel.add(reset); panel.add(next); panel.add(prev); panel.add(radioPanel); JFrame frame = new JFrame(); frame.setContentPane(panel); frame.pack(); frame.show(); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; double xscale = (getWidth() - 1.0) / (XMAX -XMIN); double yscale = (getHeight() - 1.0) / (YMIN -YMAX); g2.scale(xscale, yscale); g2.translate(-XMIN, -YMAX); g2.setStroke(new BasicStroke(0)); iterations++; int size = current.size(); if(iterations < numIterations) { for(int i = 0; i < size; i++) { current = ((FractalObject)current.get(i)).addChildren(current); } repaint(); } else { for(int i = 0; i < current.size(); i++) { ((FractalObject)current.get(i)).draw(g2); } } } public void reset() { current = new ArrayList(); if(circle.isSelected()) { current.add(new Circle(new Point2D.Double(0.0, 0.0), INITIAL_SIZE)); } else { current.add(new Plus(new Point2D.Double(0.0, 0.0), INITIAL_SIZE)); } iterations = 0; } }