1 /* 2 * Java pizza applet using CGI scripts and GridBagLayout 3 */ 4 5 import java.awt.*; 6 import java.applet.*; 7 import java.util.*; 8 import java.net.*; 9 import java.io.*; 10 11 public class pizza extends Applet 12 { 13 private Choice yesNo; 14 private Choice name; 15 16 17 private TextField addressField = new TextField(); 18 private TextField phoneField = new TextField(); 19 private TextField pizzaField = new TextField(); 20 21 private CheckboxGroup topping; 22 private CheckboxGroup Ordertopping; 23 private String str; 24 private TextArea TAspecials; 25 private TextArea TAorder; 26 27 28 private void add(Component c, 29 GridBagLayout gbl, GridBagConstraints gbc, 30 int x, int y, int w, int h) 31 32 { gbc.gridx = x; 33 gbc.gridy = y; 34 gbc.gridwidth = w; 35 gbc.gridheight = h; 36 gbl.setConstraints(c, gbc); 37 add(c); 38 } 39 40 41 42 public void init() 43 { 44 Component c; 45 resize(600,1000); 46 GridBagLayout gbl=new GridBagLayout(); 47 setLayout(gbl); 48 GridBagConstraints gbc= new GridBagConstraints(); 49 gbc.fill=GridBagConstraints.BOTH; 50 gbc.weightx=100; 51 gbc.weighty=100; 52 setFont(new Font("TimesRoman", Font.BOLD, 16)); 53 int yoff=1; 54 55 56 c=new Label("Fill-Out Form Example"); 57 c.setFont(new Font("TimesRoman", Font.BOLD, 34)); 58 add(c,gbl,gbc,0,0,4,2); 59 60 c=new Label(" A fill-out form example, with multiple text entry fields and radio buttons.."); 61 c.setFont(new Font("Curier", Font.BOLD, 13)); 62 add(c,gbl,gbc,0,2,4,1); 63 64 65 c=new Label("Internet Pizza Delivery Service"); 66 c.setFont(new Font("TimesRoman", Font.BOLD, 24)); 67 add(c,gbl,gbc,0,3,4,1); 68 69 c=new Label(" ===================================================================================================================="); 70 c.setFont(new Font("TimesRoman", Font.BOLD, 8)); 71 add(c,gbl,gbc,0,4,4,1); 72 73 /*_____________________________________________________________*/ 74 yoff=5; 75 add(new Label(" "),gbl,gbc,0,yoff,4,1); 76 c=new Label("Would you like to view (today's) specials?"); 77 c.setFont(new Font("TimesRoman", Font.BOLD, 18)); 78 c.setForeground(Color.red); 79 add(c,gbl,gbc,0,yoff+1,4,1); 80 81 82 gbc.fill=GridBagConstraints.NONE; 83 yesNo= new Choice(); 84 yesNo.setFont(new Font("TimesRoman", Font.BOLD, 16)); 85 yesNo.addItem("Yes"); 86 yesNo.addItem("No"); 87 add(yesNo,gbl,gbc,1,yoff+2,1,1); 88 89 gbc.fill=GridBagConstraints.VERTICAL; 90 Button vs=new Button("View Specials"); 91 c=vs; 92 c.setBackground(Color.yellow); 93 c.setForeground(Color.blue); 94 add(vs,gbl,gbc,2,yoff+2,1,1); 95 96 gbc.fill=GridBagConstraints.BOTH; 97 TAspecials=new TextArea(10,40); 98 add(TAspecials,gbl,gbc,0,yoff+3,4,4); 99 100 /*______________________________________________________________*/ 101 yoff=yoff+9; 102 103 add(new Label(" "),gbl,gbc,0,yoff-1,4,1); 104 c=new Label("Order Form"); 105 c.setFont(new Font("TimesRoman", Font.BOLD, 18)); 106 c.setForeground(Color.red); 107 add(c,gbl,gbc,0,yoff,4,1); 108 109 110 Panel address =new Panel(); 111 address.setLayout(new GridLayout(3,2)); 112 address.setFont(new Font("TimesRoman", Font.BOLD, 14)); 113 address.add(new Label("Type in your street address:")); 114 address.add(addressField=new TextField(20)); 115 address.add(new Label("Type in your phone number:")); 116 address.add(phoneField=new TextField(10)); 117 address.add(new Label("Choose a special pizza:")); 118 address.add(pizzaField=new TextField(20)); 119 add(address,gbl,gbc,0,yoff+1,4,4); 120 c=new Label("Or you may prefer to choose a topping for your pizza: "); 121 c.setFont(new Font("TimesRoman", Font.BOLD, 15)); 122 add(c,gbl,gbc,0,yoff+5,4,1); 123 124 Panel pTop2=new Panel(); 125 pTop2.setLayout(new GridLayout(3,1)); 126 Ordertopping = new CheckboxGroup(); 127 pTop2.add(new Checkbox("Pepperoni",Ordertopping,true)); 128 pTop2.add(new Checkbox("Sausage",Ordertopping,false)); 129 pTop2.add(new Checkbox("Anchovy",Ordertopping,false)); 130 gbc.fill=GridBagConstraints.NONE; 131 add(pTop2,gbl,gbc,2,yoff+6,2,2); 132 133 134 gbc.fill=GridBagConstraints.HORIZONTAL; 135 c=new Button("Order Pizza"); 136 c.setBackground(Color.yellow); 137 add(c,gbl,gbc,0,yoff+7,1,1); 138 139 140 141 gbc.fill=GridBagConstraints.BOTH; 142 TAorder=new TextArea(10,40); 143 add(TAorder,gbl,gbc,0,yoff+8,4,4); 144 145 add(new Label(" "),gbl,gbc,0,yoff+12,4,1); 146 c=new Label("class example. Please send comments to njm@npac.syr.edu. 2/28/96."); 147 c.setFont(new Font("Courier", Font.BOLD, 12)); 148 c.setForeground(Color.green); 149 add(c,gbl,gbc,0,yoff+13,4,1); 150 151 152 } 153 154 public boolean action(Event evt, Object arg) 155 { 156 if (evt.target instanceof Button) { 157 if (arg.equals("View Specials")) 158 { handleViewSpecials(); } 159 160 else if (arg.equals("Order Pizza")) 161 { handleOrderPizza(); } 162 } /* button case */ 163 164 else if (evt.target instanceof Checkbox) { 165 /* do nothing */ 166 } 167 else if (evt.target instanceof Choice ) { 168 /* do nothing */ 169 } 170 else return super.action(evt, arg); 171 return true; 172 } 173 174 public void handleViewSpecials() 175 { 176 String sdata; 177 String rdata; 178 sdata= "todayspecials=" + yesNo.getSelectedItem(); 179 String Script="/users-cgi/njm/Java/javapizzaspecials.pl"; 180 rdata=Network(sdata,Script); 181 TAspecials.setText(rdata); 182 } 183 184 public void handleOrderPizza() 185 { 186 String sdata; 187 String rdata; 188 Checkbox c=Ordertopping.getCurrent(); 189 190 sdata = "address=" + addressField.getText() 191 + "&phone=" + phoneField.getText() 192 + "&specialorder="+pizzaField.getText() 193 + "&topping="+c.getLabel(); 194 String Script="/users-cgi/njm/Java/javapizza2.pl"; 195 rdata=Network(sdata,Script); 196 TAorder.setText(rdata); 197 198 } 199 200 public String Network(String sdata,String script) 201 { String home = "osprey7.npac.syr.edu"; 202 203 int port = 3768; 204 Socket s = null; 205 String rdata = ""; 206 /* return sdata; */ 207 try 208 { 209 s = new Socket(home, port); 210 211 DataOutputStream os 212 = new DataOutputStream(s.getOutputStream()); 213 DataInputStream is 214 = new DataInputStream(s.getInputStream()); 215 216 os.writeBytes("POST " + script 217 + " HTTP/1.0\r\n" 218 + "Content-type: application/octet-stream\r\n" 219 + "Content-length: " 220 + sdata.length() + "\r\n\r\n"); 221 os.writeBytes(sdata); 222 223 String line=""; 224 while (((line = is.readLine()) != null) 225 && !(line.startsWith("Content-type:"))) 226 { }; 227 228 while ((line = is.readLine()) != null) 229 { rdata += line + "\n";} 230 showStatus(rdata); 231 is.close(); 232 os.close(); 233 } 234 catch (Exception e) 235 { showStatus("Error " + e); 236 if (s != null) 237 try 238 { s.close(); } 239 catch (IOException ex) {} 240 } 241 return rdata; 242 } 243 244 } 245 246 247