import java.awt.*;
import java.applet.*;

 class OrderEntryFrameType extends Frame {
 	 	public static void main(String[] arg) {
Image AnImage;
  OrderEntryFrameType BFrame = new OrderEntryFrameType("Order Entry Test App");
AnImage = Toolkit.getDefaultToolkit().getImage("Spalding.gif");
  // Resize the BFrame to width of 300 pixels,
 // And a height of 250 pixels.
 BFrame.resize(430, 500);										     
 BFrame.setup(AnImage);
  // Display the BFrame.
  BFrame.show();

}
	 OrderEntryFrameType (String InTitle) {
        // Call the Frame constructor . .
        super(InTitle);
        // Set the background color for the Applet frame. .
        setBackground(Color.white);
        }
    WarningDialog WDialog = new WarningDialog(this,"You have not entered a name. ");
    Panel Pan = new Panel();
    public void setup(Image LogoImage) {
        // Set the initial grid bag layout for the frame.
        GridBagLayout PrimaryLayout = new GridBagLayout();
        setLayout(PrimaryLayout);
        // Declare the constraints for the Logo.
        GridBagConstraints LogoConstraints = new GridBagConstraints();
        // Construct the new canvas. .
        LogoCanvas Logo = new LogoCanvas(LogoImage);
        LogoConstraints.gridwidth = GridBagConstraints.REMAINDER;
        // Set the constraints for the LogoImage.
        PrimaryLayout.setConstraints(Logo, LogoConstraints);
        // Add the logo canvas to the applet face.
        add(Logo);
        // Declare the constraints for the Logo.
        GridBagConstraints HBarConstraints = new GridBagConstraints();
        // Construct the new canvas. .
        HorizBar ProdBar = new HorizBar("Product", 425);
        // Say that we want the logo to be the last thing on the line.
        HBarConstraints.gridwidth = GridBagConstraints.REMAINDER;
        // Set the constraints for the LogoImage.
        PrimaryLayout.setConstraints(ProdBar, HBarConstraints);
        // Add the logo canvas to the applet face.
        add(ProdBar);
        // Declare and initialize the product panel.
        Panel ProductPanel = new Panel();
        // Set the layout for the product panel and set the constraints for
        // the components inside of the product panel.
        GridBagLayout ProductPanelLayout = new GridBagLayout();
        GridBagConstraints InProductPanelConstraints =
             new GridBagConstraints();
        ProductPanel.setLayout(ProductPanelLayout);
        // Here we will set the list panel, which will hold the
        // list choice method and insertion.
        Panel ListPanel = new Panel();
        ListPanel.setLayout(new BorderLayout());
        Label ProductLabel = new Label("Products");
        ListPanel.add("North", ProductLabel);
        // Create the list, 4 items visible, no multiple
        // selections.
        ProductList = new List(4, false);
        // AddItems to the List.
        ProductList.addItem("Oscar");
        ProductList.addItem("Lionhead");
        ProductList.addItem("Jack Dempsey");
        ProductList.addItem("Angelfish");
        // Add the List to the list panel.
        ListPanel.add("Center",ProductList);
        // Add the imbedded panel to the product panel.
        InProductPanelConstraints.anchor = GridBagConstraints.NORTH;
        ProductPanelLayout.setConstraints(ListPanel,
               InProductPanelConstraints);
ProductPanel.add(ListPanel);
        // Another panel which will be imbedded in the product panel.
        Panel SizePanel = new Panel();
        SizePanel.setLayout(new BorderLayout());
        // Add a label to the choice of sizes.
        SizePanel.add("North", new Label("Size:"));
        // Create the Choice box.
        SizeChoice = new Choice();
        // AddItems to the List.
        SizeChoice.addItem("Jumbo");
        SizeChoice.addItem("Large");
        SizeChoice.addItem("Medium");
        SizeChoice.addItem("Small");
        // Add the Choice to the Applet panel.
             SizePanel.add("Center",SizeChoice);
        // Add the imbedded panel to the product panel.
        ProductPanelLayout.setConstraints(SizePanel,
              InProductPanelConstraints);
        ProductPanel.add(SizePanel);
        // Another panel that will be imbedded in the product panel.
        Panel AmountPanel = new Panel();
        AmountPanel.setLayout(new BorderLayout());
        // Add a label to the slider.
        AmountPanel.add("North", new Label("Amount:"));
        // Another imbedded panel that will contain the slider and the output label.
        Panel SliderPanel = new Panel();
        SliderPanel.setLayout(new FlowLayout());
        // Insert the label that says how many are to be ordered
        // of the item.
        SliderPanel.add(AmountLabel);
        // Create a vertical slider, initial value of 0,
        // minimum value of 0, maximum value of 144.
        OrderAmountSlider = new
              Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 144);
        // Insert the slider to the Applet panel.
        SliderPanel.add(OrderAmountSlider);
        AmountPanel.add("Center", SliderPanel);
        // Add the imbedded panel to the product panel.
        ProductPanelLayout.setConstraints(AmountPanel,
             InProductPanelConstraints);
        ProductPanel.add(AmountPanel);
        // The last panel that will be imbedded in the product panel.
        Panel TotalPanel = new Panel();
        TotalPanel.setLayout(new BorderLayout());
        // Add the subtotal label and a label saying that it is the subtotal.
        TotalPanel.add("North", new Label("Total: "));
        TotalPanel.add("South",TotalLabel);
        ProductPanelLayout.setConstraints(TotalPanel,
             InProductPanelConstraints);
        ProductPanel.add(TotalPanel);
        // Add the imbedded panel to the product panel.
        ProductPanelLayout.setConstraints(TotalPanel,
             InProductPanelConstraints);
        ProductPanel.add(TotalPanel);
        // Set the constraints for the product panel, which will contain product choices, size, etc.
        GridBagConstraints ProductPanelConstraints = new GridBagConstraints();
        // The product panel will take up the rest of the space on this line.
        ProductPanelConstraints.gridwidth = GridBagConstraints.REMAINDER;
        // Set the constraints for the product panel's insertion.
        PrimaryLayout.setConstraints(ProductPanel, ProductPanelConstraints);
        // Add the product panel to the frame.
        add(ProductPanel);
        // Construct the new canvas. .
        HorizBar InfoBar = new HorizBar("Your Information", 425);
        // Say that we want the logo to be the last thing on the line.
        HBarConstraints.gridwidth = GridBagConstraints.REMAINDER;
        // Set the constraints for the LogoImage.
        PrimaryLayout.setConstraints(InfoBar, HBarConstraints);
        // Add the logo canvas to the applet face.
        add(InfoBar);
        // The second panel to be imbedded in the frame is the info panel.
        // This panel gets the information about the user.
        GridBagLayout InfoPanelLayout = new GridBagLayout();
        Panel InfoPanel = new Panel();
        GridBagConstraints InfoPanelConstraints = new GridBagConstraints();
        InfoPanelConstraints.gridwidth = GridBagConstraints.RELATIVE;
        InfoPanel.setLayout(InfoPanelLayout);
        GridBagConstraints InInfoConstraints = new GridBagConstraints();
        InInfoConstraints.gridwidth = GridBagConstraints.REMAINDER;
        InInfoConstraints.anchor = GridBagConstraints.WEST;
        Label InfoLabel = new Label("Your information: ");
        InfoPanelLayout.setConstraints(InfoLabel, InInfoConstraints);
        InfoPanel.add(InfoLabel);
        // The name entry field area.
        Panel NameFieldPanel = new Panel();
        NameFieldPanel.setLayout(new BorderLayout());
        NameFieldPanel.add("West",new Label("Name:"));
        NameFieldPanel.add("East",NameEntryField);
        InInfoConstraints.anchor = GridBagConstraints.EAST;
        InfoPanelLayout.setConstraints(NameFieldPanel,
              InInfoConstraints);
        InfoPanel.add(NameFieldPanel);
        // The Street entry area implementation.
        Panel StreetFieldPanel = new Panel();
        StreetFieldPanel.setLayout(new BorderLayout());
        StreetFieldPanel.add("West",new Label("Street:"));
        StreetFieldPanel.add("East",StreetEntryField);
        InfoPanelLayout.setConstraints(StreetFieldPanel, InInfoConstraints);
        InfoPanel.add(StreetFieldPanel);
        Panel CityFieldPanel = new Panel();
        CityFieldPanel.setLayout(new BorderLayout());
        CityFieldPanel.add("West",new Label("City:"));
        CityFieldPanel.add("East",CityEntryField);
        InfoPanelLayout.setConstraints(CityFieldPanel, InInfoConstraints);
        InfoPanel.add(CityFieldPanel);
        // The zip entry field implementation.
        Panel ZipFieldPanel = new Panel();
        ZipFieldPanel.setLayout(new BorderLayout());
        ZipFieldPanel.add("West",new Label("Zip:"));
        ZipFieldPanel.add("East",ZipEntryField);
        InfoPanelLayout.setConstraints(ZipFieldPanel, InInfoConstraints);
        InfoPanel.add(ZipFieldPanel);
        PrimaryLayout.setConstraints(InfoPanel, InfoPanelConstraints);
        // Add the info panel to the frame layout.
        add(InfoPanel);
        Panel CommentPanel = new Panel();
        GridBagLayout CommentPanelLayout = new GridBagLayout();
        GridBagConstraints CommentPanelConstraints =
             new GridBagConstraints();
        CommentPanelConstraints.gridwidth = GridBagConstraints.REMAINDER;
        CommentPanel.setLayout(CommentPanelLayout);
        GridBagConstraints InCommentConstraints = new GridBagConstraints();
        // The comment label.
        InCommentConstraints.anchor = GridBagConstraints.WEST;
        InCommentConstraints.gridwidth = GridBagConstraints.REMAINDER;
        Label CommentLabel = new Label("Comments: ");
        CommentPanelLayout.setConstraints(CommentLabel, InCommentConstraints);
        CommentPanel.add(CommentLabel);
        // Add the comment box.
        InCommentConstraints.anchor = GridBagConstraints.CENTER;
        InCommentConstraints.gridwidth = GridBagConstraints.REMAINDER;
        CommentPanelLayout.setConstraints(CommentTextArea,
        InCommentConstraints);
        CommentPanel.add(CommentTextArea);
        PrimaryLayout.setConstraints(CommentPanel, CommentPanelConstraints);
        // Add the info panel to the frame layout.
        add(CommentPanel);
// Add and create the repeat customer checkbox.
        Panel ContactPanel = new Panel();
        ContactPanel.setLayout(new BorderLayout());
        // Add a label to the ContactMethodGroup.
        ContactPanel.add("North",new
               Label("How would you like to be contacted? "));
        // Declare the checkbox group, and allocate space.
        CheckboxGroup ContactMethodGroup;
        ContactMethodGroup = new CheckboxGroup();
        // Create some checkboxes to put in the group.
        Checkbox EmailBox = new Checkbox("Email",ContactMethodGroup,true);
        Checkbox PhoneBox = new
             Checkbox("Phone",ContactMethodGroup,false);
        Checkbox MailBox = new
             Checkbox("US Mail",ContactMethodGroup,false);
        // Add the checkboxes into the applet panel.
        ContactPanel.add("West",EmailBox);
        ContactPanel.add("Center",PhoneBox);
        ContactPanel.add("East",MailBox);
        // Sets the constraints for the contact panel.
        GridBagConstraints ContactPanelConstraints =
             new GridBagConstraints();
        ContactPanelConstraints.gridwidth = 2;
        ContactPanelConstraints.weightx = 2.0;
        PrimaryLayout.setConstraints(ContactPanel, ContactPanelConstraints);
        add(ContactPanel);
        // Insert the different checkboxes into the panel.
        GridBagConstraints CustCheckBoxConstraints =  new GridBagConstraints();
        CustCheckBoxConstraints.weightx =1.0;
        CustCheckBoxConstraints.gridwidth = GridBagConstraints.REMAINDER;
        Checkbox RepeatCustCheckBox = new Checkbox("Repeat Customer?");
        PrimaryLayout.setConstraints(RepeatCustCheckBox,
        CustCheckBoxConstraints);
        add(RepeatCustCheckBox);
        // Construct the new canvas. .
        HorizBar ButtonBar = new HorizBar("Commands", 425);
        // Set the constraints for the logoin applet.
        PrimaryLayout.setConstraints(ButtonBar, HBarConstraints);
        // Add the logo canvas to the applet face.
        add(ButtonBar);
        GridBagConstraints ButtonConstraints = new GridBagConstraints();
        ButtonConstraints.gridx = GridBagConstraints.RELATIVE;
        // Spreads the buttons out across the window.
        ButtonConstraints.weightx = 1.0;
        ButtonConstraints.weighty = 1.0;
        // Declare, set, and add the "Submit" button.
        SubmitButton = new Button("Submit");
        PrimaryLayout.setConstraints(SubmitButton, ButtonConstraints);
        add(SubmitButton);
        // Declare, set, and add the "Clear" button.
        ClearButton = new Button("Clear");
        PrimaryLayout.setConstraints(ClearButton, ButtonConstraints);
        add(ClearButton);
        // Set the constraints and insert the quit button.  This button
        // due to the REMAINDER setting will be the last on the line.
        GridBagConstraints LastButtonConstraints = new GridBagConstraints();
        LastButtonConstraints.gridwidth = GridBagConstraints.REMAINDER;
        QuitButton = new Button("Quit");
        PrimaryLayout.setConstraints(QuitButton, LastButtonConstraints);
        add(QuitButton);
        // Method that resets all of the internal values.
        resetValues();
        }
    // The subtotal and total variables.
    private double SubTotalOne = 0.0;
    private double Total = 0.0;
    // The price multipliers for each different product size.
    private double multiplierSmall = 0.5;
    private double multiplierMedium = 1.0;
    private double multiplierLarge = 1.5;
    private double multiplierJumbo = 2.25;
    // The local constant base prices.
    static double ProdOneBaseValue = 1.0;
    static double ProdTwoBaseValue = 1.33;
    static double ProdThreeBaseValue = 1.75;
    static double ProdFourBaseValue = 8.75;
    // Declare all of the variables we'll use.
    private Button SubmitButton;
    private Button ClearButton;
    private Button QuitButton;
    private Checkbox RepeatCustCheckBox;
    private Checkbox MailBox;
    private Checkbox EmailBox;
    private Checkbox PhoneBox;
    private List ProductList;
    private Choice SizeChoice;
    private Scrollbar OrderAmountSlider;
    // The labels that will be variable and change when
    // the other selections are changed.
    private Label SubTotalLabel = new Label("$0.0 ");
    private Label TotalLabel = new Label("$0.0 ");
    private Label AmountLabel = new Label("0 ");
    private Label PricePerItemLabel = new Label("$0.0 ");
    // The entry field for the user to enter his name.
    private TextField NameEntryField = new TextField(25);
    private TextField ZipEntryField = new TextField(6);
    private TextField StreetEntryField = new TextField(25);
    private TextField CityEntryField = new TextField(25);
    // The comment entry area.
    private TextArea CommentTextArea = new TextArea(4, 25);
    // Our own exception . .
    private Exception ZeroValueException;
    private void updateValues() throws Exception {
        int WhichChoice = SizeChoice.getSelectedIndex();
        int AmountSelected = OrderAmountSlider.getValue();
        int WhichProduct = ProductList.getSelectedIndex();
        double CurrentBasePrice = 0.0;
        double CurrentModifier = 0.0;
       if (AmountSelected == 0) {
        throw(ZeroValueException);
       }
        switch (WhichProduct) {
            case 0:
                CurrentBasePrice = ProdOneBaseValue;
                break;
            case 1:
                CurrentBasePrice = ProdTwoBaseValue;
                break;
            case 2:
                CurrentBasePrice = ProdThreeBaseValue;
                break;
            case 3:
                CurrentBasePrice = ProdFourBaseValue;
                break;
        }
        switch (WhichChoice) {
            case 3:
                CurrentModifier = multiplierSmall;
                break;
            case 2:
                CurrentModifier = multiplierMedium;
                break;
            case 1:
                CurrentModifier = multiplierLarge;
                break;
            case 0:
                CurrentModifier = multiplierJumbo;
                break;
        }
        AmountLabel.setText(Integer.toString(AmountSelected)+ " ");
        double PricePerItem = (CurrentBasePrice*CurrentModifier);
        PricePerItemLabel.setText(Double.toString(PricePerItem)+ " ");
        double SubTotal = (CurrentBasePrice*CurrentModifier*AmountSelected);
        SubTotalLabel.setText(Double.toString(SubTotal)+ " ");
        TotalLabel.setText(SubTotalLabel.getText());
    }
    // This method will be called when the user presses the "Clear" button and
    // also when the applet is initialized in the init() method.
    public void resetValues() {
        // Reset all of these labels to zero.
        SubTotalLabel.setText("$0.0 ");
        TotalLabel.setText("$0.0 ");
        AmountLabel.setText("0 ");
        PricePerItemLabel.setText("$0.0 ");
        // Clear all of the lists and choices.
        ProductList.select(0);
        SizeChoice.select(0);
        OrderAmountSlider.setValue(0);
        // Clear all of the text fields.
        NameEntryField.setText("");
        StreetEntryField.setText("");
        CityEntryField.setText("");
        ZipEntryField.setText("");
    }
    public boolean handleEvent(Event InEvent) {
        if (InEvent.id == Event.SCROLL_LINE_UP ||
            InEvent.id == Event.SCROLL_LINE_DOWN)  {
                try {
                     updateValues();
                } catch (Exception e) {}
        } else
        if (InEvent.target == ProductList) {
                try {
                     updateValues();
                } catch (Exception e) {}
        } else
        if (InEvent.target == ClearButton) {
                    resetValues();
        } else
        if (InEvent.target == QuitButton)  {
        // Quit the applet.
            this.dispose();
        } else
        if (InEvent.target == SubmitButton) {
         // Submit the order.
        }
        return super.handleEvent(InEvent);
    }
    public boolean action (Event InEvent, Object SomeObject) {
        if (InEvent.target == SizeChoice) {
                try {
                     updateValues();
                } catch (Exception e) {}
        return true;
        }
        else
        if (InEvent.target == NameEntryField) {
            // Is the field empty?  If so, you could add a
            // pop-up dialog box to alert the user that he
            // has not entered his name.
            WDialog.show();
            return true;
            } else
        return false;
    }
}
class WarningDialog extends Dialog {
    private Button OkButton = new Button("OK");
    private Label ALabel;
    WarningDialog(Frame HostFrame, String Message)  {
        super(HostFrame, "Warning!", false);
        ALabel = new Label(Message);
        resize(180,100);
        setLayout(new FlowLayout());
        add(ALabel);
        add(OkButton);
    }
    public boolean action (Event InEvent, Object Param)  {
        if (InEvent.target == OkButton)  {
            hide();
        }
        return true;
    }
}
class LogoCanvas extends Canvas {
    private Image LogoImage;
    LogoCanvas (Image LogoInCanvas) {
         // Get the image and place it in LogoImage. .
         LogoImage = LogoInCanvas;
         // Resize the canvas to fit the Logo exactly.
        resize (425, 87);
    }
    public void paint(Graphics g)  {
        // Draw the logo on the canvas.
        g.drawImage(LogoImage,0,0,this);
    }
}
class HorizBar extends Canvas {
    private String LineString;
    private int Width;
    HorizBar(String InString, int InWidth) {
        // Set the size of the canvas.
        resize(InWidth, 25);
        // Set the local variables equal to parameters so that
        // we can use the values in the paint method.
        Width = InWidth;
        LineString = InString;
    }
    public void paint(Graphics g)  {
        // Set the font and font metrics class.
        Font f = new Font("TimesRoman", Font.BOLD, 16);
        FontMetrics FM = getFontMetrics(f);
        g.setFont(f);
        // Draw a line from x = 0 to x = 15.
        g.drawLine(0, 20, 15, 20);
        // Draw the string.
        g.drawString(LineString, 20, 20);
        // Draw the rest of the line.
        g.drawLine(FM.stringWidth(LineString) + 25, 20, Width, 20);
    }
}
