Yurttas/PL/OOL/Java/F/09/02/00/Gardener00.java

  1import java.awt.*;
  2import java.awt.event.*;
  3
  4// illustrates use of Abstract Factory pattern
  5
  6public class Gardener00 extends Frame
  7                        implements ActionListener {
  8
  9  private Checkbox Veggie,
 10                   Annual,
 11                   Peren;
 12
 13  private Button Center,
 14                 Border,
 15                 Shade,
 16                 Quit;
 17
 18  private Garden garden = null;
 19
 20  private GardenPanel gardenPlot;
 21
 22  private String borderPlant = "",
 23                 centerPlant = "",
 24                 shadePlant = "";
 25
 26  public Gardener00() {
 27    super("Garden planner");
 28    setGUI();
 29  }
 30
 31  private void setGUI() {
 32    setBackground(Color.lightGray);
 33    setLayout(new GridLayout(1,2));
 34    Panel left = new Panel();
 35    add(left);
 36    Panel right= new Panel();
 37    add(right);
 38
 39    //create label and 3 radio buttons on left side
 40    left.setLayout(new GridLayout(4, 1));
 41    left.add(new Label("Garden type"));
 42    CheckboxGroup grp= new CheckboxGroup();
 43    Veggie = new Checkbox("Vegetable", grp, false);
 44    Annual = new Checkbox("Annual", grp, false);
 45    Peren = new Checkbox("Perennial", grp, false);
 46    left.add(Veggie);
 47    left.add(Annual);
 48    left.add(Peren);
 49    Veggie.addItemListener(new VeggieListener());
 50    Peren.addItemListener(new PerenListener());
 51    Annual.addItemListener(new AnnualListener());
 52
 53    //now create right side
 54    right.setLayout(new GridLayout(2,1));
 55    gardenPlot = new GardenPanel();
 56    gardenPlot.setBackground(Color.white);
 57    Panel botRight = new Panel();
 58
 59    right.add(gardenPlot);
 60    right.add(botRight);
 61    Center = new Button("Central");
 62    Border =  new Button("Border");
 63    Shade = new Button("Shade");
 64    Quit = new Button("Quit");
 65    botRight.add(Center);
 66    Center.addActionListener(this);
 67    botRight.add(Border);
 68    Border.addActionListener(this);
 69    botRight.add(Shade);
 70    Shade.addActionListener(this);
 71    botRight.add(Quit);
 72    Quit.addActionListener(this);
 73    setBounds(200,200, 400,300);
 74    setVisible(true);
 75  }
 76
 77  public void actionPerformed(ActionEvent e) {
 78    Object obj = e.getSource();
 79    if(obj == Center)
 80      setCenter();
 81    if(obj == Border)
 82      setBorder();
 83    if(obj == Shade)
 84      setShade();
 85    if(obj == Quit)
 86      System.exit(0);
 87  }
 88
 89  private void setCenter() {
 90    if(garden!=null) centerPlant = garden.getCenter().getName();
 91    gardenPlot.repaint();
 92  }
 93
 94  private void setBorder() {
 95    if(garden!=null) borderPlant = garden.getBorder().getName();
 96    gardenPlot.repaint();
 97  }
 98
 99  private void setShade() {
100    if(garden!=null) shadePlant = garden.getShade().getName();
101    gardenPlot.repaint();
102  }
103
104  private void clearPlants() {
105    shadePlant="";
106    centerPlant="";
107    borderPlant = "";
108    gardenPlot.repaint();
109  }
110
111  static public void main(String argv[]) {
112    new Gardener00();
113  }
114
115  class GardenPanel extends Panel {
116    public void paint(Graphics g) {
117      Dimension sz=getSize();
118      g.setColor(Color.lightGray);
119      g.fillArc(0, 0, 80, 80,0, 360);
120      g.setColor(Color.black);
121      g.drawRect(0,0, sz.width-1, sz.height-1);
122      g.drawString(centerPlant, 100, 50);
123      g.drawString(borderPlant, 75, 120);
124      g.drawString(shadePlant, 10, 40);
125    }
126  }
127
128  class VeggieListener implements ItemListener {
129    public void itemStateChanged(ItemEvent e) {
130      garden = new VeggieGarden();
131      clearPlants();
132    }
133  }
134
135  class PerenListener implements ItemListener {
136    public void itemStateChanged(ItemEvent e) {
137      garden = new PerennialGarden();
138      clearPlants();
139    }
140  }
141
142  class AnnualListener implements ItemListener {
143    public void itemStateChanged(ItemEvent e) {
144      garden = new AnnualGarden();
145      clearPlants();
146    }
147  }
148
149}