/* HostApplet.java * Ian Barland ian@cs.rice.edu * 96.Dec.18 Comp210 * The applet version of the game-show host for Trivia * (see http://www.owlnet.rice.edu/~comp210/Assignments/12/12.html). * * Use this file with your hw12 to put your quiz onto your own web page. * You don't need to change your class Judge, StringList, QandA, QandA_List. * However, the driver class Trivia isn't needed any more (that approach * didn't mesh well with the event-driven style). * * To run the quiz show as an applet on your own personal web page: * (1) Copy HostApplet.java to your html directory, as well as * your other classes for hw12: Judge, StringList, QandA, QandA_List. * (HostApplet.java resides in ~comp210/public_html/Projects/Trivia.) * (2) Modify HostApplet.java to personalize the questions, * (3) Compile the .java files to .class files, and * (4) Add lines * * * to your web page as discussed in lab14. * Note that for the applet to run, the needed .class files really do * need to reside in the same directory as your .html file :-( * * If you have a fun quiz, or spruce up this interface, let me & matthias * know about it! -- ian */ import java.applet.Applet; import java.awt.*; public class HostApplet extends Applet { // Some variables for the HostApplet. // (declared private since no other objects have the right to know these.) // private Judge judge; private String rightMsg = "Got it!"; // Message when player guesses right, private String wrongMsg = "Nope."; // or guesses wrong, private String byebyeMsg = "Thanks for playing."; // or finishes. private TextField questionSite; // The box containing the question private java.awt.List answerSite; // The box containing list-of-answers /* (Note the use of the full class name, instead of just List, * to avoid any confusion with any class List of our own.) */ /* HostApplet constructor */ public HostApplet() { /* Don't do anything applet/graphics-dependent in the constructor! * Do such things in init(), instead. */ // Formulate the list of questions // QandA_List qaList = new QandA_Null(); qaList = addQA( qaList, "Who's on third?", "I don't know." ); qaList = addQA( qaList, "Which came first, chicken or egg?", "dinosaur egg" ); qaList = addQA( qaList, "A synonym for yellow", "chicken" ); qaList = addQA( qaList, "English for 'oeuf'.", "egg" ); judge = new Judge( qaList ); } /* addQA * Take a list of QandA; tack a new QandA on the front. */ private QandA_List addQA( QandA_List l, String q, String a ) { return new QandA_Cons( new QandA( q, a ), l ); } /* init() * The Applet/window-manager initialization. * Here is where to do all graphical interface setup. */ public void init() { super.init(); // call the init() for class Applet (whom we extend) questionSite = new TextField( 60 ); // Create the text gizmo. questionSite.setEditable( false ); // Customize it. add( questionSite ); // Add it to the window. answerSite = new java.awt.List(); // Create text-list gizmo. answerSite.setMultipleSelections( false ); // Customize it. add( answerSite ); // Add it to the window. // Put our answers into the answerSite, and set up initial question: setAnswers( judge.getAnswerList() ); questionSite.setText( judge.nextQuestion() ); } /* handleEvent * The heart of the applet: * If user is guessing at an answer, respond & pose next question. */ public boolean handleEvent( Event event ) { if (event.target != answerSite) // Ignore any events besides the guessing of an answer. return false; respondTo( answerSite.getSelectedItem() ); answerSite.deselect( answerSite.getSelectedIndex() ); boolean carryOn = setupNextQuestion(); if (!carryOn) { answerSite.clear(); // Get rid of all old questions System.exit(0); // Hmm, this has no effect. How should applets exit? } return true; // We've handled the answerSite event. } /* respondTo * Ask judge if "guess" was correct; praise/disparage accordingly. */ private void respondTo( String guess ) { String response; if (judge.rightAnswer( guess )) response = rightMsg; else response = wrongMsg; showStatus( response ); // Show on netscape's bottom line. } /* setupNextQuestion * Select and display the next question. * Return false if the user is done or wants to quit. */ private boolean setupNextQuestion() { String nextQ = judge.nextQuestion(); boolean finito = nextQ.equals( "" ); // Judge's way of saying "game over" if (finito) nextQ = byebyeMsg; questionSite.setText( nextQ ); // Pose the new question. return !finito; } /* setAnswers * Change the list of answers. */ public void setAnswers( StringList answers ) { answerSite.clear(); // Get rid of all old questions // Walk through list of answers, adding to answerSite. for ( StringList as = answers; as instanceof StringCons; as = ((StringCons)as).cdr ) { answerSite.addItem( ((StringCons)as).car ); } } boolean debug = false; private void debug( String msg ) { if (debug) showStatus( "HostApplet debug: " + msg ); } }