import java.awt.*; import java.io.*; import java.net.*; import java.awt.event.*; public class TVSchedule extends java.applet.Applet implements ItemListener { Font f = new Font( "Arial Black", Font.ITALIC, 15 ); DisplayPanel dataPanel; // the display panel String file; // the data file Label title; // label of the display CheckboxGroup dayOfWeek; // days of week for data Checkbox monday, tuesday, wednesday, // individual checkboxes thursday, friday, saturday, sunday; public void init() { InputStream instream = null; Panel controlPanelNorth = new Panel(); setLayout( new BorderLayout() ); // initialize the input stream and data panel try { file = getParameter( "input" ); instream = new URL( getDocumentBase(), file ).openStream(); dataPanel = new DisplayPanel( instream ); } catch ( IOException e ) { System.err.println ( "I/O Exception: probably bad filename or bad connection." + e.getMessage() ); } // make the control panel controlPanelNorth.setLayout( new FlowLayout( FlowLayout.LEFT, 5, 5) ); title = new Label( "TV Listings" ); title.setFont( f ); dayOfWeek = new CheckboxGroup(); Panel DaysOfWeekPanel = new Panel(); DaysOfWeekPanel.setLayout( new GridLayout( 7, 1 ) ); monday = new Checkbox( "Monday", dayOfWeek, true ); monday.addItemListener( this ); DaysOfWeekPanel.add( monday ); tuesday = new Checkbox( "Tuesday", dayOfWeek, false ); tuesday.addItemListener( this ); DaysOfWeekPanel.add( tuesday ); wednesday = new Checkbox( "Wednesday", dayOfWeek, false ); wednesday.addItemListener( this ); DaysOfWeekPanel.add( wednesday ); thursday = new Checkbox( "Thursday", dayOfWeek, false ); thursday.addItemListener( this ); DaysOfWeekPanel.add( thursday ); friday = new Checkbox( "Friday", dayOfWeek, false ); friday.addItemListener( this ); DaysOfWeekPanel.add( friday ); saturday = new Checkbox( "Saturday", dayOfWeek, false ); saturday.addItemListener( this ); DaysOfWeekPanel.add( saturday ); sunday = new Checkbox( "Sunday", dayOfWeek, false ); sunday.addItemListener( this ); DaysOfWeekPanel.add( sunday ); controlPanelNorth.add( title ); // add the two components to the applet area add( "East", DaysOfWeekPanel ); add( "North", controlPanelNorth ); add( "Center", dataPanel ); } public void itemStateChanged( ItemEvent e ) { if ( e.getSource() == monday ) { dataPanel.settype( "Monday" ); } else if ( e.getSource() == tuesday ) { dataPanel.settype( "Tuesday" ); } else if ( e.getSource() == wednesday ) { dataPanel.settype( "Wednesday" ); } else if ( e.getSource() == thursday ) { dataPanel.settype( "Thursday" ); } else if ( e.getSource() == friday ) { dataPanel.settype( "Friday" ); } else if ( e.getSource() == saturday ) { dataPanel.settype( "Saturday" ); } else if ( e.getSource() == sunday ) { dataPanel.settype( "Sunday" ); } dataPanel.displayfile(); } } class DisplayPanel extends Panel { String datatype = "Days"; StringBuffer buff; Font f = new Font( "Gothic", Font.ITALIC, 18 ); TextArea ta; // variables to hold the data from the file int numdata; int showID[]; String TVshowNames[]; String showTimes[]; String showChannel[]; int maxlength; public DisplayPanel( InputStream instream ) { setLayout( new BorderLayout() ); ta = new TextArea( "TV listings for the week . . .", 10, 30 ); ta.setFont ( f ); add( "Center", ta ); try { StreamTokenizer intokens = new StreamTokenizer( instream ); intokens.parseNumbers(); intokens.slashSlashComments( true ); int tok = intokens.nextToken(); numdata = (int) intokens.nval; showID = new int[numdata]; TVshowNames = new String[numdata]; showTimes = new String[numdata]; showChannel = new String[numdata]; // read data items maxlength = 0; for ( int i = 0; i < numdata; i++ ) { tok = intokens.nextToken(); showID[i] = (int)intokens.nval; tok = intokens.nextToken(); TVshowNames[i] = intokens.sval; tok = intokens.nextToken(); showTimes[i] = intokens.sval; tok = intokens.nextToken(); showChannel[i] = intokens.sval; maxlength = Math.max( maxlength, TVshowNames[i].length() ); } // for debugging for ( int i = 0; i < numdata; i++ ) { System.out.println( showID[i] ); System.out.println( TVshowNames[i] ); System.out.println( showTimes[i] ); System.out.println( showChannel[i] ); } } catch ( IOException e ) { System.err.println ( "I/O Exception: Reading data from file" + e.getMessage() ); } } public void settype ( String s ) { datatype = s; } public void displayfile() { buff = new StringBuffer(); Color MondayColor = new Color( 127, 0, 0 ); Color TuesdayColor = new Color( 0, 127, 127 ); Color WednesdayColor = new Color( 0, 127, 0 ); Color ThursdayColor = new Color( 0, 0, 127 ); Color FridayColor = new Color( 127, 0, 127 ); Color SaturdayColor = new Color( 127, 127, 127 ); Color SundayColor = new Color( 127, 127, 0 ); for ( int i = 0; i < numdata; i++ ) { if ( datatype.equals( "Monday" ) && showID[i] <= 100 ) appendtext( MondayColor, TVshowNames[i], showTimes[i], showChannel[i] ); if ( datatype.equals( "Tuesday" ) && ( ( showID[i] > 100 ) && ( showID[i] <= 200 ) ) ) appendtext( TuesdayColor, TVshowNames[i], showTimes[i], showChannel[i] ); if ( datatype.equals( "Wednesday" ) && ( ( showID[i] > 200 ) && ( showID[i] <= 300 ) ) ) appendtext( WednesdayColor, TVshowNames[i], showTimes[i], showChannel[i] ); if ( datatype.equals( "Thursday" ) && ( ( showID[i] > 300 ) && ( showID[i] <= 400 ) ) ) appendtext( ThursdayColor, TVshowNames[i], showTimes[i], showChannel[i] ); if ( datatype.equals( "Friday" ) && ( ( showID[i] > 400 ) && ( showID[i] <= 500 ) ) ) appendtext( FridayColor, TVshowNames[i], showTimes[i], showChannel[i] ); if ( datatype.equals( "Saturday" ) && ( ( showID[i] > 500 ) && ( showID[i] <= 600 ) ) ) appendtext( SaturdayColor, TVshowNames[i], showTimes[i], showChannel[i] ); if ( datatype.equals( "Sunday" ) && ( ( showID[i] > 600 ) && ( showID[i] <= 700 ) ) ) appendtext( SundayColor, TVshowNames[i], showTimes[i], showChannel[i] ); } ta.setText( buff.toString() ); } public void appendtext ( Color c, String s, String t, String h ) { ta.setForeground ( c ); int diff = maxlength - s.length(); buff.append( s + " "); for ( int i = 0; i < diff; i++ ) buff.append( " " ); buff.append( " " + t ); buff.append( " " + h + "\n" ); } }