/* * Learn.java * * Ken Shirriff ken.shirriff@eng.sun.com * * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * Please refer to the file "license.txt" * for further important copyright and licensing information. */ import java.awt.*; import java.awt.image.*; import java.applet.Applet; import java.net.*; import java.util.*; import java.io.*; import pcffont.*; public class Learn extends Applet { PCFFont lfont; // Current font for left entry in lesson PCFFont rfont; // Current font for right entry PCFFont tfont, bfont; // Current font for top line, rest of lines Choice lesson; Fontlist fontlist; Checkbox direction; Button hint; Button reset; TextField tf; int lfonts[] = new int[100], rfonts[] = new int[100]; String files[] = new String[100]; int dispWidth, dispHeight; int correct = 0; int wrong = 0; String err_str = null; final int h = 31; // Height of the text final int offset = 5; final int textoffset = 15; // Set up the user interface public void init() { int numLessons; lesson = new Choice(); lesson.addItem("--Select lesson--"); setBackground(Color.white); String fontlistfile = getParameter("fontlist"); String lessonfile = getParameter("lessonfile"); URL url = null; DataInputStream dis = null; try { fontlist = new Fontlist(getDocumentBase(), fontlistfile); } catch (Exception e) { err("Couldn't open "+fontlistfile); return; } try { url = new URL(getDocumentBase(), lessonfile); dis = new DataInputStream(url.openStream()); } catch (Exception e) { err("Couldn't open "+lessonfile); return; } try { for (numLessons=0; ;numLessons++) { String line = dis.readLine(); if (line == null) { break; } StringTokenizer st = new StringTokenizer(line); lesson.addItem(st.nextToken(" ")); lfonts[numLessons] = Integer.valueOf(st.nextToken(" ")). intValue(); rfonts[numLessons] = Integer.valueOf(st.nextToken(" ")). intValue(); files[numLessons] = st.nextToken(" "); } } catch (Exception e) { err("IOException "+e); return; } setLayout(new BorderLayout()); Panel p = new Panel(); p.setBackground(Color.white); add("South",p); p.setLayout(new BorderLayout()); tf = new TextField("Select a lesson below", 20); tf.setEditable(false); p.add("North",tf); Panel controls = new Panel(); direction = new Checkbox("Sense"); controls.add(lesson); controls.add(direction); hint = new Button("Hint"); controls.add(hint); reset = new Button("Reset"); controls.add(reset); p.add("South",controls); validate(); dispWidth = size().width; dispHeight = size().height; } // Handle a UI event public boolean handleEvent(Event e) { if (e.target.equals(lesson)) { // Hack for Netscape 4.02 problem action(e, null); } return super.handleEvent(e); } public boolean action(Event e, Object target) { if (e.target.equals(lesson)) { int idx = lesson.getSelectedIndex(); if (idx > 0) { openLesson(idx-1); } return true; } else if (curLesson>=0 && e.target.equals(reset)) { reset(); newQuestion(); } else if (curLesson>=0 && e.target.equals(hint)) { hint(); repaint(); } else if (curLesson>=0 && e.target.equals(direction)) { ansl[num_choices+1] = ""; ansr[num_choices+1] = ""; updateText(); } return false; } // Supply a hint void hint() { int offset = (int)(rand.nextDouble()*num_choices); for (int i=0; i=0 && l< num_choices) { if (l==right) { correct++; tf.setText("Correct. Score: "+correct+"/"+ (correct+wrong)+". Now try this one"); ansl[num_choices+1] = ansl[right]; ansr[num_choices+1] = ansr[right]; l_color[num_choices+1] = Color.gray; r_color[num_choices+1] = Color.gray; newQuestion(); } else { l_color[l] = Color.gray; r_color[l] = Color.gray; wrong++; tf.setText("Wrong. Score: "+correct+"/"+ (correct+wrong)+". Try again"); ansl[num_choices+1] = ""; ansr[num_choices+1] = ""; repaint(); } } return true; } public void update(Graphics g) { paint(g); } Vector line1, line2; final int num_choices = 5; Random rand = new Random(); int choices[] = new int[num_choices]; Color l_color[] = new Color[num_choices+2]; Color r_color[] = new Color[num_choices+2]; int right; String ansl[] = new String[num_choices+2]; String ansr[] = new String[num_choices+2]; // Randomly select 5 choices from the list and one that's right. void newQuestion() { right = (int)(rand.nextDouble()*num_choices); int size = line1.size(); if (size < num_choices) { err("File too small!"); return; } for (int i=0; i