/* * Java pizza applet using CGI scripts and GridBagLayout */ import java.awt.*; import java.applet.*; import java.util.*; import java.net.*; import java.io.*; public class pizza extends Applet { private Choice yesNo; private Choice name; private TextField addressField = new TextField(); private TextField phoneField = new TextField(); private TextField pizzaField = new TextField(); private CheckboxGroup topping; private CheckboxGroup Ordertopping; private String str; private TextArea TAspecials; private TextArea TAorder; private void add(Component c, GridBagLayout gbl, GridBagConstraints gbc, int x, int y, int w, int h) { gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; gbl.setConstraints(c, gbc); add(c); } public void init() { Component c; resize(600,1000); GridBagLayout gbl=new GridBagLayout(); setLayout(gbl); GridBagConstraints gbc= new GridBagConstraints(); gbc.fill=GridBagConstraints.BOTH; gbc.weightx=100; gbc.weighty=100; setFont(new Font("TimesRoman", Font.BOLD, 16)); int yoff=1; c=new Label("Fill-Out Form Example"); c.setFont(new Font("TimesRoman", Font.BOLD, 34)); add(c,gbl,gbc,0,0,4,2); c=new Label(" A fill-out form example, with multiple text entry fields and radio buttons.."); c.setFont(new Font("Curier", Font.BOLD, 13)); add(c,gbl,gbc,0,2,4,1); c=new Label("Internet Pizza Delivery Service"); c.setFont(new Font("TimesRoman", Font.BOLD, 24)); add(c,gbl,gbc,0,3,4,1); c=new Label(" ===================================================================================================================="); c.setFont(new Font("TimesRoman", Font.BOLD, 8)); add(c,gbl,gbc,0,4,4,1); /*_____________________________________________________________*/ yoff=5; add(new Label(" "),gbl,gbc,0,yoff,4,1); c=new Label("Would you like to view (today's) specials?"); c.setFont(new Font("TimesRoman", Font.BOLD, 18)); c.setForeground(Color.red); add(c,gbl,gbc,0,yoff+1,4,1); gbc.fill=GridBagConstraints.NONE; yesNo= new Choice(); yesNo.setFont(new Font("TimesRoman", Font.BOLD, 16)); yesNo.addItem("Yes"); yesNo.addItem("No"); add(yesNo,gbl,gbc,1,yoff+2,1,1); gbc.fill=GridBagConstraints.VERTICAL; Button vs=new Button("View Specials"); c=vs; c.setBackground(Color.yellow); c.setForeground(Color.blue); add(vs,gbl,gbc,2,yoff+2,1,1); gbc.fill=GridBagConstraints.BOTH; TAspecials=new TextArea(10,40); add(TAspecials,gbl,gbc,0,yoff+3,4,4); /*______________________________________________________________*/ yoff=yoff+9; add(new Label(" "),gbl,gbc,0,yoff-1,4,1); c=new Label("Order Form"); c.setFont(new Font("TimesRoman", Font.BOLD, 18)); c.setForeground(Color.red); add(c,gbl,gbc,0,yoff,4,1); Panel address =new Panel(); address.setLayout(new GridLayout(3,2)); address.setFont(new Font("TimesRoman", Font.BOLD, 14)); address.add(new Label("Type in your street address:")); address.add(addressField=new TextField(20)); address.add(new Label("Type in your phone number:")); address.add(phoneField=new TextField(10)); address.add(new Label("Choose a special pizza:")); address.add(pizzaField=new TextField(20)); add(address,gbl,gbc,0,yoff+1,4,4); c=new Label("Or you may prefer to choose a topping for your pizza: "); c.setFont(new Font("TimesRoman", Font.BOLD, 15)); add(c,gbl,gbc,0,yoff+5,4,1); Panel pTop2=new Panel(); pTop2.setLayout(new GridLayout(3,1)); Ordertopping = new CheckboxGroup(); pTop2.add(new Checkbox("Pepperoni",Ordertopping,true)); pTop2.add(new Checkbox("Sausage",Ordertopping,false)); pTop2.add(new Checkbox("Anchovy",Ordertopping,false)); gbc.fill=GridBagConstraints.NONE; add(pTop2,gbl,gbc,2,yoff+6,2,2); gbc.fill=GridBagConstraints.HORIZONTAL; c=new Button("Order Pizza"); c.setBackground(Color.yellow); add(c,gbl,gbc,0,yoff+7,1,1); gbc.fill=GridBagConstraints.BOTH; TAorder=new TextArea(10,40); add(TAorder,gbl,gbc,0,yoff+8,4,4); add(new Label(" "),gbl,gbc,0,yoff+12,4,1); c=new Label("class example. Please send comments to njm@npac.syr.edu. 2/28/96."); c.setFont(new Font("Courier", Font.BOLD, 12)); c.setForeground(Color.green); add(c,gbl,gbc,0,yoff+13,4,1); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { if (arg.equals("View Specials")) { handleViewSpecials(); } else if (arg.equals("Order Pizza")) { handleOrderPizza(); } } /* button case */ else if (evt.target instanceof Checkbox) { /* do nothing */ } else if (evt.target instanceof Choice ) { /* do nothing */ } else return super.action(evt, arg); return true; } public void handleViewSpecials() { String sdata; String rdata; sdata= "todayspecials=" + yesNo.getSelectedItem(); String Script="/users-cgi/njm/Java/javapizzaspecials.pl"; rdata=Network(sdata,Script); TAspecials.setText(rdata); } public void handleOrderPizza() { String sdata; String rdata; Checkbox c=Ordertopping.getCurrent(); sdata = "address=" + addressField.getText() + "&phone=" + phoneField.getText() + "&specialorder="+pizzaField.getText() + "&topping="+c.getLabel(); String Script="/users-cgi/njm/Java/javapizza2.pl"; rdata=Network(sdata,Script); TAorder.setText(rdata); } public String Network(String sdata,String script) { String home = "osprey7.npac.syr.edu"; int port = 3768; Socket s = null; String rdata = ""; /* return sdata; */ try { s = new Socket(home, port); DataOutputStream os = new DataOutputStream(s.getOutputStream()); DataInputStream is = new DataInputStream(s.getInputStream()); os.writeBytes("POST " + script + " HTTP/1.0\r\n" + "Content-type: application/octet-stream\r\n" + "Content-length: " + sdata.length() + "\r\n\r\n"); os.writeBytes(sdata); String line=""; while (((line = is.readLine()) != null) && !(line.startsWith("Content-type:"))) { }; while ((line = is.readLine()) != null) { rdata += line + "\n";} showStatus(rdata); is.close(); os.close(); } catch (Exception e) { showStatus("Error " + e); if (s != null) try { s.close(); } catch (IOException ex) {} } return rdata; } }