/* File: MyPanel.java Author: Stefan Robila Description: This class implements the panel with the canvas for drawing the fractals and the components for managing the drawing Structure of MyPanel (with the description of the members): public class MyPanel extends Applet implements ActionListener, ItemListener { private Panel comPanel; - is in fact the command panel with all the commands for managing the drawing of the fractals private Button helpButton; - a button that will popup a frame with a simple help message private Choice fractals; - a choice component with the list of fractals that can be drawn private Label sizetext; - a label for the depth private CheckboxGroup sizegroup; private Checkbox size0,size1,size2,size3,size4; - the five checkboxes will correspond to 5 levels of depth for drawing of the fractal private FractalCanvas c; - the canvas where the fractals will be drawn and where the information related to them will be printed. For more information about how it works see the file FractalCanvas.java private HelpFrame helpFrame; - the frame where the help message is printed. For more information, see file HelpFrame.java private int shape; - will store the type of fractal that will be currently printed private int depth; - the level of refination of the fractal private LFractal f[]; - an array with the description of the fractals. For more information see file LFractal.java public void init(); - creation of my panel, initialization of the fractal characteristics. public void itemStateChanged (ItemEvent e); - analyzes the events produced by the choice component and by the checkboxes public void actionPerformed ( ActionEvent e); - deals with the event produced by "help" button } */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class MyPanel extends Applet implements ActionListener, ItemListener { private Panel comPanel; private Button helpButton; private Choice fractals; private Label sizetext; private CheckboxGroup sizegroup; private Checkbox size0,size1,size2,size3,size4; private FractalCanvas c; private HelpFrame helpFrame; private int shape; private int depth; private LFractal f[]; public void init() { // shape is the first fractal in the list shape = 0; // depth is 0 that is: first level of refination depth = 0; // the array of fractals f = new LFractal[5]; // for every fractal we have a name, an angle,a ratio, a replacement // character, a start string, a replacing string and the start size // and position f[0]=new LFractal("Koch1",60,0.3333f,'F',"F", "F+F--F+F",0,250,600); f[1]=new LFractal("Koch2",90,0.3333f,'F',"F", "F+F-F-F+F",0,300,600); f[2]=new LFractal("Koch3",90,0.25f,'F',"F", "F-F+F+FF-F-F+F",25,180,528); f[3]=new LFractal("Peano",90,0.25f,'F',"F", "FF-F+F+F+F-F-F-F+F",70,180,400); f[4]=new LFractal("Sierpinsky",90,0.25f,'F',"F", "FFF+F+F+F+FF",10,200,580); // creation of the command panel comPanel = new Panel(); comPanel.setLayout( new FlowLayout() ); // create the list of fractals and add to the comPanel fractals = new Choice(); for (int i=0;i<5;i++) fractals.add(f[i].getName()); fractals.addItemListener(this); comPanel.add(fractals); // add the label for the depth sizetext = new Label("Depth:"); comPanel.add(sizetext); // add the checkboxes for the depth sizegroup = new CheckboxGroup(); size0=new Checkbox("0",sizegroup,true); size0.addItemListener(this); comPanel.add(size0); size1=new Checkbox("1",sizegroup,false); size1.addItemListener(this); comPanel.add(size1); size2=new Checkbox("2",sizegroup,false); size2.addItemListener(this); comPanel.add(size2); size3=new Checkbox("3",sizegroup,false); size3.addItemListener(this); comPanel.add(size3); size4=new Checkbox("4",sizegroup,false); size4.addItemListener(this); comPanel.add(size4); // add the button that will open the help frame helpButton = new Button("Help"); helpButton.addActionListener(this); comPanel.add(helpButton); // set the colors for comPanel comPanel.setBackground(Color.blue); comPanel.setForeground(Color.white); // create the canvas c = new FractalCanvas(); c.setBackground(Color.yellow); c.setForeground(Color.blue); // add comPanel and the canvas to the panel setLayout(new BorderLayout()); add(comPanel,BorderLayout.SOUTH); add(c,BorderLayout.CENTER); c.draw(f[shape],depth); // create the popup frame helpFrame = new HelpFrame("Help for Fractals"); }; // itemStateChanged analyzes, in our case, the events created by the // checkboxes or by the choice list public void itemStateChanged (ItemEvent e){ // when the event is generated by one of the checkboxes, // put the value of depth to be the number corresponding to the // checkbox if (e.getSource() == size0) depth=0; else if (e.getSource() == size1) depth=1; else if (e.getSource() == size2) depth=2; else if (e.getSource() == size3) depth=3; else if (e.getSource() == size4) depth=4; // if the event was generated by the choice list, the index will // indicate wich fractal to draw else if (e.getSource() == fractals) { showStatus("Fractal: "+e.getItem()); shape = fractals.getSelectedIndex(); } // redraw the fractal on the canvas c.draw(f[shape],depth); } // actionPerformed deals here with the event generated by pressing the // "help" button public void actionPerformed ( ActionEvent e){ // when the button is pressed, show the frame if (e.getSource() == helpButton){ helpFrame.pack(); helpFrame.setVisible(true); }; }; }