1 /* 2 * File: ShowFile.java 3 * 4 * 5 * This Java applet divides the applet area into two parts: an area 6 * with controls such as buttons and choices, called controlPanelNorth, 7 * and an area called dataPanel which has a TextArea to display 8 * data from a file. The dataPanel is a separate class. 9 * 10 * The data points are read from a file which has the following format: 11 * an integer which is the number of items in the file 12 * items: each item has an integer id no., a name, and a floating 13 * point price 14 * 15 * The data is read once from the file into a set of arrays. Then the 16 * data can be displayed in different ways depending on the controls. 17 * 18 */ 19 20 import java.awt.*; 21 import java.awt.event.*; 22 import java.io.*; 23 import java.net.*; 24 25 public class ShowFile extends java.applet.Applet 26 implements ItemListener, ActionListener 27 { 28 Font f = new Font("TimesRoman", Font.ITALIC, 18); 29 30 DisplayPanel dataPanel; // the display panel, from the class below 31 String inname; // name of the data file 32 33 Choice dataChoice; // control for type of data 34 Button showfile; // display the data under current controls 35 Label title; // label of the display 36 37 public void init() 38 { InputStream instream = null; 39 Panel controlPanelNorth = new Panel(); 40 41 setLayout(new BorderLayout()); 42 43 // initialize the input stream and data panel 44 try 45 { inname = getParameter("inputfile"); 46 instream = new URL(getDocumentBase(), inname).openStream(); 47 dataPanel = new DisplayPanel(instream); 48 } 49 catch (IOException e) 50 { System.err.println 51 ("I/O Exception: probably bad filename or bad connection." 52 + e.getMessage()); 53 } 54 55 // make the control panel 56 controlPanelNorth.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 57 58 title = new Label("Grocery Items"); 59 title.setFont(f); 60 61 dataChoice = new Choice(); 62 dataChoice.setFont(f); 63 dataChoice.addItem("Fruits"); 64 dataChoice.addItem("Veggies"); 65 dataChoice.addItemListener(this); 66 67 showfile = new Button("Show Items"); 68 showfile.setFont(f); 69 showfile.addActionListener(this); 70 71 controlPanelNorth.add(title); 72 controlPanelNorth.add(dataChoice); 73 controlPanelNorth.add(showfile); 74 75 76 // add the two components to the applet area 77 add(controlPanelNorth, BorderLayout.NORTH); 78 add(dataPanel, BorderLayout.CENTER); 79 } 80 81 public void itemStateChanged(ItemEvent e) 82 { 83 if (e.getSource() == dataChoice) 84 { 85 dataPanel.settype (dataChoice.getSelectedItem()); 86 } 87 } 88 89 public void actionPerformed(ActionEvent e) 90 { 91 if (e.getSource() == showfile) 92 { 93 dataPanel.displayfile(); 94 } 95 } 96 } 97 98 class DisplayPanel extends Panel 99 { 100 String datatype = "Fruits"; // either "Fruits" or "Veggies" 101 StringBuffer buff; 102 Font f = new Font("Monospaced", Font.ITALIC, 18); 103 TextArea ta; 104 105 // variables to hold the data from the file 106 int numdata; 107 int itemnumbers[]; 108 String itemnames[]; 109 double itemprices[]; 110 int maxlength; 111 112 InputStreamReader isr; 113 114 public DisplayPanel(InputStream instream) 115 { 116 setLayout(new BorderLayout()); 117 ta = new TextArea("Grocery Item Display Area . . .", 20, 30); 118 add( ta, BorderLayout.CENTER ); 119 120 try 121 { 122 isr = new InputStreamReader(instream); 123 StreamTokenizer intokens = new StreamTokenizer(isr); 124 intokens.parseNumbers(); intokens.slashSlashComments(true); 125 126 /* read the number of data points and allocate data arrays */ 127 int tok = intokens.nextToken(); 128 numdata = (int) intokens.nval; 129 130 itemnumbers = new int[numdata]; 131 itemnames = new String[numdata]; 132 itemprices = new double[numdata]; 133 134 // read data items 135 maxlength = 0; 136 for (int i=0; i<numdata; i++) 137 { tok = intokens.nextToken(); itemnumbers[i] = (int)intokens.nval; 138 tok = intokens.nextToken(); itemnames[i] = intokens.sval; 139 tok = intokens.nextToken(); itemprices[i] = intokens.nval; 140 maxlength = Math.max(maxlength, itemnames[i].length()); 141 } 142 143 /* for debugging 144 for (int i=0; i<numdata; i++) 145 { System.out.println(itemnumbers[i]); 146 System.out.println(itemnames[i]); 147 System.out.println(itemprices[i]); 148 } 149 */ 150 } 151 catch (IOException e) 152 { System.err.println 153 ("I/O Exception: Reading data from file" 154 + e.getMessage()); 155 } 156 } 157 158 public void settype (String s) 159 { datatype = s; 160 } 161 162 public void displayfile() 163 { 164 buff = new StringBuffer(); 165 ta.setFont (f); 166 Color fruitcolor = new Color(127, 0, 0); 167 Color vegcolor = new Color(0, 127, 127); 168 169 for (int i=0; i<numdata; i++) 170 { if (datatype.equals("Fruits") && itemnumbers[i]<=1000) 171 appendtext(fruitcolor, itemnumbers[i], itemnames[i], itemprices[i]); 172 if (datatype.equals("Veggies") && itemnumbers[i]>1000) 173 appendtext(vegcolor, itemnumbers[i], itemnames[i], itemprices[i]); 174 } 175 ta.setText(buff.toString()); 176 } 177 178 public void appendtext (Color c, int n, String s, double d) 179 { 180 ta.setForeground (c); 181 int diff = maxlength - s.length(); 182 183 buff.append( n + " " + s); 184 for (int i=0; i<diff; i++) buff.append(" "); 185 buff.append( " $" + d + "\n"); 186 } 187 }