// -------------------------------------------------------
// Some code that may be of help to students programming
// the "Fast-Food" applet (homework 6).
// 
// 605.201.31 Introduction to Programming Using Java
//   (http://apl.jhu.edu/~paulmac/intro-java.html)
//
// 7/28/98  Paul McNamee
// -------------------------------------------------------

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

// -------------------------------------------------------
// A helpful supporting class.

class Food {
  String name;
  int price;
  String getName() { return name; }
  int getPrice() { return price; }
  Food (String _name, int _price) {
    name = _name;
    price = _price;
  }
}

// -------------------------------------------------------
// Each handler instance, will be able to perform some action
// event generated by a widget (i.e. button).  Each handler has
// it's own associated food, and a reference to the McTofu
// object so it can call, drawFood.
// 
// For HW #6, your event handling method will look a little
// different, but this may give you some ideas about how the
// Food class may be useful


class McTofuHandler implements ActionListener {
  McTofu theApplet;  // A McTofu is a kind of Applet
  Food tasty;

  public McTofuHandler(Food f, McTofu mtf) {
    tasty = f;
    theApplet = mtf;
  }

  // When a button is clicked, certain actions are desireable.
  // Probably the same things should be done for all foods.
  // An item should be added to the list and the total price
  // should be incremented.
  // 
  // As an example, I just draw a string (leaving the rest for
  // hw6 

  public void actionPerformed(ActionEvent e) {
    theApplet.drawFood(tasty);
  }
}

// -------------------------------------------------------
// This is little more lengthy that needed but I wanted to
// also give an example of BorderLayout and layer.  And
// I threw in a Canvas example for kicks.

public class McTofu extends Applet {
  Button [] buttons;
  Food [] menu;
  Canvas drawingArea;

  public void init() {
    setBackground(Color.yellow);
    setForeground(Color.red);
    setLayout(new BorderLayout());

    // Design a menu (could be a class instead of an array, but
    // I'll keep it simple)
    menu = new Food [4];
    menu[0] = new Food("Coffee", 50);
    menu[1] = new Food("Pepsi", 75);
    menu[2] = new Food("McTofu Burger", 350);
    menu[3] = new Food("Pizza w/ mushrooms", -100);

    // Create buttons
    buttons = new Button [menu.length];
    Panel centerPanel = new Panel();
    for(int i=0; i<buttons.length; i++) {
      buttons[i] = new Button(menu[i].getName());
      buttons[i].addActionListener(new McTofuHandler(menu[i], this));
      // add button to center panel
      centerPanel.add(buttons[i]);
    }
    // now add panel (which contains buttons) to center area
    add(centerPanel, "Center");

    // A canvas is surface that can be drawn on
    drawingArea = new Canvas();
    drawingArea.setBackground(Color.red);
    drawingArea.setForeground(Color.yellow);
    drawingArea.setSize(300, 100);
    add(drawingArea, "South");
  }

  // This method is called by a handler when a food is selected
  public void drawFood(Food f) {
    Graphics g = drawingArea.getGraphics();
    // Not in 1.1?? drawingArea.getWidth(),drawingArea.getHeight());
    g.clearRect(0,0,300,100);
    g.drawString(f.getName() + " costs " + f.getPrice() + " cents.",
                 25, 50);
  }
}

